user1084509
user1084509

Reputation: 1902

Invoke Parallel Process from WebService in Java

I have devoloped a WebService (WS1) in Java which is deployed in a Tomcat Server. If certain conditions are met, WS1 needs to invoke another WebService (WS2) to run in parallel. Is this possible? or does the WS1 has to wait for the response of WS2 to keep running. The point of this is not delaying the response of WS1 as it is not dependant of WS2. However, WS2 trigger IS dependant of WS1 and that's how my problem starts.

My hipothetical solution to this issue is to create a separate servlet which invokes WS2 without delaying WS1. How can I implement it or can anyone think of a better solution?

Upvotes: 0

Views: 1676

Answers (3)

Michael
Michael

Reputation: 35351

You would have to invoke WS2 in an asynchronous manor. This means that the web service call is handled in a separate thread and does not impact the execution of the parent thread (WS1).

The opposite of an asynchronous call is a synchronous call, which blocks the current thread until a response is received.

Upvotes: 0

Sebi
Sebi

Reputation: 9083

It depends what you want to achieve. If you just want to kickoff the second WS, you could create a new local thread and do the call. The second WS might even support asynchronous calls so that you are not blocked after calling it. Creating a new thread would work as follows.

First, you need a class for this thread. This class would take care of communicating with WS2:

class ThreadForWS2 extends Thread {
  public void run() {
    // invoke WS2 here
  }
}

In your WS1 request handler, you just need to kick-off this thread, e.g.:

Thread threadForWS2 = new ThreadForWS2();
threadForWS2.start();

That's it, but of course there are many ways in Java to create another thread. That's up to you to investigate the best possible solution.

Upvotes: 3

Yair Zaslavsky
Yair Zaslavsky

Reputation: 4137

Of course it can be done.
I would like to know if you're tallking about SOAP or RESTFul web service
In general in both technologies, you can have a method invoked in WS1, that as a result, communicates with WS2 (i.e - sends HTTP request with REST data to a resource URL or send a SOAP envelope)
If you mean by running in parallel to open a thread an execute the invocation code of the method in WS2 - this is possible as well
In addition, from what I saw servlet API 3.0 supports asynchronous calls, maybe this can help you as well

Upvotes: 0

Related Questions