问题
We've got a stipes (java) web-app that needs to make about 15 different webserivce calls all from one method. For example: ...
public Resolution userProfile()
{
serviceOneCall();
serviceTwoCall();
serviceThreeCall();
serviceFourCall();
....
serviceTenCall();
return new RedirectResolution("profiel.jsp");
}
All of these can be called in parallel and are not dependent on each other. The one thing that most all of these calls are doing is putting data in the session, and one or two may put data into the same object that is in the session, so thread safety is probably a concern there.
Can anyone suggest a good way of calling all of these concurrently?
回答1:
All solutions to doing this work in parallel is going to involve spawning new threads or submitting jobs to a thread pool for the remote network calls to happen to.
A good way to avoid thread safety problems is to use an executorService
and submit subclasses of Callable<T>
(to either the submit(Callable)
or invokeAll(Collection<Callable>)
methods) and have the Callables return the response value. This way your initial method can simply handle the return values of each call and choose to set the responses in the session or update other objects, rather than this work occurring in another thread.
So basic algorithm:
- Submit each of these calls to an executorService in
Callable<T>
subclasses - Collect the
Future<T>
s you get back from the executorService - Call
Future.get()
on each to block until you have a response, and then process the responses however you wish back on the main thread
回答2:
Use an ExecutorService
with a thread pool to submit Callable
s for each WS you need to call, and synchronize on the object which is updated when there is a chance of concurrent modification.
You may want to use Guava's concurrent extensions for an easier management of the Future
s, using for example Futures.allAsList()
which will convert a List<Future<T>>
into a Future<List<T>>
, so you only have one get()
to do to wait for all the answers.
回答3:
for (i = 0; i <= numOfServiceCalls; i++) {
new Thread(new Runnable() {
switch(i) {
case 1 : serviceOneCall();
break();
case 2 : serviceTwoCall();
break();
// Keep going with as many cases as you have.
}
});
}
来源:https://stackoverflow.com/questions/12413708/calling-different-webservices-in-parallel-from-webapp