User thread in JAX-WS web method

I have a problem with a web service through JAX-WS. If I start a thread in a web method, it will end when the client connection is completed.

Example:

@WebMethod(operationName="test") public boolean test() { Thread th = new MyThread(); th.start(); // Thread is running ... return true; // Now thread th ends; } 

Is there any solution for thread to continue?

+4
source share
2 answers

The problem is that you are trying to run Thread on the Java EE application server. Hand threading is a violation of the Java EE specifications, so you run into problems. on some application servers you cannot even start a separate thread. From the specification:

A bean should not attempt to manage flows. A bean should not attempt to start, stop, pause, or resume a stream or change the priority or name of a stream. A bean should not attempt to manage thread groups. These functions are reserved for the EJB container. Allowing bean enterprises to manage flows will reduce the ability of containers to properly manage the runtime.

If you need to work on a separate thread, you need to use the tools provided by the application server for asynchronous work. some parameters represent the queue in the JMS queue for processing MDB or, possibly, using an asynchronous ejb request (I think that in Java EE 6).

+5
source

If you just want to be sure before returning that the thread has finished, th.join () is the easiest. This method waits until the thread dies.

0
source

All Articles