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.push.disconnect;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.List;
28
import javax.management.MBeanServer;
29
import org.jboss.remoting.Client;
30
import org.jboss.remoting.InvocationRequest;
31
import org.jboss.remoting.InvokerLocator;
32
import org.jboss.remoting.ServerInvocationHandler;
33
import org.jboss.remoting.ServerInvoker;
34
import org.jboss.remoting.callback.Callback;
35
import org.jboss.remoting.callback.HandleCallbackException;
36
import org.jboss.remoting.callback.InvokerCallbackHandler;
37
import org.jboss.remoting.transport.Connector;
39
import junit.framework.TestCase;
42
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
44
public class CallbackTestClient extends TestCase
46
// Default locator values
47
private static String transport = "socket";
48
private static String host = "localhost";
49
private static int port = 5500;
50
private static String callbackHost = "localhost";
51
private static int callbackPort = 5501;
53
private String locatorURI = transport + "://" + host + ":" + port;
55
// public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
57
public void testPullCallbackDisconnect() throws Throwable
59
String callbackServerURI = "socket://" + callbackHost + ":" + callbackPort;
60
Connector callbackServer = new Connector();
61
callbackServer.setInvokerLocator(callbackServerURI);
62
callbackServer.start();
63
// SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
65
// callbackServer.getServerInvoker().addInvocationHandler("callbackTest", invocationHandler);
67
Client client = new Client(new InvokerLocator(locatorURI), "test");
69
CallbackHandler callbackHandler = new CallbackHandler();
70
client.addListener(callbackHandler, new InvokerLocator(callbackServerURI));
72
// send invocation that will trigger sending of callbacks.
75
// wait to get some callbacks.
76
System.out.println("Waiting three seconds for callbacks.");
77
Thread.currentThread().sleep(3000);
79
System.out.println("Done waiting for callbacks, will shutdown callback server and client.");
80
// shutdown client and callback server
81
// callbackServer.getServerInvoker().removeInvocationHandler(
82
// callbackServer.getServerInvoker().getInvocationHandlers()[0].toString()
84
client.removeListener(callbackHandler);
86
callbackServer.stop();
87
callbackServer.destroy();
89
System.out.println("Callback total number after shutdown is " + callbackHandler.getCallbackNumber());
90
int callbackNumber = callbackHandler.getCallbackNumber();
92
// sleep to allow server to send more callbacks if is going to
93
System.out.println("Waiting ten seconds to make sure no more callbacks are delivered by server.");
94
Thread.currentThread().sleep(10000);
96
// check to make sure no more callbacks have been delivered.
97
System.out.println("Callback total number after waiting ten seconds is " + callbackHandler.getCallbackNumber());
98
assertEquals(callbackNumber, callbackHandler.getCallbackNumber());
103
* Simple invocation handler implementation.
105
// public static class SampleInvocationHandler implements ServerInvocationHandler
108
// private List listeners = new ArrayList();
109
// private boolean sendCallbacks = false;
110
// private int callbackCounter = 1;
112
// public SampleInvocationHandler()
118
// * called to handle a specific invocation
120
// * @param invocation
122
// * @throws Throwable
124
// public Object invoke(InvocationRequest invocation) throws Throwable
126
// // Just going to return static string as this is just simple example code.
128
// sendCallbacks = true;
129
// return RESPONSE_VALUE;
133
// private void sendCallbacks()
135
// new Thread(new Runnable()
144
// Thread.currentThread().sleep(1000);
146
// catch(InterruptedException e)
148
// e.printStackTrace();
151
// while(sendCallbacks)
154
// // Will also fire callback to listeners if they were to exist using
155
// // simple invocation request.
156
// Callback callback = new Callback(new Integer(callbackCounter));
157
// Iterator itr = listeners.iterator();
158
// while(itr.hasNext())
160
// InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
163
// callbackHandler.handleCallback(callback);
165
// catch(HandleCallbackException e)
167
// e.printStackTrace();
178
// * Adds a callback handler that will listen for callbacks from
179
// * the server invoker handler.
181
// * @param callbackHandler
183
// public void addListener(InvokerCallbackHandler callbackHandler)
185
// listeners.add(callbackHandler);
189
// * Removes the callback handler that was listening for callbacks
190
// * from the server invoker handler.
192
// * @param callbackHandler
194
// public void removeListener(InvokerCallbackHandler callbackHandler)
196
// listeners.remove(callbackHandler);
200
// * set the mbean server that the handler can reference
204
// public void setMBeanServer(MBeanServer server)
206
// // NO OP as do not need reference to MBeanServer for this handler
210
// * set the invoker that owns this handler
214
// public void setInvoker(ServerInvoker invoker)
216
// // NO OP as do not need reference back to the server invoker
222
public class CallbackHandler implements InvokerCallbackHandler
224
private Callback callback;
225
private Integer callbackNumber;
228
* Will take the callback message and send back to client.
229
* If client locator is null, will store them till client polls to get them.
232
* @throws org.jboss.remoting.callback.HandleCallbackException
235
public void handleCallback(Callback callback) throws HandleCallbackException
237
this.callback = callback;
238
System.out.println("Received callback value of: " + callback.getCallbackObject());
239
System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
240
System.out.println("Received callback server invoker of: " + callback.getServerLocator());
242
callbackNumber = (Integer) callback.getCallbackObject();
245
public int getCallbackNumber()
247
if(callbackNumber != null)
249
return callbackNumber.intValue();
257
public Callback getCallback()
b'\\ No newline at end of file'