01 package com.flat502.rox.demo;
02
03 import java.net.URL;
04
05 import com.flat502.rox.client.AsynchronousResponseHandler;
06 import com.flat502.rox.client.RpcResponseContext;
07 import com.flat502.rox.client.XmlRpcClient;
08 import com.flat502.rox.marshal.RpcCall;
09 import com.flat502.rox.marshal.RpcResponse;
10
11 /**
12 * A demo asynchronous client illustrating the
13 * {@link com.flat502.rox.client.XmlRpcClient#execute(String, Object[], Class, AsynchronousResponseHandler)}
14 * method and the {@link com.flat502.rox.client.AsynchronousResponseHandler} interface.
15 */
16 public class AsyncClientDemo implements AsynchronousResponseHandler {
17 private static boolean responseReceived = false;
18
19 public void handleResponse(RpcCall call, RpcResponse response, RpcResponseContext context) {
20 TimeInfo timeinfo = (TimeInfo) response.getReturnValue();
21 System.out.println("getDate() returned with");
22 System.out.println(" today is " + timeinfo.today);
23 System.out.println(" info: " + timeinfo.info);
24
25 responseReceived = true;
26 }
27
28 public void handleException(RpcCall call, Throwable e, RpcResponseContext context) {
29 e.printStackTrace();
30
31 responseReceived = true;
32 }
33
34 /**
35 * Call each of the exposed methods on the remote demo server.
36 * @param args
37 * A list of parameters. Only the first is used and if
38 * present must be the URL of the remote server. This
39 * defaults to <code>http://localhost:8080/</code> if
40 * not specified.
41 */
42 public static void main(String[] args) {
43 try {
44 String url = "http://localhost:8080/";
45
46 if (args != null && args.length > 0) {
47 url = args[0];
48 }
49 System.out.println("Connecting to " + url);
50
51 XmlRpcClient client = new XmlRpcClient(new URL(url));
52 client
53 .execute("example.getDate", null, TimeInfo.class, new AsyncClientDemo());
54
55 // XmlRpcClient uses daemon threads for it's background tasks.
56 // This means we need to wait for the response before letting
57 // the main thread terminuate otherwise the entire JVM may exit
58 // before we see the response.
59 while(!responseReceived) {
60 Thread.yield();
61 }
62 } catch (Exception e) {
63 e.printStackTrace();
64 }
65 }
66 }
|