Home » Java » Enterprise Java » Java EE Revisits Design Patterns: Asynchronous

About Murat Yener

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


− 3 = one

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.