Java Reference
In-Depth Information
To make use of the ManagedExecutorService , register it with the application via XML or by resource injection. In the
example, resource injection is utilized, making the ManagedExecutorService available from within the Java servlet. To
inject the resource, specify the name of it to the @Resource annotation.
@Resource(name = "concurrent/BatchExecutor")
ManagedExecutorService mes;
The ManagedExecutorService can then be invoked by calling the submit method, and passing an instance of
the Runnable task that we'd like to submit for processing. In this case, the ReporterTask class is instantiated, and an
instance of it is then passed to the service, returning a java.util.concurrent.Future object.
ReporterTask reporterTask = new ReporterTask("BookReport");
Future reportFuture = mes.submit(reporterTask);
Once submitted, the Future object that was returned can be periodically queried to see if it is still running, or if
it has been completed by calling its isDone method. It can be cancelled by calling the cancel method, and a canceled
task can be checked by calling its isCanceled method.
Example of ReporterTask
Once the application server has been configured and the ManagedExecutorService has been created, an application
can be written to utilize the newly created service. Within an application, one can choose to configure the application
to make use of the ManagedExecutorService via XML, or a @Resource annotation can be used to inject the resource.
To configure via XML, add a <resource-env-ref> element to the web.xml deployment descriptor. In this case, you
need to configure a resource of type javax.enterprise.concurrent.ManagedExecutorService , as shown in the
excerpt from the web.xml below:
This executor is used for the application's reporter task. This executor has the following
Run Location: NA
Context Info: Local Namespace
In the XML configuration, the resource has been assigned to a reference name of concurrent/BatchExecutor ,
but you could name the reference to best suit your application. If you would rather utilize an annotation, then the
following @Resource annotation can be specified to inject a ManagedExecutorService into a class for use. You will see
an example of this in use later on.
@Resource(name = "concurrent/BatchExecutor")
ManagedExecutorService mes;
Search WWH ::

Custom Search