I wrote a small test to understand asynchronized behavior in EJB3.1 using @Asynchronous
annotation. It seems that:
If the method exist in the same bean as the caller,
However, if the asynchronous method exists in another bean, the behavior is expected i.e. call is asynchronous and the caller transaction is independent (async method have REQUIRES_NEW behavior for transaction). This is puzzling for me, as why the behavior is not the same in both cases. Would someone please clarify?
P.S. Environment EJB 3.1, JBoss 6
An Asynchronous call does not block the program from the code execution. When the call returns from the event, the call returns back to the callback function. So in the context of Java, we have to Create a new thread and invoke the callback method inside that thread.
Asynchronous methods are typically used for long-running operations, for processor-intensive tasks, for background tasks, to increase application throughput, or to improve application response time if the method invocation result isn't required immediately.
Annotation Type Asynchronous Used to mark a session bean method as an asynchronous method or to designate all business methods of a session bean class as asynchronous. An asychronous method must have return type void or Future<V> , where V is the result value type.
When a synchronous method is invoked, it completes executing before returning to the caller. An asynchronous method starts a job in the background and returns to the caller immediately. Synchronous Methods. A typical synchronous method returns the result directly to the caller as soon as it completes executing.
Calling the method using this
keyword instead of using SessionContext#getBusinessObject(class)
causes that. (the difference is explained here )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With