In the async EJB example (doc: http://docs.oracle.com/javaee/6/tutorial/doc/gkiez.html) the async.web.MailerManagedBean's send() method contains the following code:
This seems problematic to me on various levels.
- We are unnecessarily setting the status to "Processing..." numerous times.
- We are unnecessarily occupying the processor until mailStatus.isDone().
- Immediately after this code, we call mailStatus.get(), which has the same effect as the while-loop (here too we wait for mailStatus.isDone())
My proposal would be to delete the first and last line from the three lines of code pasted above, leaving only the middle line (which never produces any visible result, by the way):
Furthermore, in my local code I reworked the example a bit,
- using @ManagedBean @SessionScoped instead of @Named @RequestScoped.
- making Future<String> mailStatus a class member instead of a local variable.
- removing the try-catch block containing this.setStatus(mailStatus.get() out of the send() method and into the getStatus() method, protected by an additional if (mailStatus.isDone()) check.
- changing the response value of the send() method from "response" to "response?faces-redirect=true"
This allows me to actually see the "Processing..." status in the browser as well as to refresh the page manually until the status changes. Personally, I find this gives me more of a feeling for the asynchronous nature of the code underneath, rather than experiencing a simple long-running synchronous response in the browser.