File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Start a independent thread from a webservice request

 
Oscar G. Rodriguez
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Oscar G. Rodriguez
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your implementation is in a container then it is discouraged to start your own threads in container code.
 
Oscar G. Rodriguez
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic