Successfully reported this slideshow.

Asynchronous Processing in Java/JEE/Spring


Published on

Asynchronous Processing in Java/JEE/Spring
Async Servlets, Async EJBs, Async Spring Beans, Async Web Service Clients, Async Logging.

Published in: Technology, Design

Asynchronous Processing in Java/JEE/Spring

  1. 1. Asynchronous Processing Java/JEE/Spring Naresh Chintalcheru
  2. 2. Asynchronous Processing What is Asynchronous Processing ? A process that executes independently of another process. When a request is processed asynchronously, the client application continues to perform other operations while it waits for the service request to be filled.
  3. 3. Synchronous Processing 4 Client Browser 1 2 3 5 Server Side Components [1]100ms + [2]100ms + [3]100ms + [4]100ms + [5]100ms Client Response time = 500ms
  4. 4. Asynchronous Processing 4 Client Browser 1 2 3 5 Server Side Components Client Response Time [1] = 100ms Asynchronous Background Processing Time = 400ms [2]100ms + [3]100ms + [4]100ms + [5]100ms
  5. 5. Asynchronous Processing The Asynchronous Processing is possible if the given programming language, runtime and frameworks supports Non-Blocking I/O
  6. 6. Non-Blocking I/O What is Non-Blocking I/O ? Non-Blocking I/O is a form of input/output processing that permits other processing to continue before the transmission has finished.
  7. 7. Java/JEE Java 1.4 started the support for Non-Blocking I/O JEE 6 with partial support with full support in JEE 7
  8. 8. Asynchronous EJB 3.0 Web Service Client Browser Servlet EJB DAO Database Server Side @Asynchronous //All methods async public class AccountBean { @Asynchronous //This method async public void updateAccount() { } }
  9. 9. Asynchronous Servlets 3.0 Web Service Client Browser Servlet EJB DAO Database Server Side @WebServlet(urlPatterns="/MyAsyncServlet", asyncSupported=true) public class MyAsyncServlet extends HttpServlet { protected void doGet(Httpreq, HttpServletRes) {} }
  10. 10. Async Beans in Spring Framework Web Service Client Browser Servlet Bean DAO Database Server Side @Component public class AccountBean { @Async public void createAccount() { System.out.println("NEW THREAD " + new Date() + " NEW THREAD"); } }
  11. 11. Asynchronous JavaBean/DAO Synchronus JDBC Database Calls User user = userDAO.getByID(userID); Invoice invoice = invoiceDAO.getByUserID(userID); PaymentHistory paymentHistory = paymentDAO.getHistoryByuserID(userID); List<Order> orders = orderDAO.getOpenOrdersByUserID(userID);
  12. 12. Asynchronous JavaBean/DAO Java.util.concurrent APIs Future Object : A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation
  13. 13. Asynchronous JavaBean/DAO Asynchronous JDBC Database Calls Future<User> futureUser = userDAO.getByID(userID); Future<Invoice> futureInvoice = invoiceDAO.getByUserID(userID); Future<PaymentHistory> futurePaymentHistory = paymentDAO.getByuserID(userID); Future<List<Order>> futureOrders = orderDAO.getOpenOrdersByUserID(userID); Future<user> futureUser = userDAO.getUserById(userId); User user = futureUser.get(); //block happens here user.setCount(user.getCount() + 1);
  14. 14. Asynchronous Logging No one wants Logging framework to be bottleneck Log4J’s new AsyncLoggers can improve performance Log4J AsyncLogger is Insanly Fast
  15. 15. Asynchronous Web Services Invoking a SOAP Web Service Using Asynchronous RequestResponse Creating the Asynchronous JAX-WS 2.0 Client Uses the concurrent Future Object
  16. 16. Asynchronous REST Service Invoking a RESTFul Services Asynchronously Creating the Asynchronous JAX-RS 2.0 Client The JAX-RS 2.0 client framework supports an asynchronous API and a callback API
  17. 17. Async JDBC Drivers PostgreSQL Extensions to the JDBC API Listen / Notify Listen and Notify provide a simple form of signal or interprocess communication mechanism for a collection of processes accessing the same PostgreSQL™ database. For more information on notifications consult the main server documentation. This section only deals with the JDBC specific aspects of notifications.
  18. 18. Asynchronous Processing Issues x 3 Client Browser 1 1 2 4 Server Side What happens if server side failure occurs ?
  19. 19. Event-Driven Architecture Asynchronous + Event-Driven Architecture can solve the server side failures. Look at my EDA Presentation on “Lie Cheat Steal to build Hyper-Fast Applications using Event-Driven Architecture”
  20. 20. Thank You Thank You