KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > userguide > clients > EchoNonBlockingDualClient


1 package userguide.clients;
2
3 import org.apache.axis2.Constants;
4 import org.apache.axis2.addressing.AddressingConstants;
5 import org.apache.axis2.addressing.EndpointReference;
6 import org.apache.axis2.clientapi.AsyncResult;
7 import org.apache.axis2.clientapi.Call;
8 import org.apache.axis2.clientapi.Callback;
9 import org.apache.axis2.engine.AxisFault;
10 import org.apache.axis2.om.OMElement;
11 import org.apache.axis2.om.OMOutput;
12
13 import javax.xml.namespace.QName JavaDoc;
14 import javax.xml.stream.XMLOutputFactory;
15 import javax.xml.stream.XMLStreamException;
16 import java.io.StringWriter JavaDoc;
17
18 /**
19  * Created by IntelliJ IDEA.
20  * User: Jaliya
21  * Date: Jun 4, 2005
22  * Time: 5:30:52 PM
23  */

24 public class EchoNonBlockingDualClient {
25     private static EndpointReference targetEPR = new EndpointReference(AddressingConstants.WSA_TO,
26             "http://127.0.0.1:8080/axis2/services/MyService/echo");
27
28     public static void main(String JavaDoc[] args) {
29         try {
30             OMElement payload = ClientUtil.getEchoOMElement();
31
32             Call call = new Call();
33             call.setTo(targetEPR);
34
35             //The boolean flag informs the axis2 engine to use two separate transport connection
36
//to retrieve the response.
37
call.engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
38             call.setTransportInfo(Constants.TRANSPORT_HTTP, Constants.TRANSPORT_HTTP, true);
39
40             //Callback to handle the response
41
Callback callback = new Callback() {
42                 public void onComplete(AsyncResult result) {
43                     try {
44                         StringWriter JavaDoc writer = new StringWriter JavaDoc();
45                         result.getResponseEnvelope().serializeWithCache(
46                                new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(writer)));
47                         writer.flush();
48
49                         System.out.println(writer.toString());
50
51                     } catch (XMLStreamException e) {
52                         reportError(e);
53                     }
54                 }
55
56                 public void reportError(Exception JavaDoc e) {
57                     e.printStackTrace();
58                 }
59             };
60
61             //Non-Blocking Invocation
62
call.invokeNonBlocking("echo", payload, callback);
63
64             //Wait till the callback receives the response.
65
while (!callback.isComplete()) {
66                 Thread.sleep(1000);
67             }
68             //Need to close the Client Side Listener.
69
call.close();
70
71         } catch (AxisFault axisFault) {
72             axisFault.printStackTrace();
73         } catch (Exception JavaDoc ex) {
74             ex.printStackTrace();
75         }
76
77     }
78 }
79
Popular Tags