Java Reference
In-Depth Information
NOTE
Dependency injection is not available within a callback handler. Vendors may allow this, but the spec
does not require it, so such behavior, if available, is not portable. You can use JNDI to look up your
resource if it is bound.
There are just a few rules regarding writing the handler. First, it cannot itself be an EJB or a
servlet. Also, there is no transaction context specified for callback handlers. That means that
while you can create a UserTransaction within your handleResponse method, you can-
not propagate it to another resource; it must be either committed or rolled back before your
method exits. Otherwise, the callback handler implementation is fairly straightforward. This
is shown in Example 6-24 .
Example6-24.AsyncHandler<T> implementation
class MyHandler implements AsyncHandler<DoLongJobResponse> {
private static final Logger LOGGER =
Logger.getLogger(MyHandler.class);
private DoLongJobResponse response;
public void handleResponse(Response<DoLongJobResponse> in) {
LOGGER.debug("Executing callback handler.");
try {
response = in.get();
LOGGER.debug("Got response! " +
response.getJobDone());
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public DoLongJobResponse get() {
return response;
}
}
The implementation requires only the single handleResponse method. Because you save the
response to an instance field, there's an additional method to retrieve the result of the opera-
Search WWH ::




Custom Search