EJB Asynchronous moves the task of handling concurrent threads to the container to manage. Java’s concurrency cannot be used in the normal way; by creating Thread runners, with wait() and notify() and so on.  Adam Bien talked about managing threads in JavaEE 6 using an Executor on his blog.

Asynch is easier though it does seem to me to be less powerful.  However the power of an Executor would seem to trump the low-level benefits of using Threads directly. See this Lars Vogel tutorial on Java Concurrency for what they allow you to do.

I digress; back to Async where it seems to be easier then Threads and Executor’s.

I will show an example simple usage, making use of isDone() in the client and how to use cancel().

Example simple usage

From Enterprise Java Beans 3.1 book page 60ish:

// for some Session Bean with an interface
public Future hashAsync(final String input) throws IllegalArgumentException, EncryptionException
    // Get the real hash
    final String hash = this.hash(input);
    // Wrap and return
    return new AsyncResult(hash);

// And used by the client like this
public void someMethod() {
    // Declare the input
    final String input = "Async Hashing Input";
    // Hash
    final Future hashFuture = myEjbProxyReference.hashAsync(input);
    // Now we're free to do whatever work we want here while the EJB invocation runs concurrently
    // ...[work]
    // At this point we need the hash, so get the result,
    // and block for up to 10 seconds to get it
    final String hash = hashFuture.get(10,TimeUnit.SECONDS);
    log.info("Hash of \"" + input + "\": " + hash);

When is the Asynch method done?

You’d use the following code in some way such as set an Observer on it for population in a GUI. That’s an exercise for the reader.

  final String input = "Async Hashing Input";
  // Hash
  final Future hashFuture = myEjbProxyReference.hashAsync(input);
  if (hashFuture.isDone()) {
    final String hash = hashFuture.get();

Cancelling and making the most of Asynch code

I found this quite confusing, as did others.

I did some searching and the gestault switch flicked when I read IBM discussion of Developing client code that calls EJB asynchronous methods in Websphere.

What follows is my interpretation.

In the client we have:

  final String input = "Async Hashing Input";
  // Hash
  final Future hashFuture = myEjbProxyReference.hashAsync(input);
  if (hashFuture.cancel()) {
      // it was cancelled and got a true return value - what does this mean?;
  } else {
      // it was cancelled and got a false return value - what does this mean?

And the Asynchronous method in the Session Bean is:

public Future run() throws Exception {
    while (myContext.wasCancelCalled() == false) {
        // do work can do in stages
    return new AsyncResult(result);

The contents of the while loop is some task we can break down into smaller chunks and each chunk is done in a trip around the while loop. The guard on the loop is what checks if the operation has been cancelled.

But what does the return value from the if (hashFuture.cancel()) mean?

  • true – means that the cancel happened before the Asynch method was dispatched (ie. the run()method never started)
  • false – means that the run() method started. In which case the while loop in the Asynch method was necessary to cancel the operation.
  • mayInterruptIfRunning – if true means that once the run() method has started the container won’t stop it. Thus the while loop will have no affect.

I’m doing study for the BUSINESS COMPONENT DEVELOPMENT WITH EJB TECHNOLOGY, JAVA EE 6 certification and although I didn’t think I needed to know such details for the exam, it bugged me and I just had to find out. And the truth of the matter is that I don’t know what will be on the exam.

I hope this helps anyone else doing this study or just wanting to know.

I now have a question, be it another itch I must scratch – how to use the features of Asynch I’ve just discussed with Executor’s…..

Written on September 11th, 2012 , EJB, Java Tags: , , ,

Brooke Smith is proudly powered by WordPress and the Theme Adventure by Eric Schwarz
Entries (RSS) and Comments (RSS).

Brooke Smith

Portfolio and site of a software engineer