Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Think async

1,984 views

Published on

With the growing challenges we face with shortage of resources there is a constant need to improve scalability and performance. Fortunately Java EE 7 and Java SE 7 has provided a rich set of APIs which can be used to avoid the conventional request-response paradigm. The session will span across JSRs cover how JAX-RS, Servlets, WebSockets, EJB provide support to submit asynchronous requests and cover topics like callbacks, timeouts, client side asynchrony, API like non blocking IO and lots of good stuff from Java EE 7 and Java SE 7 . Prerequisite: An open mind and be ready to “Think async”

Published in: Technology
  • Be the first to comment

Think async

  1. 1. Think Async Bhakti Mehta Twitter: @bhakti_mehta Copyright 2013, Bhakti Mehta all rights reserved
  2. 2. Introduction • Currently working as a Senior Software Engineer at Blue Jeans Network. Bluejeans.com • Worked at Sun Microsystems/Oracle for 13 years • Committer to numerous open source projects including GlassFish Copyright 2013, Bhakti Mehta all rights reserved
  3. 3. My upcoming book Copyright 2013, Bhakti Mehta all rights reserved
  4. 4. What you will learn • Start thinking asynchronously • Cover different technologies with respect to async support • Cover concepts from J2SE and J2EE with respect to asynchronous processing Copyright 2013, Bhakti Mehta all rights reserved
  5. 5. Why Think Async? • Increase speed of processing through parallelization • Involve multiple CPU cores for processing than single fast CPU • Compare with older request response models and see the benefits Copyright 2013, Bhakti Mehta all rights reserved
  6. 6. Where async can be used? • Long running operations • Processor intensive tasks • Background tasks • To improve application throughput • To improve application response time Copyright 2013, Bhakti Mehta all rights reserved
  7. 7. Usecases • User John Doe wants to download multiple files from different locations. Solution • Execute requests asynchronously and wait for results from Future object Copyright 2013, Bhakti Mehta all rights reserved
  8. 8. Usecases • Use John Doe has a large data to post to a URL Solution Use callbacks instead of waiting and blocking for response Copyright 2013, Bhakti Mehta all rights reserved
  9. 9. Usecases • Use John Doe want to upload a large file Solution Use AsynchronousFileChannel from JDK 7 Copyright 2013, Bhakti Mehta all rights reserved
  10. 10. Polling • Polling  Used by vast majority of AJAX applications  Poll the server for data  Client --->request--> Server  If no data empty response is returned  Not a true asynchronous mechanism Copyright 2013, Bhakti Mehta all rights reserved
  11. 11. Polling Copyright 2013, Bhakti Mehta all rights reserved Request (client_id) Response 200 OK: empty Request (client_id) …. …. …. …. Response 200 OK: message body
  12. 12. Polling Drawbacks  Http overhead  Reducing the interval will consume more bandwidth and processing resources for nothing. Copyright 2013, Bhakti Mehta all rights reserved
  13. 13. Long Polling  If server does not have data holds request open  COMET  Chunked transfer encoding can be used to send chunks of data as part of response body which is opened as a stream.  Chunks can be javascript tags loaded in hidden iframe and executed in order of arrival Copyright 2013, Bhakti Mehta all rights reserved
  14. 14. Long Polling Copyright 2013, Bhakti Mehta all rights reserved Request (client_id) Response 200 OK: message body Request (client_id) …. …. …. Response 200 OK: message body …. ….
  15. 15. Long Polling Drawbacks • No standard data format or message format when used in chunked transfer encoding mode • Each connection initiation has the initiation cost. • No caching between clients and server, which impacts the server performance instead of reading some contents from the cache. Copyright 2013, Bhakti Mehta all rights reserved
  16. 16. Server-sent Events  Unidirectional channel between server and client  Server pushes data to your app when it wants asynchronously  Updates can be streamed from server to client as they happen Copyright 2013, Bhakti Mehta all rights reserved
  17. 17. Server-sent Events Copyright 2013, Bhakti Mehta all rights reserved Request (client_id) …. …. …. SSE message …. …. SSE message
  18. 18. WebSockets  Next generation of asynchronous communication from client to server  Full duplex communication in either direction  After initial handshake on http communication is over TCP socket using ws(unsecure) or wss(secure) protocol Copyright 2013, Bhakti Mehta all rights reserved
  19. 19. WebSocket Handshake Client and Server upgrade from Http protocol to WebSocket protocol during initial handshake GET /text HTTP/1.1rn Upgrade: WebSocketrn Connection: Upgradern Host: www.websocket.orgrn …rn Handshake from server looks like HTTP/1.1 101 WebSocket Protocol Handshakern Upgrade: WebSocketrn Connection: Upgradern …rn Copyright 2013, Bhakti Mehta all rights reserved
  20. 20. WebSockets  After the upgrade HTTP is completely out of the picture at this point.  Using the lightweight WebSocket wire protocol, messages can now be sent or received by either endpoint at any time.  Creating a Websocket  ws = new WebSocket("ws://localhost:8080/../WebSocketChat");  You can set handlers for events onopen or onerror Copyright 2013, Bhakti Mehta all rights reserved
  21. 21. Other JavaEE technologies Async support • Servlet 3.0 • JAX-RS • EJB Copyright 2013, Bhakti Mehta all rights reserved
  22. 22. Components ofAsyncServlet • New attribute asyncSupported on @WebServlet annotation or in web.xml • AsyncContext • Thread Pool • Runnable instance Copyright 2013, Bhakti Mehta all rights reserved
  23. 23. AsyncServlet • When asyncSupported attribute is set to true response is not committed on method exit • Calling startAsync() method of AsyncContext caches the request response pair • The method returns and original thread is recycled • AsyncListener and AsyncEvent give developers control of the asynchronous lifecycle events Copyright 2013, Bhakti Mehta all rights reserved
  24. 24. AsyncServlet final AsyncContext asyncContext = request.startAsync(); asyncContext.setTimeout(600000); asyncContext.addListener(new AsyncListener() Implement the following method of the AsyncListener onComplete(), onError(),OnTimeout(), onstartAsync() Copyright 2013, Bhakti Mehta all rights reserved
  25. 25. Servlet 3.1:Non blocking IO • ReadListener • WriteListener • These are then registered using ServletInputStream.setReadListener and ServletOutputStream.setWriteListener. The listeners have callback methods that are invoked when the content is available to be read or can be written without blocking. Copyright 2013, Bhakti Mehta all rights reserved
  26. 26. Servlet 3.1: Non blocking IO public interface ReadListener extends EventListener { public void onDataAvailable(ServletRequest request); public void onAllDataRead(ServletRequest request); public void onError(Throwable t); } Copyright 2013, Bhakti Mehta all rights reserved
  27. 27. Servlet 3.0: Non blocking IO • The onDataAvailable: Invoked when all data for the current request has been read. • The onAllDataRead: Method is invoked by the container the first time when it is possible to read data. • The onError: Invoked when an error occurs processing the request Copyright 2013, Bhakti Mehta all rights reserved
  28. 28. Servlet 3.0: Non blocking IO public interface WriteListener extends EventListener { public void onWritePossible(ServletResponse response); public void onError(Throwable t); } Copyright 2013, Bhakti Mehta all rights reserved
  29. 29. Servlet 3.0: Non blocking IO • The container invokes the onWritePossible method when it is possible to write in the Servlet’s OutputStream. • The onError is invoked when writing in the Servlet’s OutputStream encounters an exception. Copyright 2013, Bhakti Mehta all rights reserved
  30. 30. ServletOutputStream • In the ServletOutputStream interface • isReady: This method can be used to determine if data can be written without blocking. • setWriteListener: Instructs the ServletOutputStream to invoke the provided WriteListener when it is possible to write Copyright 2013, Bhakti Mehta all rights reserved
  31. 31. ServletInputStream • In the ServletInputStream interface • isFinished: Returns true when all the data from the stream has been read else it returns false. • isReady: Returns true if data can be read without blocking else returns false. • setReadListener: Instructs the ServletInputStream to invoke the provided ReadListener when it is possible to read Copyright 2013, Bhakti Mehta all rights reserved
  32. 32. JAX-RS 2.0 Async support • Server Side • Client Side Copyright 2013, Bhakti Mehta all rights reserved
  33. 33. JAX-RS 2.0 Server sideAsync support • AsyncResponse: An injectable JAX-RS asynchronous response that provides means for asynchronous server side response processing. • @Suspended: The @Suspended instructs the container that the HTTP request processing should happen in a secondary thread. • CompletionCallback: A request processing callback that receives request processing completion events. Has onComplete() callback • ConnectionCallback: Asynchronous request processing lifecycle callback that receives connection related asynchronous response lifecycle events. Copyright 2013, Bhakti Mehta all rights reserved
  34. 34. JAX-RS 2.0 Server sideAsync support • InvocationCallback: Callback that can be implemented to receive the asynchronous processing events from the invocation processing. • Future: Allows the client to poll for completion of the asynchronous operation or to block and wait for it. Copyright 2013, Bhakti Mehta all rights reserved
  35. 35. @Suspended @Path(“/books/borrow”) @Stateless public class BookResource { @GET @Produce(“text/plain”) @Asynchronous public void borrow(@Suspended AsyncResponse ar) { final String result = prepareResponse(); ar.resume(result) } } Copyright 2013, Bhakti Mehta all rights reserved
  36. 36. Client side code Future<Book> future = client.target("(“books/borrow/borrow") .request() .async() .get(Book.class); try { Book book = future.get(30, TimeUnit.SECONDS); } catch (TimeoutException ex) { System.err.println("Timeout occurred"); } Copyright 2013, Bhakti Mehta all rights reserved
  37. 37. InvocationCallback sample client.target(restUrl) .request(MediaType.APPLICATION_JSON) .async() .post(Entity.entity("Here is some text", MediaType.TEXT_PLAIN), new InvocationCallback<Response>() { @Override public void completed(Response response) { processResponse(response.readEntity(JSONObject.class)); } Copyright 2013, Bhakti Mehta all rights reserved
  38. 38. EJB 3.1 • @Asynchronous annotation introduced in EJB 3.1 • Methods annotated with @Asynchronous will return immediately regardless of how long the method usually takes. • Each invocation returns a Future object which is initially empty and will have its value filled later when method completes Copyright 2013, Bhakti Mehta all rights reserved
  39. 39. AsyncResult • AsyncResult<V> wraps the result of an asynchronous method call as a Future object • The value specified in the constructor AsyncResult(V result) will be retrieved by Container and sent to client. Copyright 2013, Bhakti Mehta all rights reserved
  40. 40. Cancelling an async invocation • The session bean can check whether the client requested that the invocation be cancelled by calling the javax.ejb.SessionContext.wasCancelled(). Copyright 2013, Bhakti Mehta all rights reserved
  41. 41. @Asynchronous code snippet@Path(“/books/borrow”) @Stateless public class BookResource { @Context private ExecutionContext ctx; @GET @Produce(“application/json”) @Asynchronous public void borrow() { Executors.newSingleThreadExecutor().submit( new Runnable() { public void run() { Thread.sleep(10000); ctx.resume(“Hello async world!”); } }); ctx.suspend(); return; }} Copyright 2013, Bhakti Mehta all rights reserved
  42. 42. Java SE and Think Async • Future • A Future object represents the result of asynchronous completion. • Methods to check if computation is complete, wait for result and retrieve results. • Cancellation can be performed by cancel method Copyright 2013, Bhakti Mehta all rights reserved
  43. 43. Asynchronous IO • AsynchronousFileChannel • Reads and writes contents from a file asynchronously Copyright 2013, Bhakti Mehta all rights reserved
  44. 44. AsynchronousFileChannel try (AsynchronousFileChannel asyncChannel = AsynchronousFileChannel.open(path)){ //Returns a Future instance which can be used to read the contents of the file. Future<Integer> fileResult = asyncChannel.read(buffer, 0); while(!fileResult.isDone()){ System.out.println("Waiting to complete the file reading ..."); } Copyright 2013, Bhakti Mehta all rights reserved
  45. 45. Call for action • Download GlassFish 4.0 from glassfish.dev.java.net for latest JavaEE7 API • File issues, contribute patches • Email users@glassfish.dev.java.net Copyright 2013, Bhakti Mehta all rights reserved
  46. 46. Questions • Twitter: @bhakti_mehta • Blog https://www.java.net//blogs/bhaktimehta • LinkedIn: http://www.linkedin.com/in/bhaktihmehta • Slideshttp://www.slideshare.net/bhaktiks/think-async Copyright 2013, Bhakti Mehta all rights reserved

×