Using an asynchronous method to create a background process

Accessing an EJB synchronously allows the client to continue its work without having to wait for the EJB to return. This can be used in one of two ways. The first technique is done in an "invoke and forget" manner where the request is made of the EJB and the client is not concerned about the success or failure of the request. The second technique invokes the method but does not wait for the method to complete. The method returns a Future object. This object is used later to determine the result of the request.

In this recipe, we will develop a stateless PrintServlet EJB with two methods: printAndForget and printAndCheckLater, used to demonstrate the use of asynchronous methods.

How to do ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.