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.shutdown;
24
import java.net.InetAddress;
25
import java.util.HashMap;
28
import javax.management.MBeanServer;
30
import org.apache.log4j.Logger;
31
import org.jboss.jrunit.extensions.ServerTestCase;
32
import org.jboss.remoting.Client;
33
import org.jboss.remoting.ConnectionListener;
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.Callback;
39
import org.jboss.remoting.callback.HandleCallbackException;
40
import org.jboss.remoting.callback.InvokerCallbackHandler;
41
import org.jboss.remoting.transport.Connector;
44
* This is the server half of a unit test designed to verify that a Remoting application
45
* will shut down without any stray threads hanging it up. To exercise as many
46
* Remoting threads as possible, the server enables leasing and registers a
47
* connection listener.
49
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
50
* @version $Revision: 3011 $
52
* Copyright Jan 19, 2007
55
public class ShutdownTestServer extends ServerTestCase
57
public static int port = 9876;
58
private static Logger log = Logger.getLogger(ShutdownTestServer.class);
59
private Connector connector;
60
private String transport;
61
private Map extraConfig;
63
public ShutdownTestServer(String transport, Map config)
65
this.transport = transport;
66
this.extraConfig = config;
70
public void setUp() throws Exception
72
String host = InetAddress.getLocalHost().getHostAddress();
73
String portString = System.getProperty("port");
74
log.info("portString: " + portString);
75
int port = Integer.parseInt(portString);
76
String locatorURI = transport + "://" + host + ":" + port;
77
InvokerLocator locator = new InvokerLocator(locatorURI);
78
HashMap serverConfig = new HashMap(extraConfig);
79
log.info("serverConfig: " + serverConfig);
80
connector = new Connector(locator, serverConfig);
82
connector.setLeasePeriod(2000);
83
connector.addInvocationHandler("test", new TestHandler());
84
connector.addConnectionListener(new TestListener());
86
log.info("server started at: " + locatorURI);
91
public void tearDown()
93
if (connector != null)
96
log.info("server shut down");
101
public static void main(String[] args)
103
if (args.length == 0)
104
throw new RuntimeException();
106
HashMap config = new HashMap();
107
System.out.println("server args.length: " + args.length);
109
getConfig(config, args[1]);
111
String transport = args[0];
112
ShutdownTestServer server = new ShutdownTestServer(transport, config);
116
log.info("server back from setUp()");
118
log.info("server calling tearDown()");
120
log.info("server back from tearDown()");
129
protected static void getConfig(Map config, String configs)
132
int ampersand = configs.indexOf('&');
133
while (ampersand > 0)
135
String s = configs.substring(start, ampersand);
136
int equals = s.indexOf('=');
137
String param = s.substring(0, equals);
138
String value = s.substring(equals + 1);
139
config.put(param, value);
140
start = ampersand + 1;
141
ampersand = configs.indexOf('&', start);
143
log.info("config: " + config);
144
log.info("configs: " + configs);
148
public class TestHandler implements ServerInvocationHandler
150
public void setMBeanServer(MBeanServer server) {}
151
public void setInvoker(ServerInvoker invoker) {}
153
public Object invoke(InvocationRequest invocation) throws Throwable
155
Integer i = (Integer) invocation.getParameter();
156
return new Integer(i.intValue() + 1);
159
public void addListener(InvokerCallbackHandler callbackHandler)
163
log.info("sending callback");
164
callbackHandler.handleCallback(new Callback("callback"));
165
log.info("sent callback");
167
catch (HandleCallbackException e)
169
log.info("error handling callback");
174
public void removeListener(InvokerCallbackHandler callbackHandler) {}
178
public static class TestListener implements ConnectionListener
180
public void handleConnectionException(Throwable throwable, Client client)
182
log.info("got connection exception: " + throwable);