Enterprise Java

Java EE Revisits Design Patterns: Asynchronous

Although you may not find Asynchronous method calls listed as a design pattern, I find it worth to mention. So here comes the last post of my JavaEE Revisits Design Patterns series.

Asynchronous method calls is not much more than multithreading. Basically it refers to a a method call which would run in a separate thread, thus the main (caller) thread does not need to wait for the result of the execution of the called method. In the age of web programming, developers mostly delegate the threading issues to the running server and creating new threads can be tricky and sometime dangerous on web servers since they usually like to manage the threads themselves.

However, playing nice with the servers while using threads can be very simple with JavaEE. Annotating a method with @Asynchronous would be enough to tell the JavaEE container to run the called method in a separate thread asynchronously. To test asynchronous execution lets add a new method marked with the Asynchronous annotation to our previous example.

package com.devchronicles.observer;


import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.enterprise.event.Observes;


/**
 *
 * @author Murat Yener
 */
@Stateless
public class EventObserver {
    
    @Asynchronous
    public void doLogging(@Observes String log) {
        System.out.println("1.Start logging:"+log);
        try{
             Thread.sleep(3000);
        }catch (InterruptedException e){}
        System.out.println("1.done logging");
    }
    
    public void doLogging2(@Observes String log) {
        System.out.println("2.Start logging:"+log);
        try{
             Thread.sleep(3000);
        }catch (InterruptedException e){}
        System.out.println("2.done logging");
    } 
}

The EventService class remains same except for few lines for logging.

package com.devchronicles.observer;


import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;


/**
 *
 * @author Murat Yener
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EventService {
    @Inject
    private String message;
    
    @Inject 
    Event<string> event;
    
    public void startService(){
        System.out.println("start service call "+message);
        event.fire("this is my "+message);
        System.out.println("done...");
    }
}

Run the application and click on the button on the index.xhtml which would fire up the startService method. The log file should be something similar to the one below.

INFO: Observer was successfully deployed in 553 milliseconds.
INFO: start service call A message!!
INFO: 2.Start logging:this is my A message!!
INFO: 2.done logging
INFO: done...
INFO: 1.Start logging:this is my A message!!
INFO: 1.done logging

Although the log might differ, you should still clearly see the startService method is called which fired the event followed by the execution of the second logging method. The startService method waited until the execution of the second log method is complete. However, the first logging method started and end its execution independently from either of the other methods execution.

Although this example is based on void methods, its quite simple to use Future<> as a return type and to receive a result asynchronously.

Asynchronous annotation is very easy to use and can be very useful in situations where you do not want to wait for the execution of the called method.

Reference: JavaEE Revisits Design Patterns: Asynchronous from our JCG partner Murat Yener at the Developer Chronicles blog.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button