wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Start a independent thread from a webservice request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Start a independent thread from a webservice request" Watch "Start a independent thread from a webservice request" New topic
Author

Start a independent thread from a webservice request

Oscar G. Rodriguez
Greenhorn

Joined: Nov 05, 2011
Posts: 15
Hi,

I have a JAX-WS webservice that has to start a long process that is not required for the webservice response.

I tried to create a new Runnable thread and start it from the webservice method, but it waits for the runnable thread to finish before send the response.

This is an example, where Hilo is a Runnable class
@WebService
public class Calculator implements CalculatorWs {

public int sum(int add1, int add2) {

Hilo h = new Hilo();
h.run();
return add1 + add2;
}

public int multiply(int mul1, int mul2) {
return mul1 * mul2;
}
}


I need to send the response before Hilo finishes. What am I missing?

Thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

You don't run a Runnable using the run() method when you want it to run in a new Thread - the run() method is just a normal method with no special meaning. You need to give the Runnable to a Thread object to execute then start() the Thread:


Steve
Oscar G. Rodriguez
Greenhorn

Joined: Nov 05, 2011
Posts: 15
Ouch!!!

Thank you Steve, the previous code was a test to check the method, I was creating the Thread with the Runnable object, but I was calling thread run mehod instead start one !!!

You opened my eyes
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
If your implementation is in a container then it is discouraged to start your own threads in container code.
Oscar G. Rodriguez
Greenhorn

Joined: Nov 05, 2011
Posts: 15
I know, but how I can force the container to create this thread?

I receive a webservice request and after a delay and some internal process, I send a webservice request to other server. This is the function of the new thread.

I think about to use a queue (activeMQ), but I donĀ“t want to add it only for this process.

Any idea to avoid my forced thread?
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
If you are using EE6 you can use Asynchronous EJBs. You can also use asynchronous webservices.
Some containers also offer services for background tasks execution in a container managed way so that depends on your container.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Start a independent thread from a webservice request
 
Similar Threads
sorting
Gradebook
Concurrency with Threads
Facing problem to set classpath
Sierpinski Triangle Question (very quick)