2013-10-05 55 views
0

我们有一个asmx web服务。我必须使用WSDL测试客户端。我已经成功实现了客户端异步映射的代码。问题是我无法理解客户端如何向服务器发出多个同时请求。我看到Future接口,但我不明白我如何使用它进行并发呼叫。异步JAX-WS多次调用

private void callAsyncCallback(String encodedString, String key) { 

    DataManipulation service = new DataManipulation(); 

    try { // Call Web Service Operation(async. callback) 
     DataManipulationSoap port = service.getDataManipulationSoap12(); 
     // TODO initialize WS operation arguments here 
     AsyncHandler<GetDataResponse> asyncHandler = 
       new AsyncHandler<GetDataResponse>() { 

        @Override 
        public void handleResponse(Response<GetDataResponse> response) { 
         try { 
          // TODO process asynchronous response here 
          System.out.println("Output at::: " + new Date().toString()); 
          System.out.println("************************Result = " + response.get().getGetDataResult()); 
         } catch (Exception ex) { 
          // TODO handle exception 
         } 
        } 
       }; 
     Future<? extends Object> result = port.getDataAsync(encodedString,key, asyncHandler); 
     while (!result.isDone()) { 
      // do something 
     } 
    } catch (Exception ex) { 
     // TODO handle custom exceptions here 
    } 

} 

我知道我可以在while(!result.isDone())循环做一些事情,但我怎么能在这里再次调用Web服务?

其目的是我必须发送多个文件到Web服务。 WS对这些文件执行一些操作并发回一些结果。我希望客户端同时发送所有文件,以便所需的时间非常短。我试过在代码中多次调用方法callAsyncCallback,但只有当第一次调用返回到客户端时才会进入下一行。

编辑

谁能给我一些指点给ExecutorService的?我已经阅读了一些像invokeAll这样的选项,但我无法将其与JAX-WS相关联。任何帮助将不胜感激。

感谢

回答

0

我强烈建议你总是在所有的代码 的使用ListenableFuture而不是未来它更舒适,这不是你自己的自行车

例子:

ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); 
    ListenableFuture<Explosion> explosion = service.submit(new Callable<Explosion>() { 
     public Explosion call() { 
     return pushBigRedButton(); 
     } 
    }); 
    Futures.addCallback(explosion, new FutureCallback<Explosion>() { 
     // we want this handler to run immediately after we push the big red button! 
     public void onSuccess(Explosion explosion) { 
     walkAwayFrom(explosion); 
     } 
     public void onFailure(Throwable thrown) { 
     battleArchNemesis(); // escaped the explosion! 
     } 
    });