Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Different Webservices in parallel from Webapp

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?

like image 983
user1669664 Avatar asked Feb 19 '23 04:02

user1669664


2 Answers

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:

  1. Submit each of these calls to an executorService in Callable<T> subclasses
  2. Collect the Future<T>s you get back from the executorService
  3. 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
like image 57
matt b Avatar answered Mar 06 '23 00:03

matt b


Use an ExecutorService with a thread pool to submit Callables 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 Futures, 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.

like image 35
Frank Pavageau Avatar answered Mar 05 '23 23:03

Frank Pavageau