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
Taken from Oracle Docs on 29th January, 2017 from
http://docs.oracle.com/javaee/6/api/javax/ejb/Asynchronous.html
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]
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.
asyncRemote.processAsyncTask();
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
}
asyncRemote.nonAsyncMethod();
Result:
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.
Bibliography
- Annotation Type Asynchronous. (2011, February 10). Retrieved 2017, from Oracle Docs: http://docs.oracle.com/javaee/6/api/javax/ejb/Asynchronous.html
- Asynchronous communication with JMS(Java Messaging Service) Part 1. (2012, June 10). Retrieved from YouTube:https://www.youtube.com/watch?v=NAdrThcQkwQ
- Asynchronous Method Invocation. (2013). Retrieved 2017, from Oracle Docs: http://docs.oracle.com/javaee/6/tutorial/doc/gkkqg.html
- Asynchronous Method Invocation. (2014). Retrieved 2017, from Oracle Docs: https://docs.oracle.com/javaee/7/tutorial/ejb-async001.htm
This article was first published on August 17, 2017 on one of my earlier blogs.
Top comments (0)