How to run a thread inside a webservice
Could someone help me on this,
I have created simple web services using axis2 , apache and tomcat. This web service has a queue that keeps xml files sent from a client, so whenever a 开发者_StackOverflow中文版client calls a method on the webservice, the webservice loads this xml to its queue. Now I want to have a thread running in the webservice which monitors this queue, and if there are items in the queue takes some action.
But my problem is that the webservice is invoked only if a client calls one of the methods on its interface. But I need this thread to be running on webserbice. Could someone tell me is there are is a way to do this?
When a client invokes a method on the webservice, it does not consider previous method invocations. This means that it does not keep track of data in the queue, for each invocation it creates new queue.
If you need background threads in a web application you must manage them inside a ContextListener registered in web.xml. You are then notified when your web application is started and stopped.
Sounds like the perfect use case for JMS/message driven beans. Spring JMS provides these facilities without having to use a full-blown J2EE container, so tomcat will fit here. Active MQ can provide the messaging engine.
Essentially, your web service would put a message on a queue and a message driven bean (or message driven pojo) would read them off the queue and process. Using JMS would have the advantage that you'd be able to reconfigure the message driven bean to sit on a separate host if you're load on the server grows. It'll also mean you'll be able to move to different app servers with ease as JMS is a standardised solution.
精彩评论