When it comes to Asynchronization, you must first synchronize the following. Synchronization means that the code is executed in order. If the current Code request does not return the result normally, the subsequent Code cannot run. Asynchronous is just different from this. After code is run, the subsequent code will continue to run regardless of whether the current request returns results.
I will not go into details about Asynchronization here. If you are interested, you can check this information online.
1. Write the server code.
Code package com. hoo. service;/*** <B> function: </B> asynchronous WebService server code * @ author hoojo * @ createDate 2011-3-14 08:16:59 * @ file AsynchronousService. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class AsynchronousService {public String execute () {System. out. println ("executing this code ...... "); // After a delay of 5 seconds, the returned result is try {Thread. sleep (5000);} catch (InterruptedException e) {e. printStackTrace () ;}return "finished ";}}
2, services. xml file, create the aar file, and then copy the aar file to the [tomcat-home] \ webapps \ axis2 \ WEB-INF \ services Directory
Services. xml
Code <service name = "AsyncService"> <description> AsyncService </description> <parameter name = "ServiceClass"> com. hoo. service. asynchronousService </parameter> <messageReceivers> <messageReceiver mep = "http://www.w3.org/2004/08/wsdl/in-out" class = "org. apache. axis2.rpc. receivers. RPCMessageReceiver "/> <messageReceiver mep =" http://www.w3.org/2004/08/wsdl/in-only "class =" org. apache. axis2.rpc. receivers. RPCInOnlyMessageReceiver "/> </messageReceivers> </service>
3. Compile the client test code
Code package com. hoo. service; import java. io. IOException; 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;/*** <B> function: </B> asynchronous WebService client code * @ author hoojo * @ createDate 2011 -3-14 am 09:00:03 * @ file AsynchronousServiceClient. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class AsynchronousServiceClient {public static void main (String [] args) throws IOException {String target = "http: // localhost: 8080/axis2/services/AsyncService? Wsdl "; RPCServiceClient client = new RPCServiceClient (); Options options = client. getOptions (); options. setManageSession (true); EndpointReference epr = new EndpointReference (target); options. setTo (epr); QName qname = new QName ("http://service.hoo.com", "execute"); // specify the method for calling and passing parameter data, and set the type of the returned value client. invokeNonBlocking (qname, new Object [] {}, new AxisCallback () {public void onMessage (MessageContext ctx) {System. out. println (ctx. getEnvelope (); System. out. println ("Message:" + ctx. getEnvelope (). getFirstElement (). getFirstElement (). getFirstElement (). getText ();} public void onFault (MessageContext ctx) {} public void onError (Exception ex) {} public void onComplete () {}}); System. out. println ("Asynchronous WebService"); // prevents the program from exiting the System. in. read ();}}
The above is the code for Asynchronously calling WebService. The call method is client. invokeNonBlocking. This method has three parameters: one is the method signature to be executed, the other is the parameter to be executed, and the other is the asynchronous callback. Here, the AxiaCallback interface is implemented implicitly.
Note that you must run the program in Debug mode when running the program.