KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > samples > callback > CallbackClient


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.samples.callback;
8
9 import org.jboss.remoting.Client;
10 import org.jboss.remoting.HandleCallbackException;
11 import org.jboss.remoting.InvocationRequest;
12 import org.jboss.remoting.InvokerCallbackHandler;
13 import org.jboss.remoting.InvokerLocator;
14 import org.jboss.remoting.Callback;
15 import org.jboss.remoting.transport.Connector;
16
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 /**
21  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
22  */

23 public class CallbackClient
24 {
25    // Default locator values
26
private static String JavaDoc transport = "socket";
27    private static String JavaDoc host = "localhost";
28    private static int port = 5400;
29
30    private Client remotingClient;
31
32    public void createRemotingClient(String JavaDoc locatorURI) throws Exception JavaDoc
33    {
34       InvokerLocator locator = new InvokerLocator(locatorURI);
35       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
36
37       // This could have been new Client(locator), but want to show that subsystem param is null
38
// Could have also been new Client(locator, "sample");
39
remotingClient = new Client(locator, null);
40
41    }
42
43    public void makeInvocation() throws Throwable JavaDoc
44    {
45       Object JavaDoc response = remotingClient.invoke("Do something", null);
46       System.out.println("Invocation response: " + response);
47    }
48
49    public void testPullCallback() throws Throwable JavaDoc
50    {
51       CallbackHandler callbackHandler = new CallbackHandler();
52       // by passing only the callback handler, will indicate pull callbacks
53
remotingClient.addListener(callbackHandler);
54       // now make invocation on server, which should cause a callback to happen
55
makeInvocation();
56
57       List JavaDoc callbacks = remotingClient.getCallbacks();
58       Iterator JavaDoc itr = callbacks.iterator();
59       while(itr.hasNext())
60       {
61          Object JavaDoc obj = itr.next();
62          if(obj instanceof Callback)
63          {
64             System.out.println("Callback value = " + ((Callback)obj).getCallbackObject());
65          }
66          else
67          {
68             System.out.println("Callback value = " + obj);
69          }
70       }
71
72       // remove callback handler from server
73
remotingClient.removeListener(callbackHandler);
74    }
75
76    public void testPushCallback() throws Throwable JavaDoc
77    {
78       // Need to create remoting server to receive callbacks.
79

80       // Using loctor with port value one higher than the target server
81
String JavaDoc callbackLocatorURI = transport + "://" + host + ":" + (port + 1);
82       InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
83
84       // call to create remoting server to
85
// receive client callbacks.
86
setupServer(callbackLocator);
87
88       CallbackHandler callbackHandler = new CallbackHandler();
89       // Callback handle object will be passed back as part of the callback object
90
String JavaDoc callbackHandleObject = "myCallbackHandleObject";
91       // by passing only the callback handler, will indicate pull callbacks
92
remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject);
93       // now make invocation on server, which should cause a callback to happen
94
makeInvocation();
95
96       // need to wait for brief moment so server can callback
97
Thread.sleep(2000);
98
99       // remove callback handler from server
100
remotingClient.removeListener(callbackHandler);
101    }
102
103    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
104    {
105       System.out.println("Starting remoting server with locator uri of: " + locator);
106       Connector connector = new Connector();
107       connector.setInvokerLocator(locator.getLocatorURI());
108       connector.start();
109
110       CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
111       // first parameter is sub-system name. can be any String value.
112
connector.addInvocationHandler("sample", invocationHandler);
113    }
114
115
116    /**
117     * Can pass transport and port to be used as parameters.
118     * Valid transports are 'rmi' and 'socket'.
119     *
120     * @param args
121     */

122    public static void main(String JavaDoc[] args)
123    {
124       if(args != null && args.length == 2)
125       {
126          transport = args[0];
127          port = Integer.parseInt(args[1]);
128       }
129       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
130       CallbackClient client = new CallbackClient();
131       try
132       {
133          client.createRemotingClient(locatorURI);
134
135          // make call to test pull callbacks
136
client.testPullCallback();
137
138          // make call to test push callbacks
139
client.testPushCallback();
140       }
141       catch(Throwable JavaDoc e)
142       {
143          e.printStackTrace();
144       }
145    }
146
147    public class CallbackHandler implements InvokerCallbackHandler
148    {
149       /**
150        * Will take the callback message and send back to client.
151        * If client locator is null, will store them till client polls to get them.
152        *
153        * @param invocation
154        * @throws org.jboss.remoting.HandleCallbackException
155        *
156        */

157       public void handleCallback(InvocationRequest invocation) throws HandleCallbackException
158       {
159          if(invocation instanceof Callback)
160          {
161             Callback callback = (Callback)invocation;
162             System.out.println("Received callback value of: " + callback.getCallbackObject());
163             System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
164             System.out.println("Received callback server invoker of: " + callback.getServerLocator());
165          }
166          else
167          {
168             System.out.println("Received callback value of: " + invocation.getParameter());
169          }
170       }
171    }
172
173 }
Popular Tags