One, publish a webservice, the code is as follows
Package Com.hoo.service; Public class Asynchronousservice { publicthrows interruptedexception{ // Let the current thread sleep 5 clock, show asynchronous call Thread.Sleep (); return "Done"; }}
Second, the release service, see the previous tutorial, not more than three, RPC mode asynchronous invocation:
Importjava.io.IOException;ImportJavax.xml.namespace.QName;Importorg.apache.axis2.addressing.EndpointReference;Importorg.apache.axis2.client.Options;ImportOrg.apache.axis2.client.async.AxisCallback;ImportOrg.apache.axis2.context.MessageContext;Importorg.apache.axis2.rpc.client.RPCServiceClient; Public classasynchronousserviceclient { Public Static voidMain (string[] args)throwsIOException {String target= "Http://localhost:8080/axis2/services/AsynchronousService"; Rpcserviceclient Client=Newrpcserviceclient (); Options Options=client.getoptions (); Options.setmanagesession (true); EndpointReference EPR=Newendpointreference (target); Options.setto (EPR); QName QName=NewQName ("http://service.hoo.com", "execute"); //specify the method to invoke and pass the parameter data, and set the type of the return value, the first two parameters are not different from the synchronous call, the key is the following parameter, using the Axiscallback classClient.invokenonblocking (QName,NewObject[] {},NewAxiscallback () { Public voidonMessage (Messagecontext ctx) {System.out.println (Ctx.getenvelope ()); //get the return value and printSystem.out.println ("Message:" +Ctx.getenvelope (). Getfirstelement (). Getfirstelement (). Getfirstelement (). GetText ()); } Public voidOnfault (Messagecontext ctx) {} Public voidOnError (Exception ex) {} Public voidOnComplete () {}}); System.out.println ("Client: Calling WebService asynchronously"); //Stop program from exitingSystem.in.read (); }}
Program output:
Client: Calling WebService asynchronously
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><soapenv:envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/ envelope/"><soapenv:body><ns:executeresponse xmlns:ns=" http://service.hoo.com "><ns:return> Done</ns:return></ns:executeresponse></soapenv:body></soapenv:envelope>
Message:done
Four, the stub class asynchronous invocation
First, generate the WSDL file for the WebService, and then generate the Java source code with the WSDL file, as described in http://www.cnblogs.com/hewenwu/p/3860083.html
V. Writing client-side asynchronous calling code
Packagetest;Importjava.io.IOException;Importjava.rmi.RemoteException;ImportOrg.apache.axis2.AxisFault;ImportCom.hoo.service.AsynchronousServiceCallbackHandler;Importcom.hoo.service.AsynchronousServiceStub;ImportCom.hoo.service.Execute;ImportCom.hoo.service.ExecuteResponse; Public classasynchronousserviceclient { Public Static voidMain (string[] args)throwsIOException {asynchronousservicestub stub=Newasynchronousservicestub (); Stub.startexecute (NewExecute (),Newmycallback ()); System.out.println ("Asynchronous Call"); System.in.read (); }}
//You must redefine a class that inherits from the corresponding CallbackHandler and override the Receiveresult method to call in main class extends asynchronousservicecallbackhandler{ @Override publicvoid Receiveresultexecute (executeresponse result) { super. Receiveresultexecute (result); System.out.println (Result.get_return ());} }
Output Result:
Asynchronous invocation
done