Re: Create new thread from Web Service
ShaneLM wrote:
Hi, I'm new to java web services and am trying to understand the
context in which they are run. Any pointers to the web service
lifecycle (specifically through Axis2) would be awesome.
My specific problem is that I'm trying to create a web service that
spawns a background process that will keep running after the web
service call completes. So far my research has given 2 possible
methods.
1) Create a new thread during the service call. My worries are that
this will get killed as soon as the web service returns, or that I
won't be able to maintain a handle on it when I want to kill the
thread later.
I don't think it will just get killed, but the real danger is that
requests might spawn too many threads, and cause a loss of service.
2) Use the Axis2 framework to develop an asynchronous service, as
described here http://www.ibm.com/developerworks/webservices/library/ws-axis2/
Don't have much knowledge about this. sorry...
Would these methods work? Which would you recommend?
In either method, what's the best way to exchange data between this
background process and any future web service calls?
It might actually be better to consider a Producer/Consumer pattern. The
service request produces a Task to be done in the background, and puts
it into a queue. The Consumer is pool of threads that will read from
this task.
As for keeping track of Tasks, you can put them into some shared
data-structure that maps from some ID to the actual Task.
Also, look at the Java Concurrency:
<http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html>
I also suggest reading Java Concurrency in Practice:
<http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurrency-in-practice/>
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>