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?
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:
Callable<T>
subclassesFuture<T>
s you get back from the executorServiceFuture.get()
on each to block until you have a response, and then process the responses however you wish back on the main threadUse 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With