con 2107- think async: embrace and get addicted to the asynchronicity of ee

41

Upload: masoud-kalali

Post on 23-Jun-2015

161 views

Category:

Technology


2 download

DESCRIPTION

This presentation covers the whole spectrum of Asynchronous processing present in Java EE through introducing a use case. The coverage starts with introducing the usecase and showing how a mix of JMS, MDB and Async Servlet can address the requirement of the use case. The session will also cover asynchronicity in JAX-RS as well as covering NIO in Servlet 3.1

TRANSCRIPT

Page 1: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE
Page 2: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Think  Async  

Masoud  Kalali,  SoEware  Engineer,  ORACLE,  @MasoudKalal  

Embrace  and  Get  Addicted  to  the  Asynchronicity  of  EE  

Page 3: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Safe  Harbor  Statement  The  following  is  intended  to  outline  our  general  product  direcNon.  It  is  intended  for  informaNon  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver  any  material,  code,  or  funcNonality,  and  should  not  be  relied  upon  in  making  purchasing  decisions.  The  development,  release,  and  Nming  of  any  features  or  funcNonality  described  for  Oracle’s  products  remains  at  the  sole  discreNon  of  Oracle.  

Page 4: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Program  Agenda  

IntroducNon  

Why  to  think  Async  

A  simple  case  of  using  Async  

JMS  (2.0)  

Async  Servlet  

Async  JAX-­‐RS  

Async  EJBs  

1  

2  

3  

4  

5  

6  

7  

Page 5: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Speakers  

Masoud  Kalali  •  SoEware  engineer,  author,  blogger  •  Long  advocate  of  GlassFish  and  Java  EE  •  Tweets  at  @MasoudKalali    

Page 6: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Why  to  think  Async?  •  To  be]er  model  what  we  develop  for  •  To  further  decouple  – Ease  of  administraNon  – Ease  of  tuning  – Ease  of  maintenance  

•  Improve  consumers  experience  – An  API  consumer  – A  direct  GUI  for  human  use  – etc.  

 

Page 7: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Java  EE  And  ASynchronicity  •  Long  present  JMS  – To  be  used  almost  everywhere  

•  Servlet  3.0/  3.1  – Asynchronous  Servlets  – None  blocking  IO  

•  JAX-­‐RS  2.0  – Server  side  – Client  side  

•  Asynchronous  Session  Beans  – Server  side  – Client  side  

Page 8: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

A  simple  asynchronous  nourished  use  case  

Client  

Cache  

Query  

Processor  

JMS  

Queue  

JMS  

Queue  

Async  Servlet  

MDB  

ID,  AsyncContext  

Query  Requests  in  JMS  

Chunks  of  Query  results  Consume  result  chunks  and    send  it  back  via  AsyncContext  1  

1   2.1   3  

2  

4….  

5….  6….  7….  

8….  

No  HA/Fault  tolerance  considered,  This  Is  a  very  basic  block  diagram  to  help  with  the  presentaNon    

Page 9: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

A  simple  asynchronous  nourished  use  case  

• One  JMS  queue  for  sending  the  query  requests  • One  JMS  queue  which  will  get  chunks  of  query  results  • An  MDB  which  will  consume  the  query  result  messages  – Uses  the  cache  and  the  id  in  the  message  to  pick  up  the  right  AsyncContext  – As  long  as  the  message  does  not  say  it  is  done  it  will  not  conclude  the  response  

• Complete  the  response  when  JMS  message  implies  

More  details  of  the  Messaging  components  

Page 10: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

A  simple  asynchronous  nourished  use  case  

• Asynchronous  Servlet  receiving  the  requests  • Caching  the  AsynContext  and  a  query  Id  •  Sending  the  Query  message  to  the  JMS  queue  – Including  the  query  Id  

•  Leave  it  to  the  MDB  to  update  the  response  • Have  a  AsyncListener  to  send  proper  response  if  Nmeouts  

More  details  of  the  Servlet  components  

Page 11: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

A  simple  asynchronous  nourished  use  case  

• A  browser  • Can  be    – A  SSE  client  – A  COMET,  Long  Polling  request  

Some  basics  on  the  client  side  

Page 12: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

JMS  (2.0)  A  brief  overview  

Page 13: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

JMS:  A  brief  overview  I  • Broker  • Message  • Queue  – Producers  – Consumers  

•  Topic  – Publishers    – Subscribers  

• Accessible  from  inside  or  outside  a  Java  EE  

Page 14: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

JMS  2.0:  A  brief  overview:  • MulNple  Consumers  Allowed  on  the  Same  Topic  SubscripNon  •  Delivery  Delay  •  Sending  Messages  Asynchronously  – Send  the  message  and  get  callback  when  it  is  acknowledged  by  broker  

•  JMSXDeliveryCount  message  property  No  longer  opNonal    •  Standard  MDB  ConfiguraNon  ProperNes  as  part  of  @MessageDriven  – desNnaNonType  –  subscripNonDurability  – acknowledgeMode  –  subscripNonName  –  ...  

Page 15: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.0  Asynchronous  servlet    

Page 16: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.0  Asynchronicity    • Why  we  want  it  – More  throughput  – Be]er  architecture  mapping  

• How  it  works  – @WebServlet.asyncSupported,  async-­‐supported  in  XML  config!  – AsyncContext  – AsyncListener  

Page 17: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.0  Asynchronicity  :  AsyncContext  • Adding  listeners  • Doing  request  dispatching  • Accessing  ServletRequest  • Accessing  ServletResponse  • Concluding  the  request/response  •  Semng,  gemng  Nmeouts  

Page 18: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.0  Asynchronicity  :  AsyncListener  • Get  callback  on  important  events  on  an  async  request  processing  • onComplete(AsyncEvent  asyncEvent)  • onError(AsyncEvent  asyncEvent)  • onStartAsync(AsyncEvent  asyncEvent)  • onTimeout(AsyncEvent  asyncEvent)  

Page 19: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.0  Asynchronicity  :  A  li]le  bit  of  code  @WebServlet(asyncSupported  =  true,  value  =  ”/query-­‐servlet")  public  class  QueryServlet  extends  H]pServlet  {  protected  void  doGet(H]pServletRequest  request,  H]pServletResponse  response)  throws  ServletExcepNon,  IOExcepNon  {          String  id=...;          cache.put(id,request.startAsync());          sendQueryMessage(id,  request);      }  }  

Page 20: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.1  Non-­‐blocking  IO  

Page 21: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.1:  Non-­‐Blocking  IO  • Why  do  we  want  it?  • How  does  it  work?  – ReadListener:  To  read  inbound  data  when  available  – WriteListener:  To  write  data  when  possible  – Changes  in  ServletOutputStream    •  isReady()  •  setWriteListener(…)  

– Changes  in  ServletInputStream    •  isFinished()  •  isReady()  •  setReadListener(…)  

Page 22: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.1:  Non-­‐Blocking  IO:  ReadListener  •  To  get  callbacks  on  ServletInputStream  events  – onDataAvailable  – OnAllDataRead  – onError  

Page 23: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Servlet  3.1:  Non-­‐Blocking  IO:  WriteListener  •  To  get  noNfied  on  ServletOutputStream  events  – onError  – onWritePossible  

Page 24: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asyncronicity  in  JAX-­‐RS  2.0  • Why  would  we  need  it?  • How  does  it  work?  – @Asynchronous  – ExecuNonContext  for  programmaNc  decision  to  do  or  not  to  do  async  – AsyncResponse  – @Suspended  – CompleNonCallback  – ConnecNonCallbck  

Page 25: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asyncronicity  in  JAX-­‐RS  2.0:  Server-­‐side  I  

• On  the  serverside:  – @Asynchronous:  Annotate  a  sub-­‐resource  as  Asynchronous  – AsyncResponse:  Provides  results  an  acNons  on  the  running  request  •  semng  Nmeout  •  registering  callbacks  •  resume,  cancel  suspended  request  processing  •  updaNng  the  response    

– @Suspended:  To  inject  a  suspended  AsyncResponse  into  a  sub-­‐resource  parameter  

How  to  mark  a  resource  as  Asynchronous  

Page 26: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asyncronicity  in  JAX-­‐RS  2.0:  Server-­‐side  simple  code  @Path("/api/query")  

public  class  MyResource  {        

@Context  private  ExecuUonContext  ctx;  

@GET  @Produce(“applicaUon/json”)  

@Asynchronous  

@Path(“api/matching-­‐query”)  

public  void  prepMatchingQueryResult(@Suspended  AsyncResponse  ar,  @QueryParam  String  p1…  )  {  

       executor.submit(  new  Runnable()  {  

                 public  void  run()  {    

                       JsonObject  response  =  getQueryResult(p1…);          

                           ctx.resume(response);    //container  thread  picks  up  and  conUnue  

                   }  });      

ctx.suspend();    //  Suspend  connecUon  and  return  

   }  …  }    

Server  Code:  

Page 27: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asyncronicity  in  JAX-­‐RS  2.0:  client-­‐side  simple  code  

Future<JsonObject>  future  =  client.  

target(“/api/query/matching-­‐query”).queryParam(...).request().async().get(JsonObject.class);  

     try  {  

                 JsonObject  queryResult  =  future.get(30,  TimeUnit.SECONDS);  

             }  catch  (TimeoutExcepNon  ex)  {  

             //  

         }  

Client-­‐Side  with  future  

Page 28: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asyncronicity  in  JAX-­‐RS  2.0:  client-­‐side  simple  code  Client-­‐Side  with  callback  Future<JsonObject>  future  =  client.  target(“/api/query/matching-­‐query”).  

queryParam(...).request().async().get(new  InvocaNonCallback<JsonObject>()  {  

                       @Override  public  void  completed(JsonObject  response)  {  

   //InvocaNon  happens  and  some  response  is  back  (404,  200,  etc.)  

 }  

                       @Override  public  void  failed(Throwable  throwable)  {  

   //InvocaNon  fails  (client  side)  

 }  

               });  

Page 29: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asyncronicity  in  JAX-­‐RS  2.0:  Server-­‐side  II  

•  Single  method  interface  •  void  onComplete(Throwable  t)  •  signal  compleNon  of  serving  a  request  

CompleUonCallback  

Page 30: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asyncronicity  in  JAX-­‐RS  2.0:  Server-­‐side  III  

•  Single  method  interface  •  void  onDisconnect(AsyncResponse  disconnected)  •  signals  interrupNon  in  client  connecNon  before  compleNon  

ConnecUonCallback  

Page 31: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asynchronous  And  long  running  jobs  in  REST    

• MulN-­‐step  long  running  jobs  not  suitable  with  JAX-­‐RS  Async  •  Send  202  where  response  is  not  ready  with  LocaNon  header  – Intelligent  enough  client  can  query  the  resource  LocaUon  with  the  given  Retry-­‐Ader  header  

Don’t  keep  unnecessary  resources  for  where  not  needed!  

Page 32: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

EJB  3.1  Asynchronicity  in  Session  Beans  

Page 33: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asynchronicity  in  EJB  3.1    • Works  on  session  beans  – fire  and  forget  – Using  Future<V>  to  decide  on  compleNon  

• AsyncResult  as  container  specific  vehicle  – Passes  the  result  to  Future<V>  

• As  simple  as  using  @Asynchronous  on  method/bean  • Method  should  return  Future<V>  • Client  can  poll  the  future  for  CompleNon  

Page 34: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asynchronicity  in  EJB  3.1:  Simple  code  sample  

@Stateless  public  class  QueryProcessor  {          @Asynchronous          public  Future<QueryResult>  processQuery(QueryCrit  crit){          try{  

         QueryResult  result=  prepQueryResult(crit);              return  new  AsyncResult(result);  

         }catch(ExcepNon  e){                      //handle  

                 return  new  AsyncResult(FAILURE_RESULT);                              }  }}    

Server-­‐side  code  

Page 35: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Asynchronicity  in  EJB  3.1:  Simple  code  sample  

 @Inject    QueryProcessor    queryProcessor;    private  JsonObject  prepareQueryResult(String...  params){    QueryCrit  crit  =  new  QueryCrit(params)            Future<QueryResult>  result=queryProcessor.prepQueryResult(crit);            //poll  the  Future..  There  is  no  callback  here...              }  }  

cliet-­‐side  code  

Page 36: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Java  EE  7  and  SSE  Asynchronicity  in  SSE  

Page 37: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Server-­‐Sent  Events  §  HTML  5  component  

§  Client  subscribe  to  event  source    

§  UnidirecNonal  channel  between  server  and  client  

§  Events  can  be  streamed  from  server  to  client  when  happens  

§  ConnecNon  stays  open  

§  event  handling  on  client  side  §  onMessage  

§  onError  

§  etc.  

§  SubscripNon  resuming    

Page 38: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Server-­‐Sent  Events  § Can  be  developed  using  a  plain  Servlet  

§  use  the  right  media  type  

§  use  the  correct  message  format  

§  Jersey  provides  support  not  JAX-­‐RS  yet  §  Server  side  to  turn  a  JAX-­‐RS  endpoint  to  SSE  broadcaster  

§  Client  side  to  subscribe  and  consume  SS  events  

Page 39: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Comments,  QuesNons?  

Page 40: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

•  RESTful  Services  Patterns  and  best  practices  By  Bhakti  Mehta  •  Bhakti’s  blog:  https://www.java.net/blog/bhaktimehta  •  Book’s  sample  codes:  •  CCL  photos  used  in  slides:  

•  https://www.flickr.com/photos/treehouse1977/2892417805/  •  https://www.flickr.com/photos/treehouse1977/2892417805/  •  https://www.flickr.com/photos/essjay/165928100/  •  https://www.flickr.com/photos/jforth/4413370462/  •  https://www.flickr.com/photos/sakalak/8737872379/  •  https://www.flickr.com/photos/jbparrott/8980026600  •  https://www.flickr.com/photos/pentadact/36593493/  •  https://www.flickr.com/photos/jasohill/4442279347/  •  https://www.flickr.com/photos/mdsharpe/5075953655  •  https://www.flickr.com/photos/chuqvr/8329512894/  

Resources  

Page 41: CON 2107- Think Async: Embrace and Get Addicted to the Asynchronicity of EE