Skip to content
Advertisement

Parallel invoking list 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. 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),

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? 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)?

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]

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

Advertisement

Answer

I ended up implementing a custom callback handler that mantains a state. One different instance for each future.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement