SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Reac%ve	
  Jersey	
  Client	
  
Michal	
  Gajdos	
  
michal.gajdos@oracle.com	
  
	
  
January,	
  2015	
  
Copyright	
  ©	
  2014,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Safe	
  Harbor	
  Statement	
  
The	
  following	
  is	
  intended	
  to	
  outline	
  our	
  general	
  product	
  direc%on.	
  It	
  is	
  intended	
  for	
  
informa%on	
  purposes	
  only,	
  and	
  may	
  not	
  be	
  incorporated	
  into	
  any	
  contract.	
  It	
  is	
  not	
  a	
  
commitment	
  to	
  deliver	
  any	
  material,	
  code,	
  or	
  func%onality,	
  and	
  should	
  not	
  be	
  relied	
  upon	
  
in	
  making	
  purchasing	
  decisions.	
  The	
  development,	
  release,	
  and	
  %ming	
  of	
  any	
  features	
  or	
  
func%onality	
  described	
  for	
  Oracle’s	
  products	
  remains	
  at	
  the	
  sole	
  discre%on	
  of	
  Oracle.	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
The	
  Problem	
  
A	
  Travel	
  Agency	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
A	
  Travel	
  Agency	
  Service	
  
Orchestra%ng	
  Services	
  
Weather	
  
Customers	
  
Des%na%ons	
  
Quo%ng	
  
Travel	
  Agency	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
The	
  Why	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
The	
  Why	
  
•  Client	
  specific	
  API	
  
– Different	
  needs	
  for	
  various	
  devices:	
  screen	
  size,	
  payment	
  methods,	
  ...	
  
•  Single	
  Entry	
  Point	
  
– No	
  need	
  to	
  communicate	
  with	
  mul%ple	
  services	
  
•  Thinner	
  client	
  
– No	
  need	
  to	
  consume	
  different	
  formats	
  of	
  data	
  
•  Less	
  frequent	
  client	
  updates	
  
– Doesn’t	
  ma]er	
  if	
  one	
  service	
  is	
  removed	
  in	
  favor	
  of	
  another	
  service	
  
Building	
  an	
  Orchestra6on	
  Layer	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
The	
  How	
  
JAX-­‐RS	
  2.0	
  and	
  Jersey	
  2	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Demo	
  Applica%on	
  
•  “Remote”	
  
– applica%on/json,	
  applica%on/xml	
  
– delays	
  
•  “Agent”	
  
– applica%on/json	
  
– dependent	
  calls	
  
Exposed	
  resources	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
A	
  Naïve	
  Approach	
  
Implemen%ng	
  the	
  Service	
  
Get	
  
Customer	
  Details	
  
Get	
  a	
  list	
  of	
  10	
  
Recommended	
  
Des6na6ons	
  
Get	
  
Quote	
  
for	
  the	
  Customer	
  
Get	
  	
  
Weather	
  Forecast	
  
for	
  each	
  Des6na6on	
   for	
  each	
  Des6na6on	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Client	
  client	
  =	
  ClientBuilder.newClient();	
  
	
  
WebTarget	
  rx	
  =	
  client.target("http://example.com/rx").register(JacksonFeature.class);	
  
	
  
WebTarget	
  forecasts	
  =	
  rx.path("remote/forecast/{destination}");	
  
	
  
Forecast	
  forecast	
  =	
  forecasts.resolveTemplate("destination",	
  dest.getDestination())	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .request("application/xml")	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .get(Forecast.class);	
  
JAX-­‐RS	
  2.0	
  Client	
  –	
  Synchronous	
  
Copyright	
  ©	
  2014,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
DEMO	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
A	
  Naïve	
  Approach	
  
Implemen%ng	
  the	
  Service	
  
Get	
  
Customer	
  Details	
  
Get	
  a	
  list	
  of	
  10	
  
Recommended	
  
Des6na6ons	
  
Get	
  
Quote	
  
for	
  the	
  Customer	
  
Get	
  	
  
Weather	
  Forecast	
  
for	
  each	
  Des6na6on	
   for	
  each	
  Des6na6on	
  
150	
  ms	
   250	
  ms	
   1	
  700	
  ms	
  
170	
  ms	
  
3	
  300	
  ms	
  
330	
  ms	
  
5	
  400	
  ms	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Client	
  –	
  Synchronous	
  Approach	
  
•  Easy	
  to	
  read,	
  understand	
  and	
  debug	
  
– Simple	
  requests,	
  Composed	
  requests	
  
•  Slow	
  
– Sequen%al	
  processing	
  even	
  for	
  independent	
  requests	
  
•  Was%ng	
  resources	
  
– Wai%ng	
  threads	
  
•  Suitable	
  for	
  
– Lower	
  number	
  of	
  requests	
  
– Single	
  request	
  that	
  depends	
  on	
  the	
  result	
  of	
  previous	
  opera%on	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Op6mized	
  Approach	
  
Implemen%ng	
  the	
  Service	
  
170	
  ms	
  
Get	
  
Customer	
  Details	
  
Get	
  a	
  list	
  of	
  10	
  
Recommended	
  
Des6na6ons	
  
Async	
  Get	
  
Quote	
  
for	
  the	
  Customer	
  
Async	
  Get	
  	
  
Weather	
  Forecast	
  
for	
  each	
  Des6na6on	
  
for	
  each	
  Des6na6on	
  
150	
  ms	
   250	
  ms	
   330	
  ms	
  
330	
  ms	
  
730	
  ms	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Future<Forecast>	
  forecast	
  =	
  forecasts.resolveTemplate("destination",	
  d.getDestination())	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .request()	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .async()	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .get(new	
  InvocationCallback<Forecast>()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  @Override	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  completed(Forecast	
  forecast)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Do	
  Something.	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  @Override	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  failed(Throwable	
  throwable)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Do	
  Something	
  else.	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  });	
  
while	
  (!forecast.isDone())	
  {	
  
	
  	
  	
  	
  //	
  Do	
  Something.	
  
}	
  
System.out.println(forecast.get());	
  
JAX-­‐RS	
  2.0	
  Client	
  –	
  Asynchronous	
  
Copyright	
  ©	
  2014,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
DEMO	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Client	
  –	
  Asynchronous	
  Approach	
  
•  Returns	
  immediately	
  afer	
  submigng	
  a	
  request	
  
– Future	
  
•  Harder	
  to	
  understand,	
  debug	
  
– Especially	
  when	
  dealing	
  with	
  mul%ple	
  futures	
  	
  
•  Fast	
  
– Each	
  request	
  can	
  run	
  on	
  a	
  separate	
  thread	
  
– Need	
  to	
  ac%vely	
  check	
  for	
  comple%on	
  event	
  (future.isDone())	
  or	
  block	
  (slow)	
  
Futures	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Client	
  –	
  Asynchronous	
  Approach	
  
•  “Don’t	
  call	
  us,	
  we’ll	
  call	
  you”	
  
•  Harder	
  to	
  read,	
  understand	
  and	
  debug	
  
– Especially	
  for	
  composed	
  calls	
  (dependent)	
  
•  Need	
  to	
  find	
  out	
  when	
  all	
  Async	
  requests	
  finished	
  
– Relevant	
  only	
  for	
  2	
  or	
  more	
  requests	
  (CountDownLatch)	
  
•  Fast	
  
– Each	
  request	
  can	
  run	
  on	
  a	
  separate	
  thread	
  
•  Suitable	
  for	
  
– Many	
  independent	
  calls	
  	
  
The	
  Callback	
  Hell	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Beyond	
  The	
  Callback	
  Hell	
  
Reac6ve	
  (Jersey)	
  Client	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Client	
  –	
  Reac%ve	
  Approach	
  
•  Data-­‐Flows	
  
– Execu%on	
  model	
  propagates	
  changes	
  through	
  the	
  flow	
  
•  Asynchronous	
  
– Preferably,	
  Speed	
  
•  Event-­‐based	
  
– No%fy	
  user	
  code	
  or	
  another	
  item	
  in	
  flow	
  about	
  con%nua%on,	
  error,	
  comple%on	
  
•  Composable	
  
– Compose/Transform	
  mul%ple	
  flows	
  into	
  the	
  resul%ng	
  one	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Reac%ve	
  Java	
  Libraries	
  
•  RxJava	
  –	
  Observable	
  
– Analogy	
  to	
  Iterable	
  	
  
– Currently	
  most	
  advanced	
  reac%ve	
  API	
  in	
  Java	
  
– Contributed	
  by	
  Nemlix	
  –	
  hardened	
  &	
  tested	
  in	
  produc%on	
  
•  Java	
  SE	
  8	
  –	
  CompletionStage	
  and	
  CompletableFuture	
  
– Na%ve	
  part	
  of	
  JDK	
  
– Fits	
  the	
  new	
  Java	
  Stream	
  API	
  programming	
  model	
  
– JSR166e	
  –	
  Support	
  for	
  CompletableFuture	
  on	
  Java	
  SE	
  6	
  and	
  Java	
  SE	
  7	
  
•  Guava	
  –	
  ListenableFuture	
  and	
  Futures	
  
– Similar	
  to	
  Java	
  SE	
  8	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Observable	
  
•  Observable	
  (push)	
  
– retrieve	
  data	
  –	
  onNext(T)	
  
– discover	
  error	
  –	
  onError(Exception)	
  
