Java Reference
In-Depth Information
Table 11-1.
ScheduleExecutorService Methods
Method
Description
schedule(Callable<V> callable, long delay,
TimeUnit unit)
Creates and executes a ScheduledFeature object. The object
becomes available after the specified delay period.
schedule(Runnable command, long delay,
TimeUnit unit)
Creates and executes a one-time task that becomes available after
the specified delay.
scheduleAtFixedRate(Runnable command, long
initialDelay, long period, TimeUnit unit)
Creates and executes a periodic tasks that becomes available after
the initial specified delay period. Subsequent executions are then
scheduled in increments of the specified period after the initial
delay.
scheduleWithFixedDelay(Runnable command,
long initialDelay, long delay, TimeUnit unit)
Creates and executes a periodic task that becomes available after
the initial delay period. Subsequent executions are then scheduled
with the specified delay period in between each execution.
In the example, the
scheduleAtFixedRate
method is called, passing the task class, along with the initial delay
period of 5 minutes and then the task is executed every 5 minutes thereafter.
Example of Scheduled Task
Use the
ManagedScheduleExecutorService
to create a scheduled task within your application. As mentioned
previously, before an application can use the service, it must be created within the application server container.
To create a
ManagedScheduleExecutorService
instance within GlassFish, issue the following command from the
command-line:
bin/asadmin create-managed-scheduled-executor-service concurrent/name-of-service
In the command above, name-of-service can be whatever name you choose. The
create-managed-scheduled-
executor-service
command has many options that can be specified. To see and learn more about each option,
invoke the command help by issuing the
--help
flag after the command, rather than providing the name of the
service to create. Optionally, you could create the service using an application server resource, such as the GlassFish
administration console.
Once the service has been created within the container, it can be utilized by an application. To utilize this type of
service, the environment must be configured via XML or annotation. To utilize XML configuration, 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.ManagedScheduledExecutorService
, as shown in the excerpt from the
web.xml
below:
<resource-env-ref>
<description>Prints alerts to server log, if warranted, on a periodic basis</description>
<resource-env-ref-name>
concurrent/__defaultScheduledManagedExecutorService
</resource-env-ref-name>
<resource-env-ref-type>
javax.enterprise.concurrent.ManagedScheduledExecutorService
</resource-env-ref-type>
</resource-env-ref>
If you wish to use annotations rather than XML, the
@Resource
annotation can be used in
client code to inject the
ManagedScheduledExecutorService
, as shown in the following lines. In this case, the