首页 / 软件开发 / JAVA / 关于在weblogic中异步调用webservice
关于在weblogic中异步调用webservice2010-12-26 BlogJava 走走停停又三年这几天碰到个问题:在weblogic中调用async webservice,如果客户端不等待结果(比如服务器端因为某些原因,web service需要执行很长时间),直接退出的话,weblogic server是否保存调用结果,结果保存多长时间?如果这样的异常客户端很多,对服务器有什么负面影响,比如连接资源、内存开销等。首先我们先阐述一下异步的概念 在weblogic webservice中,有两处异步的概念:1:Synchronous request-response (the default behavior) means that every time a client application invokes a Web Service operation, it receives a SOAP response, even if the method that Choosing RPC-Oriented or Document-Oriented Web Services Programming WebLogic Web Services 4-3 implements the operation returns void. Asynchronous one-way means that the client never receives a SOAP response, even a fault or exception.默认情况下,weblogic webservice是请求-应答模式的,即客户端会block当前线程,直到server端处理完该请求(即使该请求没有任何返回值,void)。当 web service不返回结果,客户端只是提交请求,不需要知道执行结果的时候,可以采用异步单向模式。这种情况下,客户端线程为非阻塞的,它只负责提交请求,而不需要返回结果。定义这样的异步web service时,需要遵循如下的两个原则:1.1:The back-end component that implements the operation must explicitly return void.1.2:You cannot specify out or in-out parameters to the operation, you can only specify inparameters.2:This section describes how to invoke an operation asynchronously. In this context, asynchronously means you invoke an operation and then optionally get the results of the invoke in a later step.这种情况下虽然也是异步调用的,但这种调用方式客户端需要返回值。需要返回结果,但客户端又不乐意阻塞在服务器端请求处理上(可能服务器端处理该请求需要很长时间)。客户端希望继续执行它的其他业务逻辑,需要执行结果的时候,我在过来取这个结果。这样可以提高客户端的响应速度。这篇文章,我们主要看看2这种情况。2.1: web service开发开发web service不存在任何区别,但在build client jar的时候,需要在调用clientgen的时候加上generateAsyncMethods = true, 这样clientgen生成的JAX-RPC stub中会多出两个方法,如下:FutureResult startMethod (params, AsyncInfo asyncInfo);result endMethod (FutureResult futureResult);其中:Method对应于web service中的方法名,如sayHello---->startSayHello(params, AsyncInfo asyncInfo)。这两个方法就是我们客户端代码中异步调用的时候需要的。2.2:客户端代码客户端代码有两种写法,一种是客户端线程主动调用FutuerResult.isCompleted()来检查web service请求是否执行完成,另一种方式是通过Listenter来处理服务器端的返回结果。//client thread checking1 public void runUnblock(){
2 initializeEnv();
3 try{
4 System.out.println(port.getClass().getName());
5 FutureResult result = port.startSayHello(3, "test", null);
6 //you other business logic here
7 if(result.isCompleted())
8 {
9 String ret = port.endSayHello(result);
10 System.out.println("result from remote HelloWorld web service: ");
11 System.out.println(ret);
12 }
13 }catch(Exception e){
14 e.printStackTrace();
15 }
16 }
17
18 public void initializeEnv(){
19 try{
20 helloService = new HelloWorld_Impl();
21 port = helloService.getHelloWorldPort();
22 }catch(Exception e){
23 e.printStackTrace();
24 }
25 }