KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > userguide > clients > EchoNonBlockingClient


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.stream.XMLOutputFactory;
14 import javax.xml.stream.XMLStreamException;
15 import java.io.StringWriter JavaDoc;
16
17 /**
18  * Created by IntelliJ IDEA.
19  * User: Jaliya
20  * Date: Jun 4, 2005
21  * Time: 5:08:44 PM
22  */

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