Asynchronous Method in Java EE
Tags: java, asynchronous, javaee
Published: 2020-06-17 15:35:14


Implementing asynchronous methods in Java EE is made simple by using the annotation @Asynchronous. It is part of the javax.ejb.Asynchronous package.  The @Asynchronous annotation is used to indicate that the method or methods in a class are asynchronous. This implementation of the annotation is processed at runtime. There is no @Inheritance so this annotation is not inherited by child classes. It should also be noted that in Java EE the result is returned to the Enterprise bean container and not directly to the client.

@Target(value={METHOD,TYPE})

@Retention(value=RUNTIME)

public @interface **Asynchronous**

<br>
Taken from Oracle Docs on 29th January, 2017 from 
[http://docs.oracle.com/javaee/6/api/javax/ejb/Asynchronous.html](http://docs.oracle.com/javaee/6/api/javax/ejb/Asynchronous.html)

<br>
**Rules to follow when implementing asynchronous methods in Java EE**
- Use @Asynchronous annotation before the method or if all the methods in the class are asynchronous use @Asynchronous before class declaration
- Any method that that is annotated with @Asynchronous must return either void or Future `<V>` – V can be primitive data type or a class name.
- Future`<V>` values can be easily created using ejb.AsyncResult`<V>`class. Example: return new AsyncResult`<String>`(“Async Ended “);

[1][2][3][4]

<br>
Example of an Asynchronous method in a bean that will take about over 30000 milliseconds to complete:

**@Asynchronous**

public **Future`<String>`** processAsyncTask() {

System.out.print(“Async started “+Thread.currentThread().getName());

try {

Thread.sleep(30000);

} catch (InterruptedException ex) { }

System.out.print(“Async ended”+Thread.currentThread().getName());

return **new AsyncResult`<String>`(“Async Ended “);**

}

Running this method and running a non-asynchronous method will show the other method performing its tasks while the operation in the asynchronous method has not been completed as yet.  The code below calls the above asynchronous method and a non-asynchronous method 10000 milliseconds after calling the asynchronous method.

<br>

asyncRemote.processAsyncTask();

try {

Thread.sleep(10000);

} catch (InterruptedException ex) {

 

}

asyncRemote.nonAsyncMethod();

**Result:**

![Image showing the output of an asynchronous programme that was run in NetBeans IDE. After the asynchronous method was called, the rest of the programme continued to execute even though the asychronous method was not completed as yet. Therefore, the non-asynchronous method was called after but compeleted before the asynchronous method. ](https://dev-to-uploads.s3.amazonaws.com/i/5kww3kcvpsb6cyjdbark.png)

After the asynchronous method was called, the rest of the programme continued to execute even though the asychronous method was not completed as yet. Therefore, the non-asynchronous method was called after but compeleted before the asynchronous method. 

<br>
**Bibliography**
1. <em>Annotation Type Asynchronous.</em> (2011, February 10). Retrieved 2017, from Oracle Docs: [http://docs.oracle.com/javaee/6/api/javax/ejb/Asynchronous.html](http://docs.oracle.com/javaee/6/api/javax/ejb/Asynchronous.html)
2. <em>Asynchronous communication with JMS(Java Messaging Service) Part 1.</em> (2012, June 10). Retrieved from YouTube:[https://www.youtube.com/watch?v=NAdrThcQkwQ](https://www.youtube.com/watch?v=NAdrThcQkwQ)
3. <em>Asynchronous Method Invocation.</em> (2013). Retrieved 2017, from Oracle Docs: [http://docs.oracle.com/javaee/6/tutorial/doc/gkkqg.html](http://docs.oracle.com/javaee/6/tutorial/doc/gkkqg.html)
4. <em>Asynchronous Method Invocation.</em> (2014). Retrieved 2017, from Oracle Docs: [https://docs.oracle.com/javaee/7/tutorial/ejb-async001.htm](https://docs.oracle.com/javaee/7/tutorial/ejb-async001.htm)

<br>
This article was first published on August 17, 2017 on one of my earlier blogs. 
<br>