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?
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/
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.
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.
© 2022 - 2024 — McMap. All rights reserved.