– complete	
  –	
  onCompleted()	
  
•  Iterable	
  (pull)	
  
– retrieve	
  data	
  –	
  T	
  next()	
  
– discover	
  error	
  –	
  throws	
  Exception	
  
– complete	
  –	
  !hasNext()	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Observable<Response>	
  response	
  =	
  …	
  ;	
  
	
  
List<String>	
  visited	
  =	
  new	
  ArrayList<>(10);	
  
	
  
//	
  Read	
  a	
  list	
  of	
  destinations	
  from	
  JAX-­‐RS	
  response	
  
response.map(resp	
  -­‐>	
  resp.readEntity(new	
  GenericType<List<Destination>>()	
  {}))	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  If	
  an	
  exception	
  is	
  thrown,	
  continue	
  with	
  an	
  empty	
  list	
  
	
  	
  	
  	
  	
  	
  	
  	
  .onErrorReturn(throwable	
  -­‐>	
  Collections.emptyList())	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Emit	
  list	
  of	
  destinations	
  as	
  a	
  new	
  Observable	
  
	
  	
  	
  	
  	
  	
  	
  	
  .flatMap(Observable::from)	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Take	
  the	
  first	
  10	
  destinations	
  
	
  	
  	
  	
  	
  	
  	
  	
  .take(10)	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Obtain	
  a	
  string	
  representation	
  of	
  a	
  destination	
  
	
  	
  	
  	
  	
  	
  	
  	
  .map(Destination::getDestination)	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Observe	
  the	
  destination	
  events	
  on	
  a	
  separate	
  thread	
  
	
  	
  	
  	
  	
  	
  	
  	
  .observeOn(Schedulers.io())	
  
	
  	
  	
  	
  	
  	
  	
  	
  //	
  Subscribe	
  to	
  callbacks	
  –	
  OnNext,	
  OnError,	
  OnComplete	
  
	
  	
  	
  	
  	
  	
  	
  	
  .subscribe(visited::add,	
  async::resume,	
  ()	
  -­‐>	
  async.resume(visited));	
  
An	
  Observable<Response>	
  Example	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Reac%ve	
  Jersey	
  Client	
  
•  Remember	
  #request()	
  and	
  #request().async()	
  ?	
  
– request()	
  returns	
  Invoca6on.Builder;	
  SyncInvoker	
  –	
  sync	
  HTTP	
  methods	
  
– request().async()	
  returns	
  AsyncInvoker	
  –	
  async	
  HTTP	
  methods	
  
•  #rx()	
  and	
  #rx(ExecutorService)	
  
– Return	
  an	
  extension	
  of	
  RxInvoker	
  
Extension	
  of	
  JAX-­‐RS	
  Client	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
public	
  interface	
  SyncInvoker	
  {	
  
	
  
	
  	
  	
  	
  Response	
  get();	
  
	
  
	
  	
  	
  	
  <T>	
  T	
  get(Class<T>	
  responseType);	
  
	
  
	
  	
  	
  	
  <T>	
  T	
  get(GenericType<T>	
  responseType);	
  
	
  
	
  	
  	
  	
  //	
  ...	
  
}	
  
	
  
public	
  interface	
  AsyncInvoker	
  {	
  
	
  
	
  	
  	
  	
  Future<Response>	
  get();	
  
	
  
	
  	
  	
  	
  <T>	
  Future<T>	
  get(Class<T>	
  responseType);	
  
	
  
	
  	
  	
  	
  <T>	
  Future<T>	
  get(GenericType<T>	
  responseType);	
  
	
  
	
  	
  	
  	
  //	
  ...	
  
}	
  
	
  
SyncInvoker	
  and	
  AsyncInvoker	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
public	
  interface	
  RxInvoker<T>	
  {	
  
	
  
	
  	
  	
  	
  T	
  get();	
  
	
  
	
  	
  	
  	
  <R>	
  T	
  get(Class<R>	
  responseType);	
  
	
  
	
  	
  	
  	
  <R>	
  T	
  get(GenericType<R>	
  responseType);	
  
	
  
	
  	
  	
  	
  //	
  ...	
  
}	
  
	
  
public	
  interface	
  RxObservableInvoker	
  extends	
  RxInvoker<Observable>	
  {	
  
	
  
	
  	
  	
  	
  Observable<Response>	
  get();	
  
	
  
	
  	
  	
  	
  <T>	
  Observable<T>	
  get(Class<T>	
  responseType);	
  
	
  
	
  	
  	
  	
  <T>	
  Observable<T>	
  get(GenericType<T>	
  responseType);	
  
	
  
	
  	
  	
  	
  //	
  ...	
  
}	
  
