KEMBAR78
Java EE 7 Recipes for Concurrency - JavaOne 2014 | PDF
Java EE 7 Recipes for 
Concurrency 
Presented By: Josh Juneau 
Author and Application Developer
About Me 
Josh Juneau 
Day Job: Developer and DBA @ Fermilab 
Night/Weekend Job: Technical Writer 
- Java Magazine and OTN 
- Java EE 7 Recipes 
- Introducing Java EE 7 
- Java 8 Recipes 
JSF 2.3 Expert Group 
Twitter: @javajuneau
Agenda 
Resolve a series of real life scenarios using the 
features of Concurrency Utilities for Java EE 
via the AcmeWorld application.
Java EE 7 Increases 
Productivity and 
Introduces Standards
Java EE 7 Increases 
Productivity 
• CDI Everywhere 
• JAX-RS Client API, Async Processing 
• Bean Validation in EJBs and POJOs 
• JSF Flows 
• JMS 2.0
Java EE 7 Introduces 
Standards 
• WebSockets 
• JSON-P 
• Batch API 
• Concurrency Utilities for Java EE 
• JPA Schema Generation
Concurrency Utilities 
for Java EE 
Concurrency Utilities for Java EE - Now we 
have a standard for developing multi-threaded 
and concurrent applications
Recipes!
Problem #1 
You are developing an online reservation 
system for a multi-million dollar company, 
and your client wishes to allow managers the 
ability to click a button to have the details of 
their all reservations sent to them, while the 
manager continues to navigate the site.
Solution 
When the user clicks the button, send the 
task to a ManagedExecutorService on the 
application server for processing, and allow 
the user to continue their work.
Solution
How it Works 
• In Java SE 5, the java.util.concurrent package was 
introduced, providing a convenient and standard 
way to manage threads within Java SE applications 
• The Concurrency Utilities for Java EE extends upon 
the SE implementation, making the concurrent 
resources injectable and placing them in the 
application server container 
• All Java EE 7 Compliant Application Servers must 
contain default concurrent resources, in GlassFish 
these resources begin with _default, e.g: 
_defaultManagedExecutorService
How it Works 
• Concurrency Utilities relies on JTA to maintain 
transaction boundaries 
• User transaction support 
• Manageable via JMX 
• Access a Concurrent Resource via JNDI lookup or 
Injection: 
InitialContext ctx = new InitialContext(); 
ManagedExecutorService executor = 
(ManagedExecutorService)ctx.lookup("java:comp/DefaultManagedExecutorService");
How it Works 
• A “Task” is a unit of work that needs to be executed in 
a concurrent manner 
• java.lang.Runnable or java.util.concurrent.Callable, and 
optionally ManagedTask 
• Context passed to the task 
• To submit an individual task to a 
ManagedExecutorService, use the submit method to 
return a Future object 
• To submit an individual task for execution at an 
arbitrary point, use the execute method
A Look at Lifecycle
Problem #2 
You wish to invoke multiple long-running or 
resource intensive tasks in a concurrent 
manner 
(Multiple reservation reports!)
Solution 
Submit multiple tasks to a 
ManagedExecutorService by calling the 
invokeAll or invokeAny method
Solution
How it Works 
Works in the same manner as invocation of a 
single task, except pass a Collection of tasks: 
Call invokeAll to execute all tasks 
May also specify timeout… 
Call invokeAny to execute at most one
Problem #3 
You wish to execute multiple background tasks, 
and then utilize the output of each together to 
formulate an end product. 
We want to find a specified reservation, and 
then look up its corresponding restaurant 
reservation.
Solution 
Send each managed task to the 
ManagedExecutorService separately, and then 
work with each of the Future results once they 
are returned.
Solution
How it Works 
It is possible to send multiple tasks to the 
ManagedExecutorService, and each of the tasks 
will return a separate Future object with which 
work can be completed.
Problem #4 
You would like to schedule a task to be 
executed at a specified date and time. 
! 
AcmeWorld management wants a count of 
the day’s reservations periodically.
Solution 
Create a task class, implementing either 
Runnable or Callable, and pass it to the 
ManagedScheduledExecutorService
Solution 
• Use a servlet to schedule tasks… 
AcmeWorldServletContextListener
How it Works 
• Reference managed executor via JNDI 
lookup or injection via @Resource 
• User transaction support
How it Works 
• Invoke task using 
ManagedScheduledExecutorService by 
invoking scheduleAtFixedRate() 
• Other options: schedule(), 
scheduleWithFixedDelay() 
• Trigger API for further customization 
• Reference managed executor via JNDI 
lookup or injection via @Resource 
• User transaction support
Problem #5 
You would like to fine tune a 
ScheduledManagedExecutorService to skip 
specified runs or a report. In this case, we 
wish to specify a period of delay between 
executions.
Solution 
Utilize a Trigger class to implement the business 
logic to calculate which runs should be skipped, 
and pass the Trigger class to the 
ManageScheduledExecutorService with the 
Task.
How it Works 
• Trigger API 
• Allows developers more control over 
how tasks are executed. 
• schedule(RunnableCallable, Trigger)
Trigger Example
How it Works 
• getNextRunTime(LastExecution, Date): 
• Specifies the next time the task is 
scheduled to be executed
How it Works 
• skipRun(LastExecution, Date): 
• Returns a TRUE if the task run instance 
should be skipped 
• Useful for skipping task instances if the 
previous instance is paused or skipped 
• Once task is skipped, the Future state will 
throw a SkippedException…unchecked 
exceptions wrapped in SkippedException
Another Visual of 
Managed Resources
Problem #6 
You are interested in spawning a thread to 
periodically execute a task in the background
Solution 
Spawn a server managed thread by passing a task via a 
ManagedThreadFactory
How it Works 
• A server managed Thread runs the same as any 
standard Thread...but in a managed fashion 
• Utilize JNDI lookup or Injection via @Resource 
• Context of invoking class can be passed to the 
Thread 
• Threads can be managed via JMX 
• User transaction support
Problem #7 
You are interested in propagating contextual 
information from the Java EE container 
runtime to other threads. 
! 
Implement a promotional program, whereby 
a supervisor enters a promotion and the 
employee later implements it using the 
supervisor’s approval authority.
Solution 
• Create a contextual proxy of a task that 
you wish to invoke later on. 
• Make use of ContextService resource 
Create contextual object, and pass to service 
In another session…
How it Works 
• Context of application is captured upon 
creation of proxy 
• Proxy object methods can be run in the 
captured context at a later time
How it Works 
• Creating Proxy: 
• createContextualProxy - various 
implementations 
• Utilizes java.lang.reflect package
How it Works 
• Dynamic proxy can be customized via 
execution properties 
• Return execution properties of given 
instance: 
• Map<String, String> 
getExecutionProperties(Object contextualProxy)
How it Works 
Can be executed on same transaction context 
a invoking thread: 
• Execution Task: 
ManagedTask.TRANSACTION 
Use Cases: 
• Propagate Security identity 
• Run tasks within a different context
Problem #8 
You would like to monitor the lifecycle 
current asynchronous operations that are 
running within your Java EE Container.
Solution 
Utilize a ManagedTaskListener to manage your 
Task’s lifecycle.
How it Works 
• Allow managed task class to implement 
ManagedTask interface 
OR 
• ManagedTaskListener is submitted along 
with the Task class to the executor via the 
ManagedExecutors.managedTask utility 
method
How it Works 
• Create a custom class that implements 
ManagedTaskListener, and override 
methods accordingly
How it Works 
• Lifecycle Monitoring: 
• taskSubmitted 
• taskAborted 
• taskStarting 
• taskDone
Problem #9 
Your application consists of a JSF view 
containing a tabbed UI. You’d like to load 
data on different tabs using separate 
concurrent tasks.
Solution 
Utilize a separate Managed ExecutorService 
for initiating tasks in each of the tabs. Utilize 
a sophisticated user interface that informs 
the user of the current status on each tab.
How it Works 
Separate ManagedExecutorServices initiated 
from each different tab. 
Utilization of PrimeFaces poll component to 
periodically check upon the status of the 
Future objects, and supply informative 
feedback to the user.
Problem #10 
You would like to learn more about 
Concurrency Utilities for Java EE. . .
Learn More 
Code Examples: https://github.com/juneau001/AcmeWorld 
Contact on Twitter: @javajuneau
Contact 
Josh Juneau 
- Java EE 7 Recipes 
- Introducing Java EE 7 
- Java 8 Recipes 
Twitter: @javajuneau

