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:
- In line 15, you changed the service endpoint URL.
- In line 17, you added a new method to engage the “addressing” module.
- 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.
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
- Axis2 Client API
- Axis2 Deployment Options
- XML Manipulation with Apache AXIOM
- Going Beyond XML Manipulation with Apache AXIOM
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.