[JAVAEETUTORIAL-154] unecessary "while(!mailStatus.isDone())" Created: 15/Feb/13  Updated: 19/Feb/13  Resolved: 19/Feb/13

Status: Resolved
Project: javaeetutorial
Component/s: examples
Affects Version/s: 6.0.8
Fix Version/s: 7.0.0

Type: Improvement Priority: Minor
Reporter: swiss-chris Assignee: Ian Evans
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: 5 minutes
Time Spent: Not Specified
Original Estimate: 5 minutes


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:

while (!mailStatus.isDone()) {       // line x+1
    this.setStatus("Processing..."); // line x+2
}                                    // line x+3

This seems problematic to me on various levels.

  1. We are unnecessarily setting the status to "Processing..." numerous times.
  2. We are unnecessarily occupying the processor until mailStatus.isDone().
  3. 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):

    this.setStatus("Processing..."); // line x+2


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.

Comment by swiss-chris [ 15/Feb/13 ]

I ran a quick test with the while-loop, counting the number of times it ran: 2,561,715,398 times !

Comment by Ian Evans [ 18/Feb/13 ]

If you email me a ZIP containing your code changes, I'll integrate them into the trunk for Java EE 7.0.
ian period evans at oracle period com

Comment by swiss-chris [ 18/Feb/13 ]

Great! I'll send you the ZIP tomorrow. Thanks.

Comment by Ian Evans [ 19/Feb/13 ]

I merged in Chris's changes. I kept MailerManagedBean as a CDI bean, but changed the scope to SessionScoped.

Generated at Mon Apr 24 05:04:34 UTC 2017 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.