从Webapp并行调用不同的Web服务 [英] Calling Different Webservices in parallel from Webapp

查看:105
本文介绍了从Webapp并行调用不同的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个stipes(java)网络应用程序需要从一个方法进行大约15个不同的webserivce调用。例如:
...

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.

避免线程安全问题的一个好方法是使用 executorService 并提交子类 Callable< T> (到提交(可调用) invokeAll(Collection< Callable>) methods)并让Callables返回响应值。这样,您的初始方法可以简单地处理每个调用的返回值,并选择在会话中设置响应或更新其他对象,而不是在另一个线程中发生此工作。

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.

基本算法:


  1. 将这些调用提交给 Callable< T> 子类

  2. 收集您从executorService返回的 Future< T>

  3. 在每个要阻止之前调用 Future.get(),直到您有响应,然后处理响应,但是您希望回到主线程上

  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

这篇关于从Webapp并行调用不同的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