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.listeners;
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
* This is a test to make sure multiple callback listeners can be added
43
* from the same client and the each gets their callbacks, as well
44
* as making sure they are removed correctly.
46
* This test really only works the way it is coded because know that
47
* everything will be run locally, meaning that I can check the
48
* callback values after making in invoke call because the same
49
* thread will make the callbacks before the thread returns from
52
* @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
53
* @author <a href="mailto:tom@jboss.org">Ton Elrod</a>
54
* @version <tt>$Revision: 1610 $</tt>
56
public class MultipleListenersTestCase extends TestCase
58
private static List callbacks = new ArrayList();
60
private static String value1 = "FOO";
61
private static String value2 = "BAR";
62
private static String value3 = "FOOBAR";
64
public void testCallbacks() throws Throwable
67
// Start the callback server
68
InvokerLocator callbackServerLocator = new InvokerLocator("socket://localhost:2222");
69
Connector callbackConnector = new Connector();
70
callbackConnector.setInvokerLocator(callbackServerLocator.getLocatorURI());
71
callbackConnector.start();
73
// Start the target server
74
InvokerLocator targetServerLocator = new InvokerLocator("socket://localhost:3456");
75
Connector connector = new Connector();
76
connector.setInvokerLocator(targetServerLocator.getLocatorURI());
78
connector.addInvocationHandler("MySubsystem", new ServerInvocationHandlerImpl());
82
Client client = new Client(targetServerLocator);
85
InvokerCallbackHandler callbackHandler1 = new InvokerCallbackHandlerImpl("ONE");
86
InvokerCallbackHandler callbackHandler2 = new InvokerCallbackHandlerImpl("TWO");
88
client.addListener(callbackHandler1, callbackServerLocator);
90
client.invoke("call back " + value1);
92
assertEquals(1, callbacks.size());
95
client.addListener(callbackHandler2, callbackServerLocator);
97
client.invoke("call back " + value2);
99
assertEquals(2, callbacks.size());
102
client.removeListener(callbackHandler1);
104
client.invoke("call back " + value3);
106
assertEquals(1, callbacks.size());
109
client.removeListener(callbackHandler2);
112
callbackConnector.stop();
115
callbackConnector.destroy();
119
private static class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
123
public InvokerCallbackHandlerImpl(String id)
128
public void handleCallback(Callback callback) throws HandleCallbackException
130
System.out.println("Handler " + id + " got callback: " + callback.getParameter());
131
callbacks.add(callback.getParameter());
132
//throw new CustomListenerException("This is the custom listener exception that I want to know about.");
136
private static class ServerInvocationHandlerImpl implements ServerInvocationHandler
139
protected ServerInvoker serverInvoker;
140
protected ArrayList callbackHandlers = new ArrayList();
142
public void setMBeanServer(MBeanServer server)
146
public void setInvoker(ServerInvoker invoker)
148
serverInvoker = invoker;
151
public Object invoke(InvocationRequest invocation) throws Throwable
153
String param = (String) invocation.getParameter();
154
System.out.println(param);
156
if(param.startsWith("call back"))
158
String arg = param.substring("call back".length()).trim();
159
for(Iterator i = callbackHandlers.iterator(); i.hasNext();)
161
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) i.next();
162
System.out.println("sending callback to " + callbackHandler);
163
callbackHandler.handleCallback(new Callback(arg));
170
public void addListener(InvokerCallbackHandler callbackHandler)
172
System.out.println("adding listener " + callbackHandler);
173
callbackHandlers.add(callbackHandler);
176
public void removeListener(InvokerCallbackHandler callbackHandler)
178
System.out.println("removing listener " + callbackHandler);
179
callbackHandlers.remove(callbackHandler);
183
private static class CustomListenerException extends RuntimeException
185
public CustomListenerException(String e)