asynchronous invocation of Axis2

Source: Internet
Author: User

This chapter focuses on the asynchronous invocation of the Axis2 interface.

In general, we use the synchronous method (invokeblocking) to invoke the Axis2 interface, and if the called WebService method does not return for a long time, the client will be blocked until the method returns. Using the synchronous method to invoke webservice is straightforward, but when the WebService method takes a long time to return for various reasons, it causes the client program to remain in the waiting state so that the user is intolerable.

Of course, we can easily think of a solution to the problem, which is multithreading. The basic way to solve the problem is to leave the task of accessing webservice to one or more threads, and the main thread is not responsible for accessing webservice. So that even if the WebService method being accessed does not return for a long time, the client can still do other work. We can control this way of accessing webservice through multithreading is called asynchronous access.

Although the direct use of multithreading can be a good solution to this problem, but more cumbersome. Fortunately, the AXIS2 client provides the ability to access webservice asynchronously.
The Rpcserviceclient class provides an invokenonblocking method to access WebService asynchronously. The following combines an instance program description.

Directory structure:

Key code:

 PackageCom.alfred.client;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 classserviceclient { Public Static voidMain (String args[])throwsIOException {SendAxis2 ();}/** * Send Axis2 interface information * * @throws ioexception */Private Static voidSendAxis2 ()throwsIOException {//Call WebService using RPCRpcserviceclient serviceclient =NewRpcserviceclient (); Options options = Serviceclient.getoptions ();//Specify the URL to call WebServiceEndpointReference Targetepr =NewEndpointReference ("Http://127.0.0.1:8080/awyb/services/mySoapService"); Options.setto (TARGETEPR);//Specify the parameter value of the SayHello method, if there are multiple, continue to add to the backobject[] Opaddentryargs =NewObject[] {"Alfred" };//When creating a QName object, the first parameter of the constructor method of the QName class represents the WSDL, the namespace name of the file, which is the targetnamespace attribute value of the <wsdl:definitions> element///The second parameter is the name of the method to invokeQName Opaddentry =NewQName ("http://service.alfred.com", "SayHello"); Serviceclient.invokenonblocking (Opaddentry, Opaddentryargs,NewAxiscallback () { Public voidOnComplete () {} Public voidOnError (Exception arg0) {} Public voidOnfault (Messagecontext arg0) {} Public voidOnMessage (Messagecontext MC) {//Output return valueSystem.out.println (Mc.getenvelope (). Getfirstelement (). Getfirstelement (). Getfirstelement (). GetText ());}); System.out.println ("Asynchronous Call! ");//Block program exitSystem.in.read ();}}
Serviceclient.java
 Package com.alfred.service; import Org.apache.axis2.AxisFault;  Public class Soapservice {publicthrows Axisfault {try {System.out.println ("");  Delay 5 seconds catch (Exception e) {e.printstacktrace ();} return "Hello," + name;}  Public String Getworld () {return 'hello,world';}}
Soapservice.java

The client asynchronously accesses the SayHello of the Axis2, and the console prints information:

You can see that the program does not wait for the interface to return to continue after the operation.

The method used by the asynchronous call is invokenonblocking, as can be seen from the above code, the Invokenonblocking method has three parameters, the first two parameters specify the method to invoke and the method parameters of the relevant information, The last parameter is not the type information of the method return value, but rather an object instance of the class that implements the Org.apache.axis2.client.async.AxisCallback interface. In this example, the Axiscallback interface is implicitly implemented. There are four methods in the Axiscallback interface that need to be implemented, where the OnMessage method is called when the method being called asynchronously returns.

We can also use the-a parameter of the Wsdl2java command to generate a stub class that can be called asynchronously (you need to install AXIS2 First, configure the AXIS2_HOME environment variable). The following command generates the client code (two classes) for both synchronous and asynchronous calls, where-s means that the synchronous calling code is generated, and-a means that the asynchronous calling code is generated.

%axis2_home%\bin\wsdl2java-uri http://127.0.0.1:8080/awyb/services/mysoapservice?wsdl-p client-s-A- o stub

After executing the above command, two classes are generated: The Mysoapservicestub and Mysoapservicecallbackhandler classes, where the Mysoapservicestub class is responsible for synchronizing and calling WebService asynchronously. The Mysoapservicecallbackhandler class is an abstract class and a callback class that, when called with the WebService method asynchronously, if the method returns, The Receiveresultgetname method of the Mysoapservicecallbackhandler class is called. The following is code that uses the Mysoapservicestub class to access WebService asynchronously.

Directory structure:

Key code:

 Package client; Import Client. Mysoapservicestub.getworldresponse; Import Client. Mysoapservicestub.sayhelloresponse;  Public class extends Mysoapservicecallbackhandler {@Overridepublicvoid Receiveresultgetworld (getworldresponse result {//Output Getworld method return result System.out.println (Result.get_return ());} @Overridepublicvoid Receiveresultsayhello (sayhelloresponse result) {//Output SayHello method return result System.out.println (Result.get_return ());}}
Mycallback.java
 Package client; Import Client. Mysoapservicestub.sayhello;  Public class stubclient {publicstaticvoidthrowsnew mysoapservicestub ();  Asynchronously calls WebServicenew SayHello (); Sayhello.setname ("Alfredothernew Mycallback ()); System.out.println (" Asynchronous call! "); System.in.read ();}}
Stubclient.java

Mysoapservicecallbackhandler and mysoapservicestub are auto-generated code.

After accessing the SayHello console printing results:

asynchronous invocation of Axis2

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.