Spring @Async. Dragan Juričić, PBZ May 2015



Similar documents
Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd.

Threads & Tasks: Executor Framework

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Design Patterns in C++

Bayeux Protocol: la nuova frontiera della comunicazione a portata di mano. Relatore Nino Guarnacci

Module 13 Implementing Java EE Web Services with JAX-WS

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

Connecting Custom Services to the YAWL Engine. Beta 7 Release

What s new in Spring 3.1?

ActiveVOS Server Architecture. March 2009

Enterprise Application Development In Java with AJAX and ORM

Java EE Web Development Course Program

ACM Crossroads Student Magazine The ACM's First Electronic Publication

CS506 Web Design and Development Solved Online Quiz No. 01

Complete Java Web Development

JMS 2.0: Support for Multi-tenancy

1. Spring Batch Admin User Guide

Performance Monitoring API for Java Enterprise Applications

CA Data Protection. Content Provider Development Guide. Release 15.0

SPRING INTERVIEW QUESTIONS

Model-View-Controller. and. Struts 2

Spring 3.1 to 3.2 in a Nutshell. Sam Brannen Senior Software Consultant

Transactionality and Fault Handling in WebSphere Process Server Web Service Invocations. version Feb 2011

Clustering with Tomcat. Introduction. O'Reilly Network: Clustering with Tomcat. by Shyam Kumar Doddavula 07/17/2002

Solutions for detect, diagnose and resolve performance problems in J2EE applications

White Paper March 1, Integrating AR System with Single Sign-On (SSO) authentication systems

Liferay Enterprise ecommerce. Adding ecommerce functionality to Liferay Reading Time: 10 minutes

JAX-WS Developer's Guide

Web Frameworks and WebWork

Introduction to Oracle WebLogic. Presented by: Fatna Belqasmi, PhD, Researcher at Ericsson

Titolo del paragrafo. Titolo del documento - Sottotitolo documento The Benefits of Pushing Real-Time Market Data via a Web Infrastructure

Real Time Data in Web Applications

Oracle Identity Analytics Architecture. An Oracle White Paper July 2010

Java Servlet 3.0. Rajiv Mordani Spec Lead

Spring Security SAML module

WebSphere Server Administration Course

Let s Chat! (Part 1)

IBM WebSphere Server Administration

Japan Communication India Skill Development Center

JBoss Seam Performance and Scalability on Dell PowerEdge 1855 Blade Servers

Spring Data Cassandra - Reference Documentation

WASv6_Scheduler.ppt Page 1 of 18

Web Applications and Struts 2

Building Web Services with XML Service Utility Library (XSUL)

Web Development in Java

GUI and Web Programming

Capacity Planning Guide for Adobe LiveCycle Data Services 2.6

WEB SERVICES. Revised 9/29/2015

Ehcache Web Cache User Guide. Version 2.9

StreamServe Persuasion SP4 Service Broker

An introduction to creating JSF applications in Rational Application Developer Version 8.0

DTS Web Developers Guide

A Comparative Study on Vega-HTTP & Popular Open-source Web-servers

Course Name: Course in JSP Course Code: P5

<Insert Picture Here> Hudson Security Architecture. Winston Prakash. Click to edit Master subtitle style

BPM Scheduling with Job Scheduler

Web Services with ASP.NET. Asst. Prof. Dr. Kanda Saikaew Department of Computer Engineering Khon Kaen University

Performance Optimization For Operational Risk Management Application On Azure Platform

Weaving Stored Procedures into Java at Zalando

CERTIFIED MULESOFT DEVELOPER EXAM. Preparation Guide

Agenda. Tomcat Versions Troubleshooting management Tomcat Connectors HTTP Protocal and Performance Log Tuning JVM Tuning Load balancing Tomcat

Instant Chime for IBM Sametime Installation Guide for Apache Tomcat and Microsoft SQL

The Server.xml File. Containers APPENDIX A. The Server Container

ADF Code Corner. 92. Caching ADF Web Service results for in-memory filtering. Abstract: twitter.com/adfcodecorner

By Wick Gankanda Updated: August 8, 2012

JVA-561. Developing SOAP Web Services in Java

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

Framework Adoption for Java Enterprise Application Development

COM 440 Distributed Systems Project List Summary

Liferay Performance Tuning

Distributed Objects and Components

Università degli Studi di Napoli Federico II. Corsi di Laurea Specialistica in Ingegneria Informatica ed Ingegneria delle Telecomunicazioni

Spring Security 3. rpafktl Pen source. intruders with this easy to follow practical guide. Secure your web applications against malicious

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Developing Java Web Services

IBM WebSphere Message Broker Message Monitoring, Auditing, Record and Replay. Tim Kimber WebSphere Message Broker Development IBM Hursley Park, UK

This presentation introduces you to the Decision Governance Framework that is new in IBM Operational Decision Manager version 8.5 Decision Center.

Hazelcast Documentation. version 3.6-EA2

C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

Oracle Hyperion Financial Management Custom Pages Development Guide

ITS. Java WebService. ITS Data-Solutions Pvt Ltd BENEFITS OF ATTENDANCE:

Core Java+ J2EE+Struts+Hibernate+Spring

3 Techniques for Database Scalability with Hibernate. Geert Bevin - SpringOne 2009

Course Description. Course Audience. Course Outline. Course Page - Page 1 of 5

Brekeke PBX Web Service

GTask Developing asynchronous applications for multi-core efficiency

Web Performance, Inc. Testing Services Sample Performance Analysis

Ch-03 Web Applications

ebay : How is it a hit

Oracle WebLogic Server 11g Administration

StreamServe Persuasion SP5 StreamStudio

Transcription:

Spring @Async Dragan Juričić, PBZ May 2015

Topics Concept of thread pools Servlet 3 async configuration Task Execution and Scheduling Servlet 3 - asynchronous request processing Benefits and downsides 2

Concept of thread pools 3

Concept of thread pools thread per request server model (Tomcat, Jetty, WAS...) simplistic model - create a new thread for each task disadvantages of the thread-per-task approach: overhead of creating creating and destroying threads too many threads cause the system to run out of memory thread pools based on work queue offers a solution Spring TaskExecutor - abstraction for thread pooling 4

TaskExecutor types pre-built implementations included with the Spring SimpleAsyncTaskExecutor - starts up a new thread for each invocation, support a concurrency limit SyncTaskExecutor - implementation doesn't execute invocations asynchronously, takes place in the calling thread ConcurrentTaskExecutor - wrapper for a Java 5 java.util.concurrent.executor ThreadPoolTaskExecutor - exposes the Executor configuration parameters as bean properties WorkManagerTaskExecutor - implements the CommonJ WorkManager interface - standard across IBM's 5

Servlet 3 async configuration 6

Servlet 3 async configuration Spring web application configuration: XML config - update web.xml to version 3.0 JavaConfig - via WebApplicationInitializer interface DispatcherServlet need to have: asyncsupported flag Filter involved in async dispatches: asyncsupported flag ASYNC dispatcher type 7

Spring MVC async configuration WebMvcConfigurationSupport the main class providing the configuration behind the MVC JavaConfig: the default timeout value for async requests TaskAsyncExecutor (default is SimpleAsyncTaskExecutor) protected void configureasyncsupport(asyncsupportconfigurer configurer) { configurer.setdefaulttimeout(30*1000l); } } configurer.settaskexecutor(mvctaskexecutor()); protected ThreadPoolTaskExecutor mvctaskexecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setcorepoolsize(10); executor.setqueuecapacity(100); executor.setmaxpoolsize(25); return executor; 8

Task Execution and Scheduling 9

Asynchronous invocation in Spring 3.0 @Async annotation - executing tasks asynchronously (annotation on a method) the caller will return immediately and the actual execution of the method will occur in a task submitted to TaskExecutor methods are required to have a Future<T> return value @Async Future<Task> returnsomething(int i) { } // this will be executed asynchronously return new AsyncResult<Task>(results); Spring wraps call to this method in a Runnable instance and schedule this Runnable on a task executor 10

Async method return value Future<T> is a proxy or a wrapper around an object - container that holds the potential result asynchronous task done - extract result Future<T> methods: get() - blocks and waits until promised result is available isdone() - poll if the result has arrived cancel() - attempts to cancel execution of this task iscanceled() - returns true if this task was cancelled before it completed normally. Concrete implementation AsyncResult - wrap result in AsyncResult implementing Future<T> interface 11

Exceptions with @Async Exception that was thrown during the method execution @Async method has a Future typed return value - exception will be thrown when calling get() method on the Future result @Async method has void return type - the exception is uncaught and cannot be transmitted void return type - AsyncUncaughtExceptionHandler can be provided to handle such exceptions 12

The @Scheduled Annotation TaskScheduler abstraction for scheduling tasks: TimerManagerTaskScheduler - delegates to a CommonJ TimerManager instance ThreadPoolTaskScheduler external thread management is not a requirement (implements Spring s TaskExecutor) @Scheduled annotation add to a method along with trigger metadata @Scheduled(fixedDelay=5000) public void dosomething() { } // something that should execute periodically @Scheduled(cron="* 15 9-17 * * MON-FRI") public void dosomething() { } // something that should execute on weekdays only 13

Servlet 3 - asynchronous request processing 14

Asynchronous request handling Spring 3.2 introduced Servlet 3 based asynchronous request processing controller method can now return Callable or DeferredResult instance Servlet container thread is released and allowed to process other request: Callable uses TaskExecutor thread DeferredResult uses thread not known to Spring Asynchronous request processing: Controller returns and Spring MVC starts async processing Servlet and all filters exit the request thread, but response remains open Other thread will complete processing and dispetch request back to Servlet Servlet is invocked again and processing resumes with async result 15

Callable an example controller method @RequestMapping(value = {"callable.html"}, method = RequestMethod.GET) public Callable<String> callableview(final ModelMap p_model) { return new Callable<String>() { @Override public String call() throws Exception { //... processing return someview"; } }; } WebAsyncTask wrap Callable for customization: timeout TaskExecutor 16

DeferredResult an example controller method @RequestMapping("/response-body") @ResponseBody public DeferredResult<String> quotes() { DeferredResult<String> deferredresult = new DeferredResult<String>(); // Save the deferredresult in in-memory queue... } return deferredresult; // In some other thread... deferredresult.setresult(data); 17

Exception handling for async requests What happens if a Callable or DeferredResult returned from a controller method raises an Exception? Callable @ExeceptionHandler method in the same controller one of the configured HandlerExceptionResolver instances DeferredResult calling seterrorresult() method and provide an Exception or any other Object as result @ExeceptionHandler method in the same controller one of the configured HandlerExceptionResolver instances 18

Benefits and downsides 19

Benefits @Async method: asynchronous method calls solves a critical scaling issue the longer the task takes and the more tasks are invoked - the more benefit with making things asynchronous Async request: decouple processing from Servlet container thread - longer request can exhaust container thread pool quickly processing of AJAX applications efficiently browser real-time update server push (alternative to standard HTTP request-response approaches: polling, long polling, HTTP streaming) Servlet 3 specification: asynchronous support JavaConfig without need for web.xml and enhancements to servlet API 20

Downsides threading risks additional configuration (servlet, filter, thread pool...) asynchronous method calls adds a layer of indirection - no longer dealing directly with the results, but must instead poll for them converting request or method calls to an asynchronous approach may require extra work 21

References http://spring.io/ http://oracle.com/ http://docs.spring.io/spring/docs/current/spring-frameworkreference/html/scheduling.html http://www.slideshare.net/bruce.snyder/beyond-horizontal-scalability-concurrencyand-messaging-using-spring http://www.slideshare.net/chintal75/asynchronous-programmingtechniques http://www.ibm.com/developerworks/library/j-jtp0730.html 22