1. 程式人生 > >axis2系列之非同步呼叫

axis2系列之非同步呼叫

public class AsyncCallServiceClient {

	public static void main(String[] args) {
		try {
			RPCServiceClient client = new RPCServiceClient();
			Options options = client.getOptions();
			EndpointReference targetEPR = new EndpointReference("http://localhost:8080/mywebservice/services/asyncCallService");
			options.setTo(targetEPR);
			
			//呼叫的方法
			final QName qName = new QName("http://test.com","getName");
			//傳參
			Object[] oArgs = new Object[]{};
			//非同步呼叫
			client.invokeNonBlocking(qName, oArgs, new AxisCallback() {
				
				//非同步呼叫時執行的方法
				/**
				 * 返回xml格式的資料:
				 * <?xml version="1.0" encoding="utf-8"?>
				 * <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
				 *		<soapenv:Header></soapenv:Header>
				 *		<soapenv:Body>
				 *			<ns:getNameResponse xmlns:ns="http://test.com">
				 *				<ns:return>非同步呼叫返回</ns:return>
				 *			</ns:getNameResponse>
				 *		</soapenv:Body>
				 *  </soapenv:Envelope>
				 * 
				 * 通過解析該xml資料,取得返回值
				 */
				@Override
				public void onMessage(MessageContext context) {
					Iterator it = context.getEnvelope().getChildElements();
					while(it.hasNext()){
						Object obj = it.next();
						if(obj instanceof OMElement){
							OMElement ome = (OMElement)obj;
							OMElement firstEle = ome.getFirstElement();
							if(firstEle != null && !firstEle.equals("")){
								firstEle = firstEle.getFirstElement();
								System.out.println("返回值資訊:"+firstEle.getText());
							}
						}
					}
				}
				
				@Override
				public void onFault(MessageContext context) {
				}
				
				@Override
				public void onError(Exception exception) {
				}
				
				@Override
				public void onComplete() {
				}
			});
			
			System.out.println("非同步呼叫!!!!!!");
			//阻止程式退出
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}