Configuring managed scheduled executors
You can configure ManagedScheduledExecutorService instances to schedule asynchronous tasks to run with the thread context of the thread from which the task is scheduled. It is a best practice for Java™ EE applications to avoid directly managing their own threads; therefore, the ManagedScheduledExecutorService extends the JSE ExecutorService to provide a way to schedule asynchronous tasks within an application server environment. You might also configure the ManagedScheduledExecutorService to capture a thread context that is relevant to Java EE applications and propagate it to the thread of the scheduled task.
About this task
<featureManager>
<feature>concurrent-1.0</feature>
</featureManager>
Thread context capture and propagation is managed by the context service. A default instance of the context service (DefaultContextService) is created by the server and configured to propagate at least classloaderContext, jeeMetadataContext and securityContext. This default context service instance is used if a ManagedScheduledExecutorService is created without referring to a specific context service instance or configuring a context service instance directly within. For more information about context service instances, refer to the Configuring thread context service instances topic.
A default managed scheduled executor instance (DefaultManagedScheduledExecutorService) is available as java:comp/DefaultManagedScheduledExecutorService and uses the default context service instance for thread context capture and propagation.
Concurrency policies configure concurrency-related behaviors
and constraints that apply to managed scheduled executors, such as maximum concurrency and maximum
queue size. By default, managed scheduled executors use a concurrencyPolicy
configuration element default instance, defaultConcurrencyPolicy, which has
constraints that are unbounded. This default concurrency policy is used if you configure a managed
scheduled executor without referring to or directly configuring a specific
concurrencyPolicy element as a nested element. If multiple managed scheduled
executors or other configuration elements refer to the same concurrencyPolicy
element, the constraints in that policy apply across all of those managed scheduled executor
instances and other configured resources. You can also configure a managed scheduled executor with a
concurrency policy for long-running tasks, which applies to tasks with the
LONGRUNNING_HINT execution property set to true. The configuration
that is specified in the concurrencyPolicy element and the long-running
concurrencyPolicy element applies to tasks submitted to run as soon as possible.
The configuration does not apply to scheduled tasks.
Procedure
Example configuration in the server.xml file:
Example
Inject managed scheduled executors into application components (by using @Resource) or look up with resource environment references (resource-env-ref). Regardless of how the instance is obtained, it can be used interchangeably as javax.enterprise.concurrent.ManagedScheduledExecutorService or any of the following superclasses: java.util.concurrent.ScheduledExecutorSerivce, java.util.concurrent.ExecutorService, javax.enterprise.concurrent.ManagedExecutorService
- Example that looks up the default managed scheduled
executor:
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) new InitialContext().lookup( "java:comp/DefaultManagedScheduledExecutorService"); executor.schedule(beginSalePrices, 12, TimeUnit.HOURS); executor.schedule(restoreNormalPrices, 60, TimeUnit.HOURS);
- Example that uses @Resource to inject as
java.util.concurrent.ScheduledExecutorService:
@Resource(lookup="concurrent/scheduledExecutor2") ScheduledExecutorService executor; ... // schedule a task to run every half hour from now Runnable updateSalesReport = new Runnable() { public void run() throws Exception { // java:comp lookup is possible because <jeeMetadataContext> is configured DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1"); ... query and update various database tables } }; ScheduledFuture<?> future = executor.scheduleAtFixedRate(updateSalesReport, 0, 30, TimeUnit.MINUTES);
- Example that uses @Resource to inject as javax.enterprise.concurrent.ManagedScheduledExecutorService:
@Resource(lookup="concurrent/scheduledExecutor2") ManagedScheduledExecutorService executor; ... usage is same as previous example
- Example <resource-env-ref> for java.util.concurrent.ScheduledExecutorService in
the web.xml file:
<resource-env-ref> <resource-env-ref-name>concurrent/scheduledExecutor1</resource-env-ref-name> <resource-env-ref-type>java.util.concurrent.ScheduledExecutorService</resource-env-ref-type> </resource-env-ref>
- Example <resource-env-ref> for javax.enterprise.concurrent.ManagedScheduledExecutorService in
the web.xml file:
<resource-env-ref> <resource-env-ref-name>concurrent/scheduledExecutor2</resource-env-ref-name> <resource-env-ref-type>javax.enterprise.concurrent.ManagedScheduledExecutorService</resource-env-ref-type> </resource-env-ref>
- Example lookup that uses a resource environment reference:
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) new InitialContext().lookup("java:comp/env/concurrent/scheduledExecutor2"); executor.schedule(payrollTask, fridaysAtMidnightTrigger);