Java EE 7 Recipes for Concurrency - JavaOne 2014

  • 1.
    Java EE 7Recipes for Concurrency Presented By: Josh Juneau Author and Application Developer
  • 2.
    About Me JoshJuneau Day Job: Developer and DBA @ Fermilab Night/Weekend Job: Technical Writer - Java Magazine and OTN - Java EE 7 Recipes - Introducing Java EE 7 - Java 8 Recipes JSF 2.3 Expert Group Twitter: @javajuneau
  • 3.
    Agenda Resolve aseries of real life scenarios using the features of Concurrency Utilities for Java EE via the AcmeWorld application.
  • 4.
    Java EE 7Increases Productivity and Introduces Standards
  • 5.
    Java EE 7Increases Productivity • CDI Everywhere • JAX-RS Client API, Async Processing • Bean Validation in EJBs and POJOs • JSF Flows • JMS 2.0
  • 6.
    Java EE 7Introduces Standards • WebSockets • JSON-P • Batch API • Concurrency Utilities for Java EE • JPA Schema Generation
  • 7.
    Concurrency Utilities forJava EE Concurrency Utilities for Java EE - Now we have a standard for developing multi-threaded and concurrent applications
  • 8.
  • 9.
    Problem #1 Youare developing an online reservation system for a multi-million dollar company, and your client wishes to allow managers the ability to click a button to have the details of their all reservations sent to them, while the manager continues to navigate the site.
  • 10.
    Solution When theuser clicks the button, send the task to a ManagedExecutorService on the application server for processing, and allow the user to continue their work.
  • 11.
  • 12.
    How it Works • In Java SE 5, the java.util.concurrent package was introduced, providing a convenient and standard way to manage threads within Java SE applications • The Concurrency Utilities for Java EE extends upon the SE implementation, making the concurrent resources injectable and placing them in the application server container • All Java EE 7 Compliant Application Servers must contain default concurrent resources, in GlassFish these resources begin with _default, e.g: _defaultManagedExecutorService
  • 13.
    How it Works • Concurrency Utilities relies on JTA to maintain transaction boundaries • User transaction support • Manageable via JMX • Access a Concurrent Resource via JNDI lookup or Injection: InitialContext ctx = new InitialContext(); ManagedExecutorService executor = (ManagedExecutorService)ctx.lookup("java:comp/DefaultManagedExecutorService");
  • 14.
    How it Works • A “Task” is a unit of work that needs to be executed in a concurrent manner • java.lang.Runnable or java.util.concurrent.Callable, and optionally ManagedTask • Context passed to the task • To submit an individual task to a ManagedExecutorService, use the submit method to return a Future object • To submit an individual task for execution at an arbitrary point, use the execute method
  • 15.
    A Look atLifecycle
  • 16.
    Problem #2 Youwish to invoke multiple long-running or resource intensive tasks in a concurrent manner (Multiple reservation reports!)
  • 17.
    Solution Submit multipletasks to a ManagedExecutorService by calling the invokeAll or invokeAny method
  • 18.
  • 19.
    How it Works Works in the same manner as invocation of a single task, except pass a Collection of tasks: Call invokeAll to execute all tasks May also specify timeout… Call invokeAny to execute at most one
  • 20.
    Problem #3 Youwish to execute multiple background tasks, and then utilize the output of each together to formulate an end product. We want to find a specified reservation, and then look up its corresponding restaurant reservation.
  • 21.
    Solution Send eachmanaged task to the ManagedExecutorService separately, and then work with each of the Future results once they are returned.
  • 22.
  • 23.
    How it Works It is possible to send multiple tasks to the ManagedExecutorService, and each of the tasks will return a separate Future object with which work can be completed.
  • 24.
    Problem #4 Youwould like to schedule a task to be executed at a specified date and time. ! AcmeWorld management wants a count of the day’s reservations periodically.
  • 25.
    Solution Create atask class, implementing either Runnable or Callable, and pass it to the ManagedScheduledExecutorService
  • 26.
    Solution • Usea servlet to schedule tasks… AcmeWorldServletContextListener
  • 27.
    How it Works • Reference managed executor via JNDI lookup or injection via @Resource • User transaction support
  • 28.
    How it Works • Invoke task using ManagedScheduledExecutorService by invoking scheduleAtFixedRate() • Other options: schedule(), scheduleWithFixedDelay() • Trigger API for further customization • Reference managed executor via JNDI lookup or injection via @Resource • User transaction support
  • 29.
    Problem #5 Youwould like to fine tune a ScheduledManagedExecutorService to skip specified runs or a report. In this case, we wish to specify a period of delay between executions.
  • 30.
    Solution Utilize aTrigger class to implement the business logic to calculate which runs should be skipped, and pass the Trigger class to the ManageScheduledExecutorService with the Task.
  • 31.
    How it Works • Trigger API • Allows developers more control over how tasks are executed. • schedule(RunnableCallable, Trigger)
  • 32.
  • 33.
    How it Works • getNextRunTime(LastExecution, Date): • Specifies the next time the task is scheduled to be executed
  • 34.
    How it Works • skipRun(LastExecution, Date): • Returns a TRUE if the task run instance should be skipped • Useful for skipping task instances if the previous instance is paused or skipped • Once task is skipped, the Future state will throw a SkippedException…unchecked exceptions wrapped in SkippedException
  • 35.
    Another Visual of Managed Resources
  • 36.
    Problem #6 Youare interested in spawning a thread to periodically execute a task in the background
  • 37.
    Solution Spawn aserver managed thread by passing a task via a ManagedThreadFactory
  • 38.
    How it Works • A server managed Thread runs the same as any standard Thread...but in a managed fashion • Utilize JNDI lookup or Injection via @Resource • Context of invoking class can be passed to the Thread • Threads can be managed via JMX • User transaction support
  • 39.
    Problem #7 Youare interested in propagating contextual information from the Java EE container runtime to other threads. ! Implement a promotional program, whereby a supervisor enters a promotion and the employee later implements it using the supervisor’s approval authority.
  • 40.
    Solution • Createa contextual proxy of a task that you wish to invoke later on. • Make use of ContextService resource Create contextual object, and pass to service In another session…
  • 41.
    How it Works • Context of application is captured upon creation of proxy • Proxy object methods can be run in the captured context at a later time
  • 42.
    How it Works • Creating Proxy: • createContextualProxy - various implementations • Utilizes java.lang.reflect package
  • 43.
    How it Works • Dynamic proxy can be customized via execution properties • Return execution properties of given instance: • Map<String, String> getExecutionProperties(Object contextualProxy)
  • 44.
    How it Works Can be executed on same transaction context a invoking thread: • Execution Task: ManagedTask.TRANSACTION Use Cases: • Propagate Security identity • Run tasks within a different context
  • 45.
    Problem #8 Youwould like to monitor the lifecycle current asynchronous operations that are running within your Java EE Container.
  • 46.
    Solution Utilize aManagedTaskListener to manage your Task’s lifecycle.
  • 47.
    How it Works • Allow managed task class to implement ManagedTask interface OR • ManagedTaskListener is submitted along with the Task class to the executor via the ManagedExecutors.managedTask utility method
  • 48.
    How it Works • Create a custom class that implements ManagedTaskListener, and override methods accordingly
  • 49.
    How it Works • Lifecycle Monitoring: • taskSubmitted • taskAborted • taskStarting • taskDone
  • 50.
    Problem #9 Yourapplication consists of a JSF view containing a tabbed UI. You’d like to load data on different tabs using separate concurrent tasks.
  • 51.
    Solution Utilize aseparate Managed ExecutorService for initiating tasks in each of the tabs. Utilize a sophisticated user interface that informs the user of the current status on each tab.
  • 52.
    How it Works Separate ManagedExecutorServices initiated from each different tab. Utilization of PrimeFaces poll component to periodically check upon the status of the Future objects, and supply informative feedback to the user.
  • 53.
    Problem #10 Youwould like to learn more about Concurrency Utilities for Java EE. . .
  • 54.
    Learn More CodeExamples: https://github.com/juneau001/AcmeWorld Contact on Twitter: @javajuneau
  • 55.
    Contact Josh Juneau - Java EE 7 Recipes - Introducing Java EE 7 - Java 8 Recipes Twitter: @javajuneau