2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.callback;
25
import junit.framework.TestCase;
26
import org.jboss.logging.Logger;
27
import org.jboss.remoting.Client;
28
import org.jboss.remoting.InvocationRequest;
29
import org.jboss.remoting.InvokerLocator;
30
import org.jboss.remoting.ServerInvocationHandler;
31
import org.jboss.remoting.ServerInvoker;
32
import org.jboss.remoting.callback.Callback;
33
import org.jboss.remoting.callback.HandleCallbackException;
34
import org.jboss.remoting.callback.InvokerCallbackHandler;
35
import org.jboss.remoting.transport.Connector;
37
import javax.management.MBeanServer;
38
import java.util.ArrayList;
39
import java.util.Iterator;
40
import java.util.List;
43
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
45
public class CallbackTestClient extends TestCase
47
// Default locator values
48
private static String transport = "socket";
49
private static String host = "localhost";
50
private static int port = 5500;
52
private String locatorURI = transport + "://" + host + ":" + port;
54
private Client remotingClient;
55
private Connector connector;
57
private static final Logger log = Logger.getLogger(CallbackTestClient.class);
59
public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
62
private void init() throws Exception
64
createRemotingClient();
67
public void createRemotingClient() throws Exception
69
InvokerLocator locator = new InvokerLocator(locatorURI);
70
System.out.println("Calling remoting server with locator uri of: " + locatorURI);
72
// This could have been new Client(locator), but want to show that subsystem param is null
73
// Could have also been new Client(locator, "sample");
74
remotingClient = new Client(locator);
75
remotingClient.connect();
79
public void makeInvocation() throws Throwable
81
Object response = remotingClient.invoke("Do something", null);
82
System.out.println("Invocation response: " + response);
85
public void setUp() throws Exception
90
public void tearDown() throws Exception
92
if(remotingClient != null)
94
remotingClient.disconnect();
103
public void testPullCallback() throws Throwable
105
CallbackHandler callbackHandler = new CallbackHandler();
108
// by passing only the callback handler, will indicate pull callbacks
109
remotingClient.addListener(callbackHandler);
110
// now make invocation on server, which should cause a callback to happen
113
List callbacks = remotingClient.getCallbacks(callbackHandler);
114
Iterator itr = callbacks.iterator();
117
Object obj = itr.next();
118
log.info("testPullCallback - Callback object should have been " +
119
CallbackTestServer.CALLBACK_VALUE + " and was " +
120
(obj == null ? null : ((Callback) obj).getCallbackObject()));
121
if(obj instanceof Callback)
123
assertEquals("Callback object is NOT same.", CallbackTestServer.CALLBACK_VALUE, ((Callback) obj).getCallbackObject());
127
assertTrue("Callback object is NOT of type Callback.", false);
133
if(remotingClient != null)
135
// remove callback handler from server
136
remotingClient.removeListener(callbackHandler);
141
public void testPushCallback() throws Throwable
144
CallbackHandler callbackHandler = new CallbackHandler();
148
// Using loctor with port value one higher than the target server
149
String callbackLocatorURI = transport + "://" + host + ":" + (port + 5);
150
InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
152
log.info("testPushCallback - Setting up server.");
153
// call to create remoting server to
154
// receive client callbacks.
155
setupServer(callbackLocator);
157
// Callback handle object will be passed back as part of the callback object
158
String callbackHandleObject = "myCallbackHandleObject";
159
log.info("testPushCallback - adding listener.");
160
// by passing only the callback handler, will indicate pull callbacks
161
remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject);
162
log.info("testPushCallback - make invocation");
163
// now make invocation on server, which should cause a callback to happen
166
// need to wait for brief moment so server can callback
169
Callback callback = callbackHandler.getCallback();
170
log.info("testPushCallback - Callback returned was " + callback);
171
assertNotNull("Callback returned was null.", callback);
173
Object callbackObj = callback.getCallbackObject();
174
log.info("testPushCallback - Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", and was " + callbackObj);
175
assertEquals("Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", but was " + callbackObj,
176
CallbackTestServer.CALLBACK_VALUE, callbackObj);
177
Object callbackHandleObj = callback.getCallbackHandleObject();
178
log.info("testPushCallback - Callback handle object should have been " + callbackHandleObject + ", and was " + callbackHandleObj);
179
assertEquals("Callback handle object should have been " + callbackHandleObject + ", but was " + callbackHandleObj,
180
callbackHandleObject, callbackHandleObj);
181
InvokerLocator serverLoc = callback.getServerLocator();
182
log.info("testPushCallback - Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
183
", and was " + serverLoc);
184
assertEquals("Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
185
", but was " + serverLoc,
186
remotingClient.getInvoker().getLocator(), serverLoc);
191
if(remotingClient != null)
193
// remove callback handler from server
194
remotingClient.removeListener(callbackHandler);
201
public void setupServer(InvokerLocator locator) throws Exception
203
log.info("Starting remoting server with locator uri of: " + locator);
206
connector = new Connector();
207
connector.setInvokerLocator(locator.getLocatorURI());
210
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
211
// first parameter is sub-system name. can be any String value.
212
connector.addInvocationHandler("sample", invocationHandler);
216
log.error("Error starting callback server", e);
222
* Simple invocation handler implementation.
224
public static class SampleInvocationHandler implements ServerInvocationHandler
227
private List listeners = new ArrayList();
231
* called to handle a specific invocation
237
public Object invoke(InvocationRequest invocation) throws Throwable
239
// Just going to return static string as this is just simple example code.
241
// Will also fire callback to listeners if they were to exist using
242
// simple invocation request.
243
Callback callback = new Callback("This is the payload of callback invocation.");
244
Iterator itr = listeners.iterator();
247
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
250
callbackHandler.handleCallback(callback);
252
catch(HandleCallbackException e)
258
return RESPONSE_VALUE;
263
* Adds a callback handler that will listen for callbacks from
264
* the server invoker handler.
266
* @param callbackHandler
268
public void addListener(InvokerCallbackHandler callbackHandler)
270
listeners.add(callbackHandler);
274
* Removes the callback handler that was listening for callbacks
275
* from the server invoker handler.
277
* @param callbackHandler
279
public void removeListener(InvokerCallbackHandler callbackHandler)
281
listeners.remove(callbackHandler);
285
* set the mbean server that the handler can reference
289
public void setMBeanServer(MBeanServer server)
291
// NO OP as do not need reference to MBeanServer for this handler
295
* set the invoker that owns this handler
299
public void setInvoker(ServerInvoker invoker)
301
// NO OP as do not need reference back to the server invoker
307
public class CallbackHandler implements InvokerCallbackHandler
309
private Callback callback;
312
* Will take the callback message and send back to client.
313
* If client locator is null, will store them till client polls to get them.
316
* @throws org.jboss.remoting.callback.HandleCallbackException
319
public void handleCallback(Callback callback) throws HandleCallbackException
321
this.callback = callback;
322
System.out.println("Received callback value of: " + callback.getCallbackObject());
323
System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
324
System.out.println("Received callback server invoker of: " + callback.getServerLocator());
327
public Callback getCallback()
b'\\ No newline at end of file'