Axis2 release of WebService (4)-webservice Asynchronous call

Source: Internet
Author: User
Tags wsdl

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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.