Think Async Embrace and Get Addicted to the
Think Async Embrace and Get Addicted to the Asynchronicity of Java SE and EE Masoud Kalali, Software Engineer, ORACLE, @Masoud. Kalal Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Program Agenda 1 Introduction 2 Why to think Async 3 A simple case of using Async 4 JMS (2. 0) 5 Async Servlet 6 Async JAX-RS 7 Async EJBs Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Speakers Masoud Kalali • Software engineer, author, blogger • Long advocate of Glass. Fish and Java EE • Tweets at @Masoud. Kalali Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Why to think Async? • To better model what we develop for • To further decouple – Ease of administration – Ease of tuning – Ease of maintenance • Improve consumers experience – An API consumer – A direct GUI for human use – etc. 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 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
A simple asynchronous nourished use case Client 8…. 6…. JMS Queue MDB Cache ID, Async. Context 1 4…. Query Requests in JMS 2 7…. 3 Query Processor JMS Queue 2. 1 Async Servlet 1 Consume result chunks and send it back via Async. Context Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 5…. Chunks of Query results
A simple asynchronous nourished use case More details of the Messaging components • 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 Async. Context – As long as the message does not say it is done it will not conclude the response • Complete the response when JMS message implies Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
A simple asynchronous nourished use case More details of the Servlet components • Asynchronous Servlet receiving the requests • Caching the Asyn. Context 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 Async. Listener to send proper response if timeouts Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
A simple asynchronous nourished use case Some basics on the client side • A browser • Can be – A SSE client – A COMET, Long Polling request Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
JMS (2. 0) A brief overview Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
JMS: A brief overview I • Broker • Message • Queue – Producers – Consumers • Topic – Publishers – Subscribers Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
JMS 2. 0: A brief overview: • Multiple Consumers Allowed on the Same Topic Subscription • Delivery Delay • Sending Messages Asynchronously – Send the message and get callback when it is acknowledged by broker • JMSXDelivery. Count message property No longer optional • Standard MDB Configuration Properties as part of @Message. Driven – destination. Type – subscription. Durability – acknowledge. Mode – subscription. Name –. . . Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 0 Asynchronous servlet Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 0 Asynchronicity • Why we want it – More throughput – Better architecture mapping • How it works – @Web. Servlet. async. Supported, async-supported in XML config! – Async. Context – Async. Listener Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 0 Asynchronicity : Async. Context • Adding listeners • Doing request dispatching • Accessing Servlet. Request • Accessing Servlet. Response • Concluding the request/response • Setting, getting timeouts Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 0 Asynchronicity : Async. Listener • Get callback on important events on an async request processing • on. Complete(Async. Event async. Event) • on. Error(Async. Event async. Event) • on. Start. Async(Async. Event async. Event) • on. Timeout(Async. Event async. Event) Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 0 Asynchronicity : A little bit of code @Web. Servlet(async. Supported = true, value = ”/query-servlet") public class Query. Servlet extends Http. Servlet { protected void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { String id=. . . ; cache. put(id, request. start. Async()); send. Query. Message(id, request); } } Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 1 Non-blocking IO 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? – Read. Listener: To read inbound data when available – Write. Listener: To write data when possible – Changes in Servlet. Output. Stream • is. Ready() • set. Write. Listener(…) – Changes in Servlet. Input. Stream • is. Finished() • is. Ready() • set. Read. Listener(…) Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 1: Non-Blocking IO: Read. Listener • To get callbacks on Servlet. Input. Stream events – on. Data. Available – On. All. Data. Read – on. Error Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Servlet 3. 1: Non-Blocking IO: Write. Listener • To get notified on Servlet. Output. Stream events – on. Error – on. Write. Possible 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 – Execution. Context for programmatic decision to do or not to do async – Async. Response – @Suspended – Completion. Callback – Connection. Callbck Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asyncronicity in JAX-RS 2. 0: Server-side I How to mark a resource as Asynchronous • On the serverside: – @Asynchronous: Annotate a sub-resource as Asynchronous – Async. Response: Provides results an actions on the running request • • setting timeout registering callbacks resume, cancel suspended request processing updating the response – @Suspended: To inject a suspended Async. Response into a sub-resource parameter Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asyncronicity in JAX-RS 2. 0: Server-side simple code Server Code: @Path("/api/query") public class My. Resource { @Context private Execution. Context ctx; @GET @Produce(“application/json”) @Asynchronous @Path(“api/matching-query”) public void prep. Matching. Query. Result(@Suspended Async. Response ar, @Query. Param String p 1… ) { executor. submit( new Runnable() { public void run() { Json. Object response = get. Query. Result(p 1…); ctx. resume(response); //container thread picks up and continue } }); ctx. suspend(); // Suspend connection and return }…} Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asyncronicity in JAX-RS 2. 0: client-side simple code Client-Side with future Future<Json. Object> future = client. target(“/api/query/matchingquery”). query. Param(. . . ). request(). async(). get(Json. Object. class); try { Json. Object query. Result = future. get(30, Time. Unit. SECONDS); } catch (Timeout. Exception ex) { // } 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<Json. Object> future = client. target(“/api/query/matching-query”). query. Param(. . . ). request(). async(). get(new Invocation. Callback<Json. Object>() { @Override public void completed(Json. Object response) { //Invocation happens and some response is back (404, 200, etc. ) } @Override public void failed(Throwable throwable) { //Invocation fails (client side) } }); Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asyncronicity in JAX-RS 2. 0: Server-side II Completion. Callback • Single method interface • void on. Complete(Throwable t) • signal completion of serving a request Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asyncronicity in JAX-RS 2. 0: Server-side III Connection. Callback • Single method interface • void on. Disconnect(Async. Response disconnected) • signals interruption in client connection before completion Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asynchronous And long running jobs in REST Don’t keep unnecessary resources for where not needed! • Multi-step long running jobs not suitable with JAX-RS Async • Send 202 where response is not ready with Location header – Intelligent enough client can query the resource Location with the given Retry-After header Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
EJB 3. 1 Asynchronicity in Session Beans 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 completion • Async. Result 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 Completion Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asynchronicity in EJB 3. 1: Simple code sample Server-side code @Stateless public class Query. Processor { @Asynchronous public Future<Query. Result> process. Query(Query. Crit crit){ try{ Query. Result result= prep. Query. Result(crit); return new Async. Result(result); }catch(Exception e){ //handle return new Async. Result(FAILURE_RESULT); } }} Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Asynchronicity in EJB 3. 1: Simple code sample cliet-side code @Inject Query. Processor query. Processor; private Json. Object prepare. Query. Result(String. . . params){ Query. Crit crit = new Query. Crit(params) Future<Query. Result> result=query. Processor. prep. Query. Result(crit); //poll the Future. . There is no callback here. . . } } Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Java EE 7 and SSE Asynchronicity in SSE Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Server-Sent Events § HTML 5 component § Client subscribe to event source § Unidirectional channel between server and client § Events can be streamed from server to client when happens § Connection stays open § event handling on client side § on. Message § on. Error § etc. § Subscription resuming 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 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Comments, Questions? Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Resources • • 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/treehouse 1977/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/ Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
- Slides: 41