• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Start a independent thread from a webservice request

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic