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.
22
package org.jboss.test.remoting.callback.asynch;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
29
import javax.management.MBeanServer;
31
import org.jboss.jrunit.extensions.ServerTestCase;
32
import org.jboss.logging.Logger;
33
import org.jboss.remoting.Client;
34
import org.jboss.remoting.InvocationRequest;
35
import org.jboss.remoting.InvokerLocator;
36
import org.jboss.remoting.ServerInvocationHandler;
37
import org.jboss.remoting.ServerInvoker;
38
import org.jboss.remoting.callback.AsynchInvokerCallbackHandler;
39
import org.jboss.remoting.callback.Callback;
40
import org.jboss.remoting.callback.HandleCallbackException;
41
import org.jboss.remoting.callback.InvokerCallbackHandler;
42
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
43
import org.jboss.remoting.transport.Connector;
44
import org.jboss.util.threadpool.BasicThreadPool;
47
* AsynchCallbackTestClientRoot and AsynchCallbackTestServerRoot are the parent classes
48
* for a set of transport specific tests of the asynchronous callback facility.
50
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
51
* @version $Revision: 2103 $
53
* Copyright Nov 25, 2006
56
public abstract class AsynchCallbackTestServerRoot extends ServerTestCase
58
public static int port = 5413;
60
public static String SYNCHRONOUS_TEST = "synchronousTest";
61
public static String ASYNCHRONOUS_SERVER_SIDE_TEST = "asynchronousServerSideTest";
62
public static String ASYNCHRONOUS_CLIENT_SIDE_TEST = "asynchronousClientSideTest";
63
public static String GET_STATUS = "getStatus";
64
public static String RESET = "reset";
65
public static String GET_THREAD_COUNT = "getThreadCount";
66
public static String GET_QUEUE_SIZE = "getQueueSize";
67
public static String THREAD_COUNT = "17";
68
public static String QUEUE_SIZE = "19";
70
private static Logger log = Logger.getLogger(AsynchCallbackTestServerRoot.class);
72
// remoting server connector
73
static private Connector connector;
74
String serverLocatorURI;
78
* Sets up target remoting server.
80
public void setUp() throws Exception
82
log.info("entering setUp()");
83
String locatorURI = getTransport() + "://" + InetAddress.getLocalHost().getHostAddress() + ":" + port;
84
InvokerLocator serverLocator = new InvokerLocator(locatorURI);
85
System.out.println("Starting remoting server with locator uri of: " + locatorURI);
86
Map config = new HashMap();
87
config.put(Client.MAX_NUM_ONEWAY_THREADS, THREAD_COUNT);
88
config.put(Client.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, QUEUE_SIZE);
89
addTransportSpecificConfig(config);
90
connector = new Connector(serverLocator, config);
92
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
93
connector.addInvocationHandler("sample", invocationHandler);
99
* Shuts down the server
101
public void tearDown()
108
protected abstract String getTransport();
111
protected void addTransportSpecificConfig(Map config)
116
* Simple invocation handler implementation. When callback client's are registered, will
117
* generate callbacks periodically.
119
static class SampleInvocationHandler implements ServerInvocationHandler
122
AsynchInvokerCallbackHandler callbackHandler;
124
public void addListener(InvokerCallbackHandler callbackHandler)
126
System.out.println("Adding callback listener.");
127
assertTrue(callbackHandler instanceof AsynchInvokerCallbackHandler);
128
this.callbackHandler = (AsynchInvokerCallbackHandler) callbackHandler;
131
public Object invoke(InvocationRequest invocation) throws Throwable
135
String test = (String) invocation.getParameter();
136
if (test.equals(SYNCHRONOUS_TEST))
138
log.info("making synchronous callback");
139
callbackHandler.handleCallback(new Callback(test));
140
log.info("made synchronous callback");
142
else if (test.equals(ASYNCHRONOUS_SERVER_SIDE_TEST))
144
log.info("making asynchronous callback - server side");
145
callbackHandler.handleCallbackOneway(new Callback("callback"), true);
147
else if (test.equals(ASYNCHRONOUS_CLIENT_SIDE_TEST))
149
log.info("making asynchronous callback - client side");
150
callbackHandler.handleCallbackOneway(new Callback("callback"));
152
else if (test.equals(GET_STATUS))
156
log.info("returning status: " + done);
157
return new Boolean(done);
160
else if (test.equals(RESET))
167
else if (test.equals(GET_THREAD_COUNT))
169
ServerInvokerCallbackHandler sich = (ServerInvokerCallbackHandler) callbackHandler;
170
Client callbackClient = sich.getCallbackClient();
171
Field field = Client.class.getDeclaredField("onewayThreadPool");
172
field.setAccessible(true);
173
BasicThreadPool threadPool = (BasicThreadPool) field.get(callbackClient);
174
int size = threadPool.getMaximumPoolSize();
175
return Integer.toString(size);
177
else if (test.equals(GET_QUEUE_SIZE))
179
ServerInvokerCallbackHandler sich = (ServerInvokerCallbackHandler) callbackHandler;
180
Client callbackClient = sich.getCallbackClient();
181
Field field = Client.class.getDeclaredField("onewayThreadPool");
182
field.setAccessible(true);
183
BasicThreadPool threadPool = (BasicThreadPool) field.get(callbackClient);
184
int size = threadPool.getMaximumQueueSize();
185
return Integer.toString(size);
189
log.error("unrecognized test: " + test);
192
catch (HandleCallbackException e)
194
log.error("Unable to send callback");
204
public void removeListener(InvokerCallbackHandler callbackHandler) {}
205
public void setMBeanServer(MBeanServer server) {}
206
public void setInvoker(ServerInvoker invoker) {}