Java Reference
In-Depth Information
} catch (InterruptedException|ExecutionException ex) {
Logger.getLogger(BuilderServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
,,,
}
Utilizing this technique, a series of tasks can be concurrently processed, returning results that can be later used
to formulate a response. In the case of this example, a report is constructed by calling two task classes and returning
the results of queried information. This same technique can be applied to an array of different tasks, allowing an
application to process the results of multiple task invocations in one central location.
Scheduled Tasks
To schedule a task to run at specific times, utilize the
javax.concurrent.ManagedScheduledExecutorService
interface. This interface extends the
java.util.concurrent.ScheduledExecutorService
and
javax.enterprise.concurrent.ManagedExecutorService
interfaces. The
ManagedScheduleExecutorService
can
be used to execute a
Runnable
task according to a specified schedule.
The Executor Service
As mentioned previously, a
ManagedScheduleExecutorService
can be used to schedule
Runnable
tasks. That is, any
class that implements
java.lang.Runnable
can be invoked via the service. The code that is contained within the task
class's
run
method is invoked each time the task is initiated. In the example that follows, the
run
method executes
another method within the class that is used to query an entity and perform some work against the results.
To make use of a
ManagedScheduledExecutorService
, one can be created within the application server
container. Similarly to the
ManagedExecutorService
, this resource can be created by issuing the
asadmin
create-managed-scheduled-executor-service
command. The GlassFish 4.0 application server also contains
a panel within the administrative console for creation of such resources. To create the resource from within the
administrative console, log in and navigate to the Resources
➤
Concurrent Resources
➤
Managed Scheduled
Executor Services panel (Figure
11-3
).
Figure 11-3.
Managed Scheduled Executor Services Panel