KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > callback > CallbackTestClient


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.test.remoting.callback;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import org.jboss.logging.Logger;
14 import org.jboss.remoting.Client;
15 import org.jboss.remoting.InvocationRequest;
16 import org.jboss.remoting.InvokerLocator;
17 import org.jboss.remoting.ServerInvocationHandler;
18 import org.jboss.remoting.ServerInvoker;
19 import org.jboss.remoting.callback.Callback;
20 import org.jboss.remoting.callback.HandleCallbackException;
21 import org.jboss.remoting.callback.InvokerCallbackHandler;
22 import org.jboss.remoting.transport.Connector;
23
24 import junit.framework.TestCase;
25
26 /**
27  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
28  */

29 public class CallbackTestClient extends TestCase
30 {
31    // Default locator values
32
private static String JavaDoc transport = "socket";
33    private static String JavaDoc host = "localhost";
34    private static int port = 5500;
35
36    private String JavaDoc locatorURI = transport + "://" + host + ":" + port;
37
38    private Client remotingClient;
39    private Connector connector;
40
41    private static final Logger log = Logger.getLogger(CallbackTestClient.class);
42
43    public static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
44
45
46    private void init() throws Exception JavaDoc
47    {
48       createRemotingClient();
49    }
50
51    public void createRemotingClient() throws Exception JavaDoc
52    {
53       InvokerLocator locator = new InvokerLocator(locatorURI);
54       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
55
56       // This could have been new Client(locator), but want to show that subsystem param is null
57
// Could have also been new Client(locator, "sample");
58
remotingClient = new Client(locator, null);
59
60    }
61
62    public void makeInvocation() throws Throwable JavaDoc
63    {
64       Object JavaDoc response = remotingClient.invoke("Do something", null);
65       System.out.println("Invocation response: " + response);
66    }
67
68    public void setUp() throws Exception JavaDoc
69    {
70       init();
71    }
72
73    public void tearDown() throws Exception JavaDoc
74    {
75       if(remotingClient != null)
76       {
77          remotingClient.disconnect();
78       }
79       if(connector != null)
80       {
81          connector.stop();
82          connector.destroy();
83       }
84    }
85
86    public void testPullCallback() throws Throwable JavaDoc
87    {
88       CallbackHandler callbackHandler = new CallbackHandler();
89       try
90       {
91          // by passing only the callback handler, will indicate pull callbacks
92
remotingClient.addListener(callbackHandler);
93          // now make invocation on server, which should cause a callback to happen
94
makeInvocation();
95
96          List JavaDoc callbacks = remotingClient.getCallbacks(callbackHandler);
97          Iterator JavaDoc itr = callbacks.iterator();
98          while(itr.hasNext())
99          {
100             Object JavaDoc obj = itr.next();
101             log.info("testPullCallback - Callback object should have been " +
102                      CallbackTestServer.CALLBACK_VALUE + " and was " +
103                      (obj == null ? null : ((Callback) obj).getCallbackObject()));
104             if(obj instanceof Callback)
105             {
106                assertEquals("Callback object is NOT same.", CallbackTestServer.CALLBACK_VALUE, ((Callback) obj).getCallbackObject());
107             }
108             else
109             {
110                assertTrue("Callback object is NOT of type Callback.", false);
111             }
112          }
113       }
114       finally
115       {
116          if(remotingClient != null)
117          {
118             // remove callback handler from server
119
remotingClient.removeListener(callbackHandler);
120          }
121       }
122    }
123
124    public void testPushCallback() throws Throwable JavaDoc
125    {
126
127       CallbackHandler callbackHandler = new CallbackHandler();
128
129       try
130       {
131          // Using loctor with port value one higher than the target server
132
String JavaDoc callbackLocatorURI = transport + "://" + host + ":" + (port + 5);
133          InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
134
135          log.info("testPushCallback - Setting up server.");
136          // call to create remoting server to
137
// receive client callbacks.
138
setupServer(callbackLocator);
139
140          // Callback handle object will be passed back as part of the callback object
141
String JavaDoc callbackHandleObject = "myCallbackHandleObject";
142          log.info("testPushCallback - adding listener.");
143          // by passing only the callback handler, will indicate pull callbacks
144
remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject);
145          log.info("testPushCallback - make invocation");
146          // now make invocation on server, which should cause a callback to happen
147
makeInvocation();
148
149          // need to wait for brief moment so server can callback
150
Thread.sleep(5000);
151
152          Callback callback = callbackHandler.getCallback();
153          log.info("testPushCallback - Callback returned was " + callback);
154          assertNotNull("Callback returned was null.", callback);
155
156          Object JavaDoc callbackObj = callback.getCallbackObject();
157          log.info("testPushCallback - Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", and was " + callbackObj);
158          assertEquals("Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", but was " + callbackObj,
159                       CallbackTestServer.CALLBACK_VALUE, callbackObj);
160          Object JavaDoc callbackHandleObj = callback.getCallbackHandleObject();
161          log.info("testPushCallback - Callback handle object should have been " + callbackHandleObject + ", and was " + callbackHandleObj);
162          assertEquals("Callback handle object should have been " + callbackHandleObject + ", but was " + callbackHandleObj,
163                       callbackHandleObject, callbackHandleObj);
164          InvokerLocator serverLoc = callback.getServerLocator();
165          log.info("testPushCallback - Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
166                   ", and was " + serverLoc);
167          assertEquals("Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
168                       ", but was " + serverLoc,
169                       remotingClient.getInvoker().getLocator(), serverLoc);
170
171       }
172       finally
173       {
174          if(remotingClient != null)
175          {
176             // remove callback handler from server
177
remotingClient.removeListener(callbackHandler);
178          }
179       }
180
181
182    }
183
184    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
185    {
186       log.info("Starting remoting server with locator uri of: " + locator);
187       try
188       {
189          connector = new Connector();
190          connector.setInvokerLocator(locator.getLocatorURI());
191          connector.start();
192
193          SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
194          // first parameter is sub-system name. can be any String value.
195
connector.addInvocationHandler("sample", invocationHandler);
196       }
197       catch(Exception JavaDoc e)
198       {
199          log.error("Error starting callback server", e);
200          throw e;
201       }
202    }
203
204    /**
205     * Simple invocation handler implementation.
206     */

207    public static class SampleInvocationHandler implements ServerInvocationHandler
208    {
209
210       private List JavaDoc listeners = new ArrayList JavaDoc();
211
212
213       /**
214        * called to handle a specific invocation
215        *
216        * @param invocation
217        * @return
218        * @throws Throwable
219        */

220       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
221       {
222          // Just going to return static string as this is just simple example code.
223

224          // Will also fire callback to listeners if they were to exist using
225
// simple invocation request.
226
Callback callback = new Callback("This is the payload of callback invocation.");
227          Iterator JavaDoc itr = listeners.iterator();
228          while(itr.hasNext())
229          {
230             InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
231             try
232             {
233                callbackHandler.handleCallback(callback);
234             }
235             catch(HandleCallbackException e)
236             {
237                e.printStackTrace();
238             }
239          }
240
241          return RESPONSE_VALUE;
242
243       }
244
245       /**
246        * Adds a callback handler that will listen for callbacks from
247        * the server invoker handler.
248        *
249        * @param callbackHandler
250        */

251       public void addListener(InvokerCallbackHandler callbackHandler)
252       {
253          listeners.add(callbackHandler);
254       }
255
256       /**
257        * Removes the callback handler that was listening for callbacks
258        * from the server invoker handler.
259        *
260        * @param callbackHandler
261        */

262       public void removeListener(InvokerCallbackHandler callbackHandler)
263       {
264          listeners.remove(callbackHandler);
265       }
266
267       /**
268        * set the mbean server that the handler can reference
269        *
270        * @param server
271        */

272       public void setMBeanServer(MBeanServer JavaDoc server)
273       {
274          // NO OP as do not need reference to MBeanServer for this handler
275
}
276
277       /**
278        * set the invoker that owns this handler
279        *
280        * @param invoker
281        */

282       public void setInvoker(ServerInvoker invoker)
283       {
284          // NO OP as do not need reference back to the server invoker
285
}
286
287    }
288
289
290    public class CallbackHandler implements InvokerCallbackHandler
291    {
292       private Callback callback;
293
294       /**
295        * Will take the callback message and send back to client.
296        * If client locator is null, will store them till client polls to get them.
297        *
298        * @param callback
299        * @throws org.jboss.remoting.callback.HandleCallbackException
300        *
301        */

302       public void handleCallback(Callback callback) throws HandleCallbackException
303       {
304           this.callback = callback;
305           System.out.println("Received callback value of: " + callback.getCallbackObject());
306           System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
307           System.out.println("Received callback server invoker of: " + callback.getServerLocator());
308       }
309
310       public Callback getCallback()
311       {
312          return callback;
313       }
314
315    }
316
317 }
Popular Tags