RxInvoker	
  and	
  an	
  extension	
  Example	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Reac%ve	
  Jersey	
  Client	
  –	
  contd	
  
•  Affected	
  JAX-­‐RS	
  interfaces	
  
– RxInvoca%onBuilder<RX	
  extends	
  RxInvoker>	
  extends	
  Invoca%on.Builder	
  
– RxWebTarget<RX	
  extends	
  RxInvoker>	
  extends	
  WebTarget	
  
– RxClient<RX	
  extends	
  RxInvoker>	
  extends	
  Client	
  
•  Rx	
  class	
  
– RxObservable	
  
– RxComple%onStage	
  
– RxListenableFuture	
  
– RxCompletableFuture	
  (JSR	
  166e)	
  
Extension	
  of	
  JAX-­‐RS	
  Client	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Client	
  client	
  =	
  ClientBuilder.newClient();	
  
WebTarget	
  target	
  =	
  client.target("...");	
  
	
  
	
  
//	
  Rx	
  
RxClient<RxObservableInvoker>	
  rxClient	
  =	
  Rx.newClient(RxObservableInvoker.class);	
  
RxClient<RxObservableInvoker>	
  rxClient	
  =	
  Rx.client(client,	
  RxObservableInvoker.class);	
  
	
  
RxWebTarget<RxObservableInvoker>	
  rxTarget	
  =	
  
	
  	
  	
  	
  Rx.target(target,	
  RxObservableInvoker.class);	
  
	
  
	
  
//	
  RxObservable	
  
RxClient<RxObservableInvoker>	
  rxClient	
  =	
  RxObservable.newClient();	
  
RxClient<RxObservableInvoker>	
  rxClient	
  =	
  RxObservable.client(client);	
  
	
  
RxWebTarget<RxObservableInvoker>	
  rxTarget	
  =	
  RxObservable.target(target);	
  
	
  
	
  
Reac%ve	
  Client	
  –	
  Crea%on	
  
Copyright	
  ©	
  2014,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
DEMO	
  
Copyright	
  ©	
  2014,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Q&A	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Resources	
  
•  JAX-­‐RS	
  Client	
  API	
  
– h]ps://jax-­‐rs-­‐spec.java.net/nonav/2.0/apidocs/overview-­‐summary.html	
  
– h]ps://jersey.java.net/documenta%on/latest/client.html	
  
•  Jersey	
  Rx	
  Client	
  
– h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client	
  
– h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐guava	
  
– h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐java8	
  
– h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐jsr166e	
  
– h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐rxjava	
  
JAX-­‐RS	
  and	
  Jersey	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  	
  
Resources	
  
•  3rd	
  party	
  libraries	
  
– h]ps://code.google.com/p/guava-­‐libraries/	
  
– h]ps://github.com/Reac%veX/RxJava	
  
– h]p://docs.oracle.com/javase/8/docs/api/java/u%l/concurrent/package-­‐
summary.html	
  
– h]p://gee.cs.oswego.edu/dl/concurrency-­‐interest/index.html	
  
•  Example	
  (JDK7)	
  
– h]ps://github.com/jersey/jersey/tree/master/examples/rx-­‐client-­‐webapp	
  
Example	
  and	
  Libraries	
  

Más contenido relacionado

La actualidad más candente

Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lightbend
 

La actualidad más candente (19)

Typesafe Reactive Platform: Monitoring 1.0, Commercial features and more
Typesafe Reactive Platform: Monitoring 1.0, Commercial features and moreTypesafe Reactive Platform: Monitoring 1.0, Commercial features and more
Typesafe Reactive Platform: Monitoring 1.0, Commercial features and more
 
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
 
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
[Japanese] How Reactive Streams and Akka Streams change the JVM Ecosystem @ R...
 
Akka-chan's Survival Guide for the Streaming World
Akka-chan's Survival Guide for the Streaming WorldAkka-chan's Survival Guide for the Streaming World
Akka-chan's Survival Guide for the Streaming World
 
How Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM EcosystemHow Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM Ecosystem
 
Akka 2.4 plus new commercial features in Typesafe Reactive Platform
Akka 2.4 plus new commercial features in Typesafe Reactive PlatformAkka 2.4 plus new commercial features in Typesafe Reactive Platform
Akka 2.4 plus new commercial features in Typesafe Reactive Platform
 
Akka Streams in Action @ ScalaDays Berlin 2016
Akka Streams in Action @ ScalaDays Berlin 2016Akka Streams in Action @ ScalaDays Berlin 2016
Akka Streams in Action @ ScalaDays Berlin 2016
 
Architectural changes in the repo in 6.1 and beyond
Architectural changes in the repo in 6.1 and beyondArchitectural changes in the repo in 6.1 and beyond
Architectural changes in the repo in 6.1 and beyond
 
What’s expected in Spring 5
What’s expected in Spring 5What’s expected in Spring 5
What’s expected in Spring 5
 
Do's and don'ts when deploying akka in production
Do's and don'ts when deploying akka in productionDo's and don'ts when deploying akka in production
Do's and don'ts when deploying akka in production
 
End to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to SocketEnd to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to Socket
 
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
 
Implementing Micro Services Tasks (service discovery, load balancing etc.) - ...
Implementing Micro Services Tasks (service discovery, load balancing etc.) - ...Implementing Micro Services Tasks (service discovery, load balancing etc.) - ...
Implementing Micro Services Tasks (service discovery, load balancing etc.) - ...
 
Exachk and oem12c
Exachk and oem12cExachk and oem12c
Exachk and oem12c
 
Dropwizard and Groovy
Dropwizard and GroovyDropwizard and Groovy
Dropwizard and Groovy
 
#GeodeSummit - Apex & Geode: In-memory streaming, storage & analytics
#GeodeSummit - Apex & Geode: In-memory streaming, storage & analytics#GeodeSummit - Apex & Geode: In-memory streaming, storage & analytics
#GeodeSummit - Apex & Geode: In-memory streaming, storage & analytics
 
Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's GuideDeveloping Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's Guide
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Developing Resilient Cloud Native Apps with Spring Cloud
Developing Resilient Cloud Native Apps with Spring CloudDeveloping Resilient Cloud Native Apps with Spring Cloud
Developing Resilient Cloud Native Apps with Spring Cloud
 

Destacado

Reactive Streams and RabbitMQ
Reactive Streams and RabbitMQReactive Streams and RabbitMQ
Reactive Streams and RabbitMQ
mkiedys
 
Akka in Practice: Designing Actor-based Applications
Akka in Practice: Designing Actor-based ApplicationsAkka in Practice: Designing Actor-based Applications
Akka in Practice: Designing Actor-based Applications
NLJUG
 

Destacado (10)

Tachyon and Apache Spark
Tachyon and Apache SparkTachyon and Apache Spark
Tachyon and Apache Spark
 
Reactive streams
Reactive streamsReactive streams
Reactive streams
 
xPatterns on Spark, Shark, Mesos, Tachyon
xPatterns on Spark, Shark, Mesos, TachyonxPatterns on Spark, Shark, Mesos, Tachyon
xPatterns on Spark, Shark, Mesos, Tachyon
 
PSUG #52 Dataflow and simplified reactive programming with Akka-streams
PSUG #52 Dataflow and simplified reactive programming with Akka-streamsPSUG #52 Dataflow and simplified reactive programming with Akka-streams
PSUG #52 Dataflow and simplified reactive programming with Akka-streams
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Reactive Streams and RabbitMQ
Reactive Streams and RabbitMQReactive Streams and RabbitMQ
Reactive Streams and RabbitMQ
 
Akka in Practice: Designing Actor-based Applications
Akka in Practice: Designing Actor-based ApplicationsAkka in Practice: Designing Actor-based Applications
Akka in Practice: Designing Actor-based Applications
 
xPatterns ... beyond Hadoop (Spark, Shark, Mesos, Tachyon)
xPatterns ... beyond Hadoop (Spark, Shark, Mesos, Tachyon)xPatterns ... beyond Hadoop (Spark, Shark, Mesos, Tachyon)
xPatterns ... beyond Hadoop (Spark, Shark, Mesos, Tachyon)
 
Micro services, reactive manifesto and 12-factors
Micro services, reactive manifesto and 12-factorsMicro services, reactive manifesto and 12-factors
Micro services, reactive manifesto and 12-factors
 
12 Factor App: Best Practices for JVM Deployment
12 Factor App: Best Practices for JVM Deployment12 Factor App: Best Practices for JVM Deployment
12 Factor App: Best Practices for JVM Deployment
 

Similar a Reactive Jersey Client

Similar a Reactive Jersey Client (20)

Stream Analytics
Stream Analytics Stream Analytics
Stream Analytics
 
JAX-RS.next
JAX-RS.nextJAX-RS.next
JAX-RS.next
 
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute InfodeckServlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
 
How to Thrive on REST/WebSocket-Based Microservices
How to Thrive on REST/WebSocket-Based MicroservicesHow to Thrive on REST/WebSocket-Based Microservices
How to Thrive on REST/WebSocket-Based Microservices
 
HTTP/2 Comes to Java - What Servlet 4.0 Means to You
HTTP/2 Comes to Java - What Servlet 4.0 Means to YouHTTP/2 Comes to Java - What Servlet 4.0 Means to You
HTTP/2 Comes to Java - What Servlet 4.0 Means to You
 
JAX RS and CDI bike the reactive bridge
JAX RS and CDI bike the reactive bridgeJAX RS and CDI bike the reactive bridge
JAX RS and CDI bike the reactive bridge
 
JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013
 
JAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) BridgeJAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) Bridge
 
Oracle REST Data Services
Oracle REST Data ServicesOracle REST Data Services
Oracle REST Data Services
 
Why Upgrade to Oracle Database 12c?
Why Upgrade to Oracle Database 12c?Why Upgrade to Oracle Database 12c?
Why Upgrade to Oracle Database 12c?
 
QA standup - workload analysis
QA standup  - workload analysisQA standup  - workload analysis
QA standup - workload analysis
 
Alta Disponibilidade no MySQL 5.7
Alta Disponibilidade no MySQL 5.7Alta Disponibilidade no MySQL 5.7
Alta Disponibilidade no MySQL 5.7
 
Real World Problem Solving Using Application Performance Management 10
Real World Problem Solving Using Application Performance Management 10Real World Problem Solving Using Application Performance Management 10
Real World Problem Solving Using Application Performance Management 10
 
112815 java ee8_davidd
112815 java ee8_davidd112815 java ee8_davidd
112815 java ee8_davidd
 
Explore Advanced CA Release Automation Configuration Topics
Explore Advanced CA Release Automation Configuration TopicsExplore Advanced CA Release Automation Configuration Topics
Explore Advanced CA Release Automation Configuration Topics
 
Jfokus 2017 Oracle Dev Cloud and Containers
Jfokus 2017 Oracle Dev Cloud and ContainersJfokus 2017 Oracle Dev Cloud and Containers
Jfokus 2017 Oracle Dev Cloud and Containers
 
(ZDM) Zero Downtime DB Migration to Oracle Cloud
(ZDM) Zero Downtime DB Migration to Oracle Cloud(ZDM) Zero Downtime DB Migration to Oracle Cloud
(ZDM) Zero Downtime DB Migration to Oracle Cloud
 
Oracle Cloud Platform - Migrer vers la Database 12c
Oracle Cloud Platform - Migrer vers la Database 12cOracle Cloud Platform - Migrer vers la Database 12c
Oracle Cloud Platform - Migrer vers la Database 12c
 
Coherence 12.1.2 Live Events
Coherence 12.1.2 Live EventsCoherence 12.1.2 Live Events
Coherence 12.1.2 Live Events
 
Pushing JavaEE outside of the enterprise: Home Automation & IoT - David Delab...
Pushing JavaEE outside of the enterprise: Home Automation & IoT - David Delab...Pushing JavaEE outside of the enterprise: Home Automation & IoT - David Delab...
Pushing JavaEE outside of the enterprise: Home Automation & IoT - David Delab...
 

Último

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Último (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 

Reactive Jersey Client

  • 1. Reac%ve  Jersey  Client   Michal  Gajdos   michal.gajdos@oracle.com     January,  2015   Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      
  • 2. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Safe  Harbor  Statement   The  following  is  intended  to  outline  our  general  product  direc%on.  It  is  intended  for   informa%on  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a   commitment  to  deliver  any  material,  code,  or  func%onality,  and  should  not  be  relied  upon   in  making  purchasing  decisions.  The  development,  release,  and  %ming  of  any  features  or   func%onality  described  for  Oracle’s  products  remains  at  the  sole  discre%on  of  Oracle.  
  • 3. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       The  Problem   A  Travel  Agency  
  • 4. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       A  Travel  Agency  Service   Orchestra%ng  Services   Weather   Customers   Des%na%ons   Quo%ng   Travel  Agency  
  • 5. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       The  Why  
  • 6. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       The  Why   •  Client  specific  API   – Different  needs  for  various  devices:  screen  size,  payment  methods,  ...   •  Single  Entry  Point   – No  need  to  communicate  with  mul%ple  services   •  Thinner  client   – No  need  to  consume  different  formats  of  data   •  Less  frequent  client  updates   – Doesn’t  ma]er  if  one  service  is  removed  in  favor  of  another  service   Building  an  Orchestra6on  Layer  
  • 7. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       The  How   JAX-­‐RS  2.0  and  Jersey  2  
  • 8. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Demo  Applica%on   •  “Remote”   – applica%on/json,  applica%on/xml   – delays   •  “Agent”   – applica%on/json   – dependent  calls   Exposed  resources  
  • 9. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       A  Naïve  Approach   Implemen%ng  the  Service   Get   Customer  Details   Get  a  list  of  10   Recommended   Des6na6ons   Get   Quote   for  the  Customer   Get     Weather  Forecast   for  each  Des6na6on   for  each  Des6na6on  
  • 10. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Client  client  =  ClientBuilder.newClient();     WebTarget  rx  =  client.target("http://example.com/rx").register(JacksonFeature.class);     WebTarget  forecasts  =  rx.path("remote/forecast/{destination}");     Forecast  forecast  =  forecasts.resolveTemplate("destination",  dest.getDestination())                                                            .request("application/xml")                                                            .get(Forecast.class);   JAX-­‐RS  2.0  Client  –  Synchronous  
  • 11. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       DEMO  
  • 12. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       A  Naïve  Approach   Implemen%ng  the  Service   Get   Customer  Details   Get  a  list  of  10   Recommended   Des6na6ons   Get   Quote   for  the  Customer   Get     Weather  Forecast   for  each  Des6na6on   for  each  Des6na6on   150  ms   250  ms   1  700  ms   170  ms   3  300  ms   330  ms   5  400  ms  
  • 13. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Client  –  Synchronous  Approach   •  Easy  to  read,  understand  and  debug   – Simple  requests,  Composed  requests   •  Slow   – Sequen%al  processing  even  for  independent  requests   •  Was%ng  resources   – Wai%ng  threads   •  Suitable  for   – Lower  number  of  requests   – Single  request  that  depends  on  the  result  of  previous  opera%on  
  • 14. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Op6mized  Approach   Implemen%ng  the  Service   170  ms   Get   Customer  Details   Get  a  list  of  10   Recommended   Des6na6ons   Async  Get   Quote   for  the  Customer   Async  Get     Weather  Forecast   for  each  Des6na6on   for  each  Des6na6on   150  ms   250  ms   330  ms   330  ms   730  ms  
  • 15. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Future<Forecast>  forecast  =  forecasts.resolveTemplate("destination",  d.getDestination())                                                                          .request()                                                                          .async()                                                                          .get(new  InvocationCallback<Forecast>()  {                                                                                  @Override                                                                                  public  void  completed(Forecast  forecast)  {                                                                                          //  Do  Something.                                                                                  }                                                      @Override                                                    public  void  failed(Throwable  throwable)  {                                                            //  Do  Something  else.                                                    }                                              });   while  (!forecast.isDone())  {          //  Do  Something.   }   System.out.println(forecast.get());   JAX-­‐RS  2.0  Client  –  Asynchronous  
  • 16. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       DEMO  
  • 17. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Client  –  Asynchronous  Approach   •  Returns  immediately  afer  submigng  a  request   – Future   •  Harder  to  understand,  debug   – Especially  when  dealing  with  mul%ple  futures     •  Fast   – Each  request  can  run  on  a  separate  thread   – Need  to  ac%vely  check  for  comple%on  event  (future.isDone())  or  block  (slow)   Futures  
  • 18. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Client  –  Asynchronous  Approach   •  “Don’t  call  us,  we’ll  call  you”   •  Harder  to  read,  understand  and  debug   – Especially  for  composed  calls  (dependent)   •  Need  to  find  out  when  all  Async  requests  finished   – Relevant  only  for  2  or  more  requests  (CountDownLatch)   •  Fast   – Each  request  can  run  on  a  separate  thread   •  Suitable  for   – Many  independent  calls     The  Callback  Hell  
  • 19. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Beyond  The  Callback  Hell   Reac6ve  (Jersey)  Client  
  • 20. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Client  –  Reac%ve  Approach   •  Data-­‐Flows   – Execu%on  model  propagates  changes  through  the  flow   •  Asynchronous   – Preferably,  Speed   •  Event-­‐based   – No%fy  user  code  or  another  item  in  flow  about  con%nua%on,  error,  comple%on   •  Composable   – Compose/Transform  mul%ple  flows  into  the  resul%ng  one  
  • 21. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Reac%ve  Java  Libraries   •  RxJava  –  Observable   – Analogy  to  Iterable     – Currently  most  advanced  reac%ve  API  in  Java   – Contributed  by  Nemlix  –  hardened  &  tested  in  produc%on   •  Java  SE  8  –  CompletionStage  and  CompletableFuture   – Na%ve  part  of  JDK   – Fits  the  new  Java  Stream  API  programming  model   – JSR166e  –  Support  for  CompletableFuture  on  Java  SE  6  and  Java  SE  7   •  Guava  –  ListenableFuture  and  Futures   – Similar  to  Java  SE  8  
  • 22. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Observable   •  Observable  (push)   – retrieve  data  –  onNext(T)   – discover  error  –  onError(Exception)   – complete  –  onCompleted()   •  Iterable  (pull)   – retrieve  data  –  T  next()   – discover  error  –  throws  Exception   – complete  –  !hasNext()  
  • 23. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Observable<Response>  response  =  …  ;     List<String>  visited  =  new  ArrayList<>(10);     //  Read  a  list  of  destinations  from  JAX-­‐RS  response   response.map(resp  -­‐>  resp.readEntity(new  GenericType<List<Destination>>()  {}))                  //  If  an  exception  is  thrown,  continue  with  an  empty  list                  .onErrorReturn(throwable  -­‐>  Collections.emptyList())                  //  Emit  list  of  destinations  as  a  new  Observable                  .flatMap(Observable::from)                  //  Take  the  first  10  destinations                  .take(10)                  //  Obtain  a  string  representation  of  a  destination                  .map(Destination::getDestination)                  //  Observe  the  destination  events  on  a  separate  thread                  .observeOn(Schedulers.io())                  //  Subscribe  to  callbacks  –  OnNext,  OnError,  OnComplete                  .subscribe(visited::add,  async::resume,  ()  -­‐>  async.resume(visited));   An  Observable<Response>  Example  
  • 24. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Reac%ve  Jersey  Client   •  Remember  #request()  and  #request().async()  ?   – request()  returns  Invoca6on.Builder;  SyncInvoker  –  sync  HTTP  methods   – request().async()  returns  AsyncInvoker  –  async  HTTP  methods   •  #rx()  and  #rx(ExecutorService)   – Return  an  extension  of  RxInvoker   Extension  of  JAX-­‐RS  Client  
  • 25. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       public  interface  SyncInvoker  {            Response  get();            <T>  T  get(Class<T>  responseType);            <T>  T  get(GenericType<T>  responseType);            //  ...   }     public  interface  AsyncInvoker  {            Future<Response>  get();            <T>  Future<T>  get(Class<T>  responseType);            <T>  Future<T>  get(GenericType<T>  responseType);            //  ...   }     SyncInvoker  and  AsyncInvoker  
  • 26. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       public  interface  RxInvoker<T>  {            T  get();            <R>  T  get(Class<R>  responseType);            <R>  T  get(GenericType<R>  responseType);            //  ...   }     public  interface  RxObservableInvoker  extends  RxInvoker<Observable>  {            Observable<Response>  get();            <T>  Observable<T>  get(Class<T>  responseType);            <T>  Observable<T>  get(GenericType<T>  responseType);            //  ...   }   RxInvoker  and  an  extension  Example  
  • 27. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Reac%ve  Jersey  Client  –  contd   •  Affected  JAX-­‐RS  interfaces   – RxInvoca%onBuilder<RX  extends  RxInvoker>  extends  Invoca%on.Builder   – RxWebTarget<RX  extends  RxInvoker>  extends  WebTarget   – RxClient<RX  extends  RxInvoker>  extends  Client   •  Rx  class   – RxObservable   – RxComple%onStage   – RxListenableFuture   – RxCompletableFuture  (JSR  166e)   Extension  of  JAX-­‐RS  Client  
  • 28. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Client  client  =  ClientBuilder.newClient();   WebTarget  target  =  client.target("...");       //  Rx   RxClient<RxObservableInvoker>  rxClient  =  Rx.newClient(RxObservableInvoker.class);   RxClient<RxObservableInvoker>  rxClient  =  Rx.client(client,  RxObservableInvoker.class);     RxWebTarget<RxObservableInvoker>  rxTarget  =          Rx.target(target,  RxObservableInvoker.class);       //  RxObservable   RxClient<RxObservableInvoker>  rxClient  =  RxObservable.newClient();   RxClient<RxObservableInvoker>  rxClient  =  RxObservable.client(client);     RxWebTarget<RxObservableInvoker>  rxTarget  =  RxObservable.target(target);       Reac%ve  Client  –  Crea%on  
  • 29. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       DEMO  
  • 30. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Q&A  
  • 31. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Resources   •  JAX-­‐RS  Client  API   – h]ps://jax-­‐rs-­‐spec.java.net/nonav/2.0/apidocs/overview-­‐summary.html   – h]ps://jersey.java.net/documenta%on/latest/client.html   •  Jersey  Rx  Client   – h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client   – h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐guava   – h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐java8   – h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐jsr166e   – h]ps://github.com/jersey/jersey/tree/master/incubator/rx/rx-­‐client-­‐rxjava   JAX-­‐RS  and  Jersey  
  • 32. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.       Resources   •  3rd  party  libraries   – h]ps://code.google.com/p/guava-­‐libraries/   – h]ps://github.com/Reac%veX/RxJava   – h]p://docs.oracle.com/javase/8/docs/api/java/u%l/concurrent/package-­‐ summary.html   – h]p://gee.cs.oswego.edu/dl/concurrency-­‐interest/index.html   •  Example  (JDK7)   – h]ps://github.com/jersey/jersey/tree/master/examples/rx-­‐client-­‐webapp   Example  and  Libraries