Java EE Concurrency Utilities
-
Upload
reza-rahman -
Category
Technology
-
view
10.287 -
download
2
description
Transcript of Java EE Concurrency Utilities
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.1
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.2
Concurrency Utilities for the Java EE PlatformReza RahmanGlassFish/Java EE [email protected]
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4
Concurrency Utilities for the Java EE Platform
Provides low-level asynchronous processing capabilities to Java EE application components in a safe, reliable, consistent manner
Extension of Java SE Concurrency Utilities APIs
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5
Primary Interfaces
Extends existing Java SE concurrency utilities– ManagedExecutorService
– ManagedScheduledExecutorService
– ManagedThreadFactory
New interface– ContextService
Lookup via JNDI or @Resource Default objects
– java:comp/DefaultManagedExecutorService etc...
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6
ManagedExecutorService
For running tasks asynchronously on threads provided by Java EE container
Container context captured from submitting thread to be applied on execution thread
Not transactional – tasks should use their own transactions
Overview
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7
ManagedExecutorService
Extends from java.util.concurrent.ExecutorService No additions Lifecycle APIs disabled – throws IllegalStateException
– awaitTermination, isTerminated, isShutdown, shutdown, shutdownNow
API
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8
ManagedExecutorServiceAPI
void execute(Runnable command);
<T> Future<T> submit(Callable<T> task);
Future<?> submit(Runnable task);
<T> Future<T> submit(Runnable task, T result);
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks);
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit);
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit);
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.9
ManagedExecutorServiceExample@Resource(name=“concurrent/myExecutor”)ManagedExecutorService mes;
void someMethod() { Callable<Integer> c = new Callable<>() { Integer call() { // Interact with a database...return answer. }
// Submit the task and do something else. Future result = mes.submit(c); ... // Get the result when ready... int theValue = result.get(); ...
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.10
ManagedScheduledExecutorService
For scheduling tasks to run after a given delay, periodically, or at some custom schedule
Extends from ManagedExecutorService and java.util.concurrent.ScheduledExecutorService
Overview
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11
ManagedScheduledExecutorServiceAPI
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);
ScheduledFuture<V> schedule(Runnable command, long delay,
TimeUnit unit);
ScheduledFuture<?> scheduleAtFixedFate(Runnable command,
long initialDelay, long period, TimeUnit unit);
ScheduledFuture<?> scheduledWithFixedDelay(Runnable command,
long initialDelay, long delay, TimeUnit unit);
ScheduledFuture<?> schedule(Runnable command, Trigger trigger);
<V> ScheduledFuture<V> schedule(Callable<V> callable, Trigger trigger);
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12
ManagedScheduledExecutorServiceTrigger
interface Trigger {
// Return true if you want to skip the
// currently-scheduled execution.
boolean skipRun(LastExecution lastExecutionInfo,
Date scheduledRunTime);
// Retrieves the time in which to run the task
// next. Invoked during submit time and after
// each task has completed.
Date getNextRunTime(LastExecution lastExecutionInfo,
Date taskScheduledTime);
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13
Managed[Scheduled]ExecutorServiceManagedTaskListener
Listeners can be registered with the task when submitted to Managed[Scheduled]ExecutorService
API– taskSubmitted – The task was submitted to the executor
– taskAborted – The task was unable to start or was cancelled
– taskStarting – The task is about to start
– taskDone – The task has completed (successfully, exception, cancelled, aborted, or rejected)
The listener method runs in unspecified context, but can be configured to run in the same container context as the task
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14
Managed[Scheduled]ExecutorServiceManagedTask
Any task submitted to an ManagedExecutorService or ManagedScheduledExecutorService can optionally implement ManagedTask
API– Map<String, String> getExecutionProperties()
– ManagedTaskListener getManagedTaskListener()
Execution properties– CONTEXTUAL_CALLBACK_HINT
– IDENTITY_NAME
– LONGRUNNING_HINT
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15
ManagedTaskRegistering ManagedTaskListener
// Runnable implements ManagedTaskpublic class TaskWithListener implements Runnable, ManagedTask { ... public ManagedTaskListener getManagedTaskListener() { return aManagedTaskListener; }}// Or use ManagedExecutors utility method to associate// a ManagedTaskListener to a taskRunnable aTask;ManagedTaskListener myTaskListner;Runnable taskWithListener = ManagedExecutors.managedTask(aTask, myTaskListener);// Submit taskWithListener to a ManagedExecutorService
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16
Where to Find Out More
Read the draft spec and API docs at http://concurrency-ee-spec.java.net
GlassFish 4.0 – http://glassfish.java.net/
– http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/
Reference Implementation– http://java.net/projects/cu-javaee
Slide Deck– http://www.slideshare.net/reza_rahman
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17
Graphic Section Divider
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18