Handling service.doThreadInvoke()

Hi, there…

Is there any way to know if the service i have run using service.doThreadInvoke() in java service have been finished or still running?
I have read some of thread in this forum, they recommand using ServiceThread which is the output from the service.doThreadInvoke(). But how to use this class?
I have looking about this class in the API doc, but i couldn’t find it. Does any one have documentation about ServiceThread? or any solution of my problem?

Thank’s before…

The service you invoked on its own thread must use pub.sync:notify to communicate its results to the parent (spawning) service. The parent service must use pub.sync:wait if it needs to obtain the output of the child service.

If the wait timeout period expires before results are returned, you don’t have a way to see if the spawned thread is still running or if it died silently. That’s why I recommend taking care to not simply throw exceptions in a spawned service but to use notifyOnSuccess / notifyOnError approach.

If your wait timeout period is too short, the parent thread will give up on the child thread that is still happily doing its thing. You can either make the wait period longer or use a repeat statement. So long as you are always notifying correctly, if your results aren’t back you can assume the service is still running.

Of course, you could create some logic in which a spawned thread uses some type of semaphore to indicate it is running, but that sounds like unnecessary complexity.

HTH,

Mark

In addition to the threading advice from Mark, one should also consider whether a threaded invoke is really, really necessary. If the assumption is that it will make things faster, you’ll want to verify that assumption before committing to the complexity of a multi-threaded solution.

Hi Mark, i have read about usage of pub.sync:notify and pub.sync:wait.
In my case there’s one service as a parent who’s called two other service as a thread. If I use this wait and notify, can I get the an output for each service that I invoke using thread?
Should I call pub.sync:wait twice to get the output for each service?

Can you give me an example about usage of pub.sync:notify and pub.sync:wait in a java service?

Thank’s for the warning Rob, but i have no other solution for my case than invoke the thread.

Sorry if I’am asking to much, cause i’m newbie to webMethods…
Thank’s for all your help…

Yes, you’ll need to make two calls to pub.sync:wait. Each will use a unique key to get the results from the spawned thread. Ideally, you’ll check first for the results of the task that you expect to have the shorter run time, then the longer running one.

Look at the “thread” examples in the WmSamples folder available fror download from Advantage.

Mark

Hi mark, i have tried use wait and notify, and it’s work.
And i have a few another question :smiley:

  • How if I don’t know when the child thread will end?
  • If the notify called earlier than the wait, still the wait method can get the notify message?
  • How long the value of notify will live in server? (For example the thread call notify at 10.44 and the parent just call the wait at 11.00, is the parent still get the notify?)

Thanks again…

The parent thread will not know when the child will end. If you you don’t know which of two child threads will end first then just wait for them in the order that you spawned them.

Yes. The wait will just be used to obtain the results, if the child thread is already completed there will be no pause.

Never tested this and you should never use multi-threading for this type of long-running process. In theory the results should still be available.

Mark

Thank’s Mark for all your help.
It’s really help me… :slight_smile:

seen in 7.1.2/7-1-1_Integration_Server_Java_API_Reference/com/wm/app/b2b/server/ServiceThread.html
"To join this thread and get the results of the service invocation, you call the getIData() method, which returns the pipeline of the service running in this thread when that service is completed. "

AFAIK it is the same in newer versions