Java EE 5 - How to implement an async method call without JMS
Asked Answered
C

3

6

We have an old application that runs on JBoss EAP 5.1 and exposes a web service that invokes a long running ejb method, so we would like to integrate a simple async management of this method call, just something like this:
when a client invokes the endpoint method, the server immediately returns an UUID and then invokes the business logic method in a separate thread.
What is the best way to implement this without using JMS?

Carinacarinate answered 7/1, 2014 at 11:10 Comment(0)
O
1

I would suggest to use a message driven bean (MDB) and use the WebService as a Producer which sends the message to the MDB. The MDB then invokes the expensive bean method within its onMessage method. This is EJB3.0 conform and possible within JBoss 5.1

Have a look here: http://docs.oracle.com/javaee/5/tutorial/doc/bnbpk.html or for the complete specification: http://download.oracle.com/otndocs/jcp/ejb-3_0-fr-eval-oth-JSpec/

Obsidian answered 7/1, 2014 at 23:33 Comment(0)
V
1

You can use java 5's ExecutorService feature which will provide thread pool type implementation. You can create runnable object and submit it to the thread pool.Business method can be called by the run method of the runnable object.

Veneer answered 20/1, 2014 at 11:19 Comment(0)
L
0

With EJB 3.1, you have the possibility to make asynchronous method calls. If you are using Java 6 o later, one possibility could be annotate your ejb business method with the annotation @Asynchronous. This way, when you invoke the ejb from the web service, the ejb container will return the control immediatly to the WS.

If the ejb method returns a value, things could be more complicated, you will need to check periodically the execution status and once completed get the result. If this is your scenary, take in mind that the code that invoke the asyncronous method has to maintain a reference to a Future object.

Other option could be to create a new thread that execute the ejb method invocation. Since JEE 7 (JSR 236: Concurrency Utilities), exists the possibility to create a managed thread, which allows you run new threads within a Container in a safe way.

Levesque answered 7/1, 2014 at 15:47 Comment(3)
As i wrote, we are using JavaEE 5 on JBoss EAP 5.1 (this means EJB 3.0)Carinacarinate
JBoss EAP 5.1 can execute with different java version.Levesque
The annotation javax.ejb.Asynchronous belongs to JavaEE 6 and it's not supported by JBoss EAP 5.1, see this tableCarinacarinate

© 2022 - 2024 — McMap. All rights reserved.