Java 异步JAX-WS多调用
我们有一个ASMXWeb服务。我必须使用WSDL测试客户机。我已经成功地实现了客户端异步映射的代码。问题是,我无法理解客户机如何同时向服务器发出多个请求。我已经看到了Java 异步JAX-WS多调用,java,web-services,jax-ws,executorservice,callable,Java,Web Services,Jax Ws,Executorservice,Callable,我们有一个ASMXWeb服务。我必须使用WSDL测试客户机。我已经成功地实现了客户端异步映射的代码。问题是,我无法理解客户机如何同时向服务器发出多个请求。我已经看到了Future接口,但我不明白如何使用它进行并发调用 private void callAsyncCallback(String encodedString, String key) { DataManipulation service = new DataManipulation(); try { // Call
Future
接口,但我不明白如何使用它进行并发调用
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
}
}
private void callAsyncCallback(字符串编码字符串,字符串键){
DataManipulation服务=新建DataManipulation();
尝试{//调用Web服务操作(async.callback)
DataOperationsOAP端口=服务。GetDataOperationsOAP12();
//TODO在此处初始化WS操作参数
异步处理程序异步处理程序=
新的AsyncHandler(){
@凌驾
公共无效处理响应(响应){
试一试{
//TODO在此处理异步响应
System.out.println(“输出时间::”+新日期().toString());
System.out.println(“************************Result=“+response.get().getDataResult());
}捕获(例外情况除外){
//TODO句柄异常
}
}
};
Future我强烈建议您在所有代码中始终使用Future而不是Future
它更舒适,而且不是你自己的自行车
例如:
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!
}
});
listengExecutorService=MoreExecutors.listengDecorator(Executors.newFixedThreadPool(10));
ListenableFuture explosion=service.submit(new Callable()){
公众爆炸电话(){
返回pushBigRedButton();
}
});
Futures.addCallback(爆炸,新未来回调(){
//我们希望这个处理程序在按下红色大按钮后立即运行!
成功时的公共空间(爆炸){
从(爆炸)中逃走;
}
失败时公共无效(可丢弃){
战神报应();//逃过了爆炸!
}
});