简体   繁体   English

并行调用列表<futures>异步网络服务客户端</futures>

[英]Parallel invoking list<Futures> async webservice client

I've a webservice client that invokes several webservices with same wsdl on different endpoint, so I decided to make asynchronous calls in a parallel way.我有一个 Web 服务客户端,它在不同的端点上调用多个具有相同 wsdl 的 Web 服务,所以我决定以并行方式进行异步调用。 I generated wsimport async jax-ws client and I decided to use a Future invocation with Async callback (using a single AsyncHandler instance for all the futures operation),我生成了 wsimport async jax-ws 客户端,我决定使用带有异步回调的 Future 调用(对所有期货操作使用单个AsyncHandler 实例),

The question is: is there a way in the handleResponse(Response response) method to know to which of the future in the futures list the response is referred?问题是:handleResponse(Response response) 方法中是否有办法知道响应被引用到期货列表中的哪个未来? Anything that can be done in the request context and propagate to reponse context?任何可以在请求上下文中完成并传播到响应上下文的事情? Should I indeed preserve a state in the AsyncHandler and instatiate a new Handler foreach future operation and store the response in a shared list (maybe a thread safe class member)?我是否应该在 AsyncHandler 中保留 state 并为未来的操作设置一个新的 Handler 并将响应存储在共享列表中(可能是线程安全的 class 成员)?

Is there a way to Propagate Request Context to the Response as in here?有没有办法像这里一样将请求上下文传播到响应? [https://docs.oracle.com/middleware/1213/wls/WSGET/jax-ws-async-roadmap.htm#BABFAFHD][1] [https://docs.oracle.com/middleware/1213/wls/WSGET/jax-ws-async-roadmap.htm#BABFAFHD][1]

Thanks all.谢谢大家。 My snippet:我的片段:

        WsSoapClient ws = new ....;

        List<Future> futures = new ArrayList<>();

        for (String url : urlList) {

            
            
        
            BindingProvider bp = (BindingProvider) ws;
            bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
            

            Future invokeAsync = ws.getAsync("foo", mh);
            
            futures.add(invokeAsync);
            
        }
        
        futures.stream().parallel().forEach((t) -> {
            try {

                t.get(15L, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException ex) {
                
            }
        });
        



class MessageHandler implements AsyncHandler<String> {

        private final List<String> responses;

        public MessageHandler() {
            List<String> rr = new ArrayList<>();
            responses = Collections.synchronizedList(rr);
        }

        @Override
        public void handleResponse(Response<String> response) {
            try {

                String get = response.get();
                responses.add(get);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public List<String> getResponses() {
            return responses;
        }

    }
        
            


  [1]: https://docs.oracle.com/middleware/1213/wls/WSGET/jax-ws-async-roadmap.htm#BABFAFHD

I ended up implementing a custom callback handler that mantains a state.我最终实现了一个包含 state 的自定义回调处理程序。 One different instance for each future.每个未来都有一个不同的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM