Java Reference
In-Depth Information
@Override
public ManagedTaskListener getManagedTaskListener() {
return taskListener.get(); [3]
}
@Override
public Map<String, String> getExecutionProperties() {
return new HashMap<>(); [4]
}
@Override
public List<Integer> call() throws Exception {
logger.info("Sleeping...");
Thread.sleep(5000); [5]
logger.info("Finished sleeping!");
return Arrays.asList(4, 5, 6);
}
}
Our task implements [1] two interfaces: Callable and ManagedTask . The Man-
agedExecutorService requires an object that fulfils the contract of a Callable or
Runnable interface known from Java SE.
The ManagedTask interface is optional, but it allows us to register a Man-
agedTaskListener along with the task itself and return additional properties from the
task. The task listener has a set of life cycle callbacks, which are called during the task's
execution. We will use it in order to log additional information about our task. In order to
create an instance of the task listener, we used the Instance<T> class [2] . It is used to
create instances of CDI beans on demand. We return ManagedTaskListener in a
method from the ManagedTask interface [3] . We don't need any additional properties;
therefore, we return an empty object in the second method from the ManagedTask in-
terface [4] .
Finally, we implement the call method; the thread will be suspended for 5 seconds (to
simulate long work) and return a list of predefined IDs.
Our task listener is simply a bean with a logger, which will get all the information about
the task's lifecycle. This is shown in the following code snippet:
Search WWH ::




Custom Search