问题
We use axis2 for building our webservices and a Jboss server to run the logic of all of our applications. We were asked to build a webservice that talks to a bean that could take up to 1 hour to respond (depending on the size of the request) so we would not be able to keep the connection with the consumers opened during that time.
We could use an asynchronous webservice but that hasn't come out all that well so we decided we could implement a bean that will do the logic behind the webservice and have the service invoke that bean asynchronously. The webservice will generate a token that will pass to the consumer and the consumer can use it to query the status of the request.
The questions I have are:
- How to I query the status of the bean on the Jboss server once I have returned from the method in the service that created that bean. Do I need to use stateful beans?
- Can I use stateful beans if I want to do asynchronous calls from the webservice side?
回答1:
Another approach you could take is to make use of JMS and a DB.
The process would be
- In web service call, put a message on a JMS Queue
- Insert a record into a DB table, and return a unique id for that record to the client
- In an MDB that listens to the Queue, call the bean
- When the bean returns, update the DB record with a "Done" status
- When the client calls for status, read the DB record, return "Not Done" or "Done" depending on the record.
- When the client calls and the record indicates "Done", return "Done" and delete the record
This process is a bit heavier on resource usage, but has some advantages
- A Durable JMS Queue will redeliver if your bean method throws an Exception
- A Durable JMS Queue will redeliver if your server restarts
- By using a DB table instead of some static data, you can support a clustered or load balanced environment
回答2:
I don't think stateful session beans are the answer to your problem, they're designed for long-running conversational sessions, which isn't your scenario.
My recommendation would be to use a Java5-style ExecutorService thread pool, created using the Executors factory class:
- When the web service server initializes, create an
ExecutorService
instance. - Web service call comes in, the handler creates an instance of Callable. The
Callable.call()
method would make the actual invocation on the business logic bean, in whatever form that takes. - This
Callable
is passed toExecutorService.submit()
, which immediately returns aFuture
object representing the eventual result of the call. TheExecutor
will start to invoke yourCallable
in a separate thread. - Generate a random token, store the
Future
in aMap
with the token as the key. - Return the token to the web service client (steps 1 to 4 should happen immediately)
- Later, he web service client makes another call asking for the result, passing in the token
- The server looks up the
Future
using the token, and callsget()
on theFuture
, with a timeout value so that it only waits a short time for the answer. Theget()
call will return the execution result of whatever theCallable
invoked.- If the answer is available, return it to the client, and remove the
Future
from the `Map. - Otherwise, tell the client to come back later.
- If the answer is available, return it to the client, and remove the
It's a pretty robust approach. You can even configure the ExecutorService
to limit the number of calls that can be in execution at the same time, if you so desire.
来源:https://stackoverflow.com/questions/1835652/long-running-webservice-architecture