【问题标题】:How to call multiple soap calls parallel in java-spring application如何在java-spring应用程序中并行调用多个soap调用
【发布时间】:2019-10-29 04:48:57
【问题描述】:

我想让我的soap请求调用在java中并行。 我是多线程的新手,没有办法做到这一点。

public List<TB600Model.Response> getTableDesc(final List<TB600Model.Request> requests)  
{
    List<TB600Model.Response> responses = new ArrayList<>();
    for (TB600Model.Request request : requests) 
    {
        responses.add(
            this.modifyDescription(
                this.getDescription(
                    request.getSite()
                    ,request.getDescType()
                    ,request.getKeyData()
                    ,request.getEffMdy()
                )
                ,request.getDescType()
                ,request.getKeyData()
            )
        );
    }
    return responses;
}

enter image description here

【问题讨论】:

  • 尝试编辑您的问题并以正确的形式发送,为您的代码使用“代码标签”
  • @Mehdi 更新了代码,知道如何让调用并行运行,这会影响我的应用程序的性能。
  • 这段代码还没有发送任何soap调用,它只是将响应信息保存在一个数组列表中。把发送部分,我告诉你怎么写并行
  • 代码getDescription中的部分是调用客户端。
  • 添加了代码图片

标签: java spring multithreading soap


【解决方案1】:

对于 Java 8+ 尝试改用此方法:

requests.parallelStream().forEach(request -> {...});

将代码放入 response.add 而不是 ...

对于早期版本:

ExecutorService es = Executors.newFixedThreadPool(10);
List<Response> responses = new ArrayList<>();
for(Request request:  requests){
    Future<Response> future = es.submit(new Callable<Response>() {
        @Override
        public Response call() throws Exception {
           //create your response here and return
        }
    });

    try {
        responses.add(future.get());
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我使用的是 1.7 版本,它不接受 lambda 表达式。
  • 下次请先在问题中提及所有内容。试试上面的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
相关资源
最近更新 更多