April 15, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using Axis2 and Java for Asynchronous Web Service Invocation on the Client Side, Page 2

  • February 5, 2010
  • By Deepal Jayasinghe
  • Send Email »
  • More Articles »

Transport-level Asynchronous Support Example

Now let's use full asynchronous support in Axis2, which requires WS-Address support for both the client and server sides. You can engage the addressing module on the server side using any of the available ways for doing that, but in this example, you use the following source code to start Axis2 and engage addressing (in real life, however, you would not use the following code to start Axis2).

1    package org.sample; 
2     
3    import org.apache.axis2.engine.AxisServer; 
4    import org.apache.axis2.context.ConfigurationContext; 
5    import org.apache.axis2.context.ConfigurationContextFactory; 
6    import org.apache.axis2.transport.http.SimpleHTTPServer; 
7    import org.apache.axis2.description.AxisService; 
8    import org.apache.axis2.description.Parameter; 
9    import org.apache.axis2.description.java2wsdl.Java2WSDLConstants; 
10    
11   public class AddressingEnableServer { 
12       public static void main(String[] args) throws Exception{ 
13           ConfigurationContext configurationContext = 
ConfigurationContextFactory.createConfigurationContextFromFileSystem(
null, null); 14 SimpleHTTPServer smt = new SimpleHTTPServer(configurationContext, 8080); 15 AxisService service1 =
AxisService.createService(EchoService.
class.getName(),
configurationContext.getAxisConfiguration()); 16 configurationContext.getAxisConfiguration().engageModule(
"addressing"); 17 configurationContext.getAxisConfiguration().addService(service1); 18 smt.start(); 19 } 20 } 21


Now let's run the code. You will be able to access the service from http://localhost:8080/axis2/services/EchoService.

As previously mentioned, you also need to engage addressing on the client side. You can do that as follows:

1    package org.sample; 
2     
3    import org.apache.axiom.om.OMAbstractFactory; 
4    import org.apache.axiom.om.OMElement; 
5    import org.apache.axiom.om.OMFactory; 
6    import org.apache.axiom.om.OMNamespace; 
7    import org.apache.axis2.addressing.EndpointReference; 
8    import org.apache.axis2.client.Options; 
9    import org.apache.axis2.client.ServiceClient; 
10    
11   public class EchoClient { 
12       public static void main(String[] args) throws Exception { 
13           ServiceClient client = new ServiceClient(); 
14           Options opts = new Options(); 
15           opts.setTo(new EndpointReference("http://localhost:8080/axis2/services/EchoService")); 
16           opts.setAction("urn:echo"); 
17           client.engageModule("addressing"); 
18           opts.setUseSeparateListener(true); 
19           client.setOptions(opts); 
20           client.sendReceiveNonBlocking(createPayLoad(), new MyCallBack()); 
21           System.out.println("send the message"); 
22           while (true) { 
23               Thread.sleep(100); 
24           } 
25       } 
26    
27       public static OMElement createPayLoad() { 
28           OMFactory fac = OMAbstractFactory.getOMFactory(); 
29           OMNamespace omNs = fac.createOMNamespace( 
30                   "http://sample.org", "ns"); 
31           OMElement method = fac.createOMElement("echo", omNs); 
32           OMElement value = fac.createOMElement("value", omNs); 
33           method.addChild(value); 
34           value.setText("Axis2"); 
35           return method; 
36    
37       } 
38   }


As you can see, the source code has three changes:

  1. In line 15, you changed the service endpoint URL.
  2. In line 17, you added a new method to engage the "addressing" module.
  3. The most important change is to Line 18, where you tell Axis2 that you need to invoke this in a transport-level asynchronous manner.

To fully understand this, let's run the sample using TCP monitor. First, change line 15 as follows:

opts.setTo(new EndpointReference("http://localhost:8000/axis2/services/EchoService"));


Now configure TCP monitor as shown below.

TCP Monitor2

Now, when you run your sample you will see that you have the request only in the TCP monitor, and you can see a HTTP acknowledgment in the reply. However, you will see the expected output in the console. So now the question is: how do you get the response?

In the following example, you send the request using one HTTP connection. When sending the request, Axis2 tells the server where to send the reply (using addressing headers).

<wsa:ReplyTo>
            <wsa:Address>http://192.168.1.101:6060/axis2/services/anonService2/</wsa:Address>
  </wsa:ReplyTo>


So, when Axis2 sends the reply, it sends it to the above address.

Blocking Invocation

Invoking a service in a blocking manner is very simple. In this example, you only need to change the sendReceiveNonBlocking method to sendReceive as shown below:

OMElement element = client.sendReceive(createPayLoad());
System.out.println(element);


When you invoke the service, you will see the following output:

<ns:echoResponse xmlns:ns="http://sample.org"><return>Axis2</return></ns:echoResponse>
send the message


One of the main things to note here is that when you invoke in a non-blocking manner, "send the message" prints before the response. With blocking, it prints after the response.

Blocking with Transports Asynchronous

With Axis2, you also can invoke the service in application-level blocking and in transport-level non-blocking. The following source code illustrates how to use the Axis2 client API to invoke a service in this manner. Note that you do not need the while loop after the service call, because your application is blocked until you get the response.

1    package org.sample; 
2     
3    import org.apache.axiom.om.OMAbstractFactory; 
4    import org.apache.axiom.om.OMElement; 
5    import org.apache.axiom.om.OMFactory; 
6    import org.apache.axiom.om.OMNamespace; 
7    import org.apache.axis2.addressing.EndpointReference; 
8    import org.apache.axis2.client.Options; 
9    import org.apache.axis2.client.ServiceClient; 
10    
11   public class EchoClient { 
12       public static void main(String[] args) throws Exception { 
13           ServiceClient client = new ServiceClient(); 
14           Options opts = new Options(); 
15           opts.setTo(new EndpointReference("http://localhost:8000/axis2/services/EchoService")); 
16           opts.setAction("urn:echo"); 
17           client.engageModule("addressing"); 
18           opts.setUseSeparateListener(true); 
19           client.setOptions(opts); 
20           OMElement element = client.sendReceive(createPayLoad()); 
21           System.out.println(element); 
22           System.out.println("send the message"); 
23       } 
24    
25       public static OMElement createPayLoad() { 
26           OMFactory fac = OMAbstractFactory.getOMFactory(); 
27           OMNamespace omNs = fac.createOMNamespace( 
28                   "http://sample.org", "ns"); 
29           OMElement method = fac.createOMElement("echo", omNs); 
30           OMElement value = fac.createOMElement("value", omNs); 
31           method.addChild(value); 
32           value.setText("Axis2"); 
33           return method; 
34    
35       } 
36   }


What Have You Learned?

In this article, you have explored the two types of client-side asynchronous support in Axis2 with hands-on examples. You also learned about blocking invocation and how to use it. Finally, you took in application-level blocking invocation with transport-level non-blocking invocation. To fully understand these invocation types, follow the code samples carefully and run them again on your own.

For Further Reading

About the Author

Deepal Jayasinghe, a computer science PhD student at Georgia Tech, is very interested in SOA and distributed computing, especially web services. Before pursuing his doctorate, Deepal worked at WSO2 for three years. As a key member of the Axis2 project, he participated in its design, development and implementation as well.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel