3. Introduction
2
2003 2006 2008 2012 2013
JSR 236
released as part
of Java EE 7
JSR237 is
merged into
JSR236
JSR236 (Timers)
and JSR237 (Work
Managers) filed
CommonJ API
replaced with
extension of Java SE
concurrency API
JSR 236
restarted
JSR Timeline
4. Introduction
Goals
• Provide easy-to-use support to developers for both simple and
advanced concurrency patterns
• Provide a path to migrate standalone apps using Java SE
Concurrency Utilities (JSR-166) to the Java EE platform with a
consistent API
• Provide an easy-to-understand API for developers to create
applications using concurrency design principles
• Make it easy to add concurrency to existing Java EE
applications
3
5. Introduction
• Extension of the Java SE Concurrency Utilities API
• Addresses long standing tenant of EE app servers that well-
behaved applications should not create threads
• WebSphere (like other application servers) provided other
mechanisms for creating and managing threads, such as
Asynchronous Beans, CommonJ Timer and Work Manager.
• Asynchronous task lifecycle management, monitoring and
notification
• Flexible propagation of common Java EE contexts to tasks
4
Available 4Q2014 via Liberty Repository!
7. Services
Overview
• JSR 236 defines four services to be provided by an application
server
• ManagedExecutorService
• ManagedScheduledExecutorService
• ManagedThreadFactory
• ContextService
• Services are accessed by application via:
• resource injection using @Resource
• JNDI lookup
• default instances
• Configured through vendor-defined properties
6
8. Services
ManagedExecutorService overview
• Provides a familiar API for concurrent processing of tasks in
Java EE
• Extends java.util.concurrent.ExecutorService of
Java SE concurrency API
• Allow execution of asynchronous tasks on server managed
threads
• Tasks must implement either:
– java.lang.Runnable
– java.util.concurrent.Callable
7
9. Services
ManagedExecutorService API
• Supports Java EE context propagation including JNDI naming,
classloader, and security contexts
• Not transactional contexts
• Because the service is managed by the server, calling any of
the lifecycle API methods results in IllegalStateException
• Like Java SE, task(s) submission returns one or more
Future(s). These are used to:
• check for task execution status
• wait for and retrieve task result
• cancel task
8
10. Services
ManagedExecutorService Example
@WebServlet(asyncSupported=true)
public class MyServlet extends HttpServlet {
@Resource ManagedExecutorService managedExecutorService;
protected void doGet(HttpServletRequest request,
HttpServletResponse response {
final AsyncContext asyncContext = request.startAsync();
…
Runnable myTask = new Runnable() {
public void run() {
asyncContext.complete();
}
};
…
Future future = managedExecutorService.submit(myTask);
}
9
11. Services
ManagedScheduledExecutorService overview
• Provides a familiar API for scheduling asynchronous tasks in
Java EE
• Supports Java EE context propagation including JNDI naming,
classloader, and security contexts
• Not transactional contexts
• Used for scheduling tasks to run:
• periodically
• after specified delay
• at some custom schedule
10
12. Services
ManagedScheduledExecutorService API
• Extends both ManagedExecutorService and
java.util.concurrent.ScheduledExecutorService
interfaces
• Like ManagedExecutorService, calling any of the lifecycle
API methods results in IllegalStateException
• In addition to schedule methods inherited from Java SE API,
adds methods to support custom scheduling
• schedule with Trigger interface
11
13. Services
ManagedScheduledExecutorService Trigger API
• Interface implemented by application developer to support
flexible, custom scheduling
• Scheduling rules may be:
• simple: a single, absolute date/time
• complex: calendar logic
• Trigger is submitted along with a task using a schedule
method in ManagedScheduledExecutorService
12
14. Services
ManagedScheduledExecutorService Trigger Example
public class SimpleFixedDateTrigger implements Trigger {
private Date fireTime;
public SimpleFixedDateTrigger(Date triggerDate) {
fireTime = triggerDate;
}
public Date getNextRunTime(LastExecution lastExecutionInfo,
Date taskScheduledTime) {
if(taskScheduledTime.after(fireTime)) return null;
return fireTime;
}
public boolean skipRun(LastExecution lastExecutionInfo,
Date scheduledRunTime) {
return scheduledRunTime.after(fireTime);
}
}
13
15. Services
ManagedThreadFactory overview
• Provides the means for Java EE applications to obtain
container-managed threads
• Supports Java EE context propagation including JNDI naming,
classloader, and security contexts
• Not transactional contexts
14
16. Services
ManagedThreadFactory API
• Extends java.util.concurrent.ThreadFactory
• Same API: Thread.newThread(Runnable)
• Supports Java EE context propagation including JNDI naming,
classloader, and security contexts
• Not transactional contexts
• Threads from factory are:
• contextualized with container context
• required to implement the ManagableThread interface
15
17. Services
ManagedThreadFactory Example
// Create a ThreadPoolExecutor using a ManagedThreadFactory.
@Resource ManagedThreadFactory threadFactory;
public ExecutorService getManagedThreadPool() {
// All threads will be contextualized with the context
// of the creating application component.
return new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10), threadFactory);
}
16
18. Services
ContextService overview
• Creates dynamic proxy objects
• objects are contextualized with container context upon creation
• object methods execute with that captured context
• Used by the other services to contextualize task and threads
• May be used directly for advanced use cases:
• request contextualization of Trigger methods
• request contextualization of task listener notification methods
• propagate custom contexts
• Execution properties control how contextualization occurs
17
22. Additional APIs
Transaction Management
• Transactions are not propagated to the threads on which tasks
are run
• Application may obtain a UserTransaction from JTA
• Contextual proxy objects can run in the transaction context of
the invoking thread, controlled by the execution property
ManagedTask.TRANSACTION
• default is ManagedTask.SUSPEND
• ManagedTask.USE_TRANSACTION_OF_EXECUTION_THREAD
21
23. Additional APIs
ManagedTaskListener
• Tasks events are emitted to listeners via
ManagedTaskListener methods when tasks are:
• submitted
• unable to start or cancelled
• starting
• completed, either successfully or with exceptions
• Useful for:
• monitoring task progress
• resubmitting failed tasks
• ManagedTaskListener methods execute with unspecified
context by default
22
24. Additional APIs
ManagedTask
• Any task can optionally implement ManagedTask interface
which provides:
• ManagedTaskListener for lifecycle events notification
• task identification
• any desired execution properties
• Other standard execution ManagedTask properties
• IDENTITY_NAME – used to provide a task name
• LONGRUNNING_HINT – hint of task length
• TRANSACTION – controls transaction participation
– SUSPEND
– USE_TRANSACTION_OF_EXECUTION_THREAD
23
25. Additional APIs
ManagedExecutors
• Similar to Executors utility class of Java SE concurrency
package, contain methods for:
• Creating a ManagedTask capable of receiving notifications via
the specified ManagedTaskListener and with any specified
execution properties
• Testing whether current thread is a ManageableThread that has
been marked for shutdown
24
27. Additional Resources
• JSR236 specification and API docs
• http://jcp.org/en/jsr/detail?id=236
• JSR236 forum
• http://concurrency-ee-spec.java.net
• Java EE 7 API docs
• http://docs.oracle.com/javaee/7/api
26
28. Sampling of Related Sessions…
• AAI-1713A: Introduction to Java EE 7
• Monday, 2-3pm, Mandalay Bay, Reef Ballroom E
• AAI-1641A: Introduction to Web Sockets
• Monday, 5-6pm, Mandalay Bay, Reef Ballroom E
• AAI-1313A: Agile Development Using Java EE 7 with WebSphere Liberty Profile
(LAB)
• Tuesday, 8-10am, Mandalay Bay, South Seas Ballroom D
• AAI-2236A: Using the New Java Concurrency Utilities with IBM WebSphere
• Tuesday, 2-3pm, Mandalay Bay, Reef Ballroom D
• AAI-2235A: OpenJPA and EclipseLink Usage Scenarios Explained
• Wednesday, 5:30-6:30pm, Mandalay Bay, Surf Ballroom A
• AAI-1610A: Configuring IBM WebSphere Application Server for Enterprise
Messaging Needs
• Wednesday, 5:30-6:30pm, Mandalay Bay, Surf Ballroom E
• AAI-3085A: Don’t Wait! Develop Responsive Applications with Java EE7 Instead
• Thursday, 10:30-11:30am, Mandalay Bay, Lagoon L
27
31. Notices and Disclaimers (con’t)
Information concerning non-IBM products was obtained from the suppliers of those products, their published
announcements or other publicly available sources. IBM has not tested those products in connection with this
publication and cannot confirm the accuracy of performance, compatibility or any other claims related to non-IBM
products. Questions on the capabilities of non-IBM products should be addressed to the suppliers of those products.
IBM does not warrant the quality of any third-party products, or the ability of any such third-party products to
interoperate with IBM’s products. IBM EXPRESSLY DISCLAIMS ALL WARRANTIES, EXPRESSED OR IMPLIED,
INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE.
The provision of the information contained herein is not intended to, and does not, grant any right or license under any
IBM patents, copyrights, trademarks or other intellectual property right.
• IBM, the IBM logo, ibm.com, Bluemix, Blueworks Live, CICS, Clearcase, DOORS®, Enterprise Document
Management System™, Global Business Services ®, Global Technology Services ®, Information on Demand,
ILOG, Maximo®, MQIntegrator®, MQSeries®, Netcool®, OMEGAMON, OpenPower, PureAnalytics™,
PureApplication®, pureCluster™, PureCoverage®, PureData®, PureExperience®, PureFlex®, pureQuery®,
pureScale®, PureSystems®, QRadar®, Rational®, Rhapsody®, SoDA, SPSS, StoredIQ, Tivoli®, Trusteer®,
urban{code}®, Watson, WebSphere®, Worklight®, X-Force® and System z® Z/OS, are trademarks of
International Business Machines Corporation, registered in many jurisdictions worldwide. Other product and
service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on
the Web at "Copyright and trademark information" at: www.ibm.com/legal/copytrade.shtml.
32. Thank You
Your Feedback is
Important!
Access the InterConnect 2015
Conference CONNECT Attendee
Portal to complete your session
surveys from your smartphone,
laptop or conference kiosk.