Long running webservice architecture

后端 未结 2 421
迷失自我
迷失自我 2021-01-14 21:31

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

2条回答
  •  抹茶落季
    2021-01-14 22:14

    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:

    1. When the web service server initializes, create an ExecutorService instance.
    2. 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.
    3. This Callable is passed to ExecutorService.submit(), which immediately returns a Future object representing the eventual result of the call. The Executor will start to invoke your Callable in a separate thread.
    4. Generate a random token, store the Future in a Map with the token as the key.
    5. Return the token to the web service client (steps 1 to 4 should happen immediately)
    6. Later, he web service client makes another call asking for the result, passing in the token
    7. The server looks up the Future using the token, and calls get() on the Future, with a timeout value so that it only waits a short time for the answer. The get() call will return the execution result of whatever the Callable 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.

    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.

提交回复
热议问题