日期:2014-05-16  浏览次数:20767 次

WebService axis2异步调用无返回值
service代码:
package yibudiaoyong;

public class YiBuDiaoYongService {
public String getName(){
try { 
System.out.println("getName方法正在执行"); 
// 延迟5秒 
Thread.sleep(2000);
} catch (Exception e) {


return "火星";
}
}


service.xml配置文件代码:
<service name="yibudiaoyongService"> 
<description> 异步调用演示 </description> 
<parameter name="ServiceClass">
yibudiaoyong.YiBuDiaoYongService
</parameter>
<messageReceivers> 
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers> 
</service>



异步调用程序:
package yibudiaoyong;

import javax.xml.namespace.QName;


import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.async.AxisCallback;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.rpc.client.RPCServiceClient;


public class RPCAsyncClient {
public static void main(String[] args) throws Exception {
RPCServiceClient serviceClient =new RPCServiceClient();
Options options=serviceClient.getOptions();
EndpointReference targetERP=new EndpointReference("http://localhost:8080/axis2/services/yibudiaoyongService");
options.setTo(targetERP);
Object[] opAddEntryArgs=new Object[]{};
QName opAddEntry=new QName("http://yibudiaoyong","getName");
serviceClient.invokeNonBlocking(opAddEntry, opAddEntryArgs, new AxisCallback(){
public void onComplete() {
// TODO Auto-generated method stub
}

public void onError(Exception arg0) {
// TODO Auto-generated method stub
}

public void onFault(MessageContext arg0) {
// TODO Auto-generated method stub
}

public void onMessage(MessageContext mc) {
System.out.println("111111111111111111");
System.out.println(mc.getEnvelope().getFirstElement().getFirstElement().getFirstElement().getText());
}

});
System.out.println("异步调用!");
// 阻止程序退出 
Class[] classes = new Class[] {String.class};
//System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
System.in.read();
}
}

异步调用没返回值,只输出异步调用!,我再加一个同步调用,两个就都返回值了,这是为什么?为什么直接异步调用没返回值?
------解决方案--------------------
你还在么 - -  我最近也在弄AXIS2 的webService发布和调用。


------解决方案--------------------
异步调用,本来就没有返回值。  它是在请求响应以后,去回调你的onMessage方法。 我现在就纠结于, onMessage 跟 System.in.Read()的关系,(不写不掉回调方法)    另外你说的-再加一个同步调用,两个就都返回值了。什么意思?