~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/soak/ServerLauncher.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
package org.jboss.test.remoting.soak;
23
 
 
24
 
import java.net.InetAddress;
25
 
import java.util.HashMap;
26
 
import java.util.HashSet;
27
 
import java.util.Iterator;
28
 
import java.util.Map;
29
 
import java.util.Set;
30
 
 
31
 
import javax.management.MBeanServer;
32
 
 
33
 
import org.apache.log4j.ConsoleAppender;
34
 
import org.apache.log4j.Level;
35
 
import org.apache.log4j.Logger;
36
 
import org.apache.log4j.PatternLayout;
37
 
import org.jboss.logging.XLevel;
38
 
import org.jboss.remoting.InvocationRequest;
39
 
import org.jboss.remoting.InvokerLocator;
40
 
import org.jboss.remoting.ServerInvocationHandler;
41
 
import org.jboss.remoting.ServerInvoker;
42
 
import org.jboss.remoting.callback.Callback;
43
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
44
 
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
45
 
import org.jboss.remoting.transport.Connector;
46
 
 
47
 
 
48
 
/**
49
 
 * 
50
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
51
 
 * @version $Revision: 1.1 $
52
 
 * <p>
53
 
 * Copyright Mar 13, 2008
54
 
 * </p>
55
 
 */
56
 
public class ServerLauncher extends SoakConstants
57
 
{
58
 
   private static Logger log = Logger.getLogger(ServerLauncher.class);
59
 
   private static Map locators = new HashMap();
60
 
   private static Connector[] connectors = new Connector[4];
61
 
   
62
 
   public static Map getLocators()
63
 
   {
64
 
      return locators;
65
 
   }
66
 
   
67
 
   public static void main(String[] args)
68
 
   {
69
 
      try
70
 
      {
71
 
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
72
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
73
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
74
 
         PatternLayout layout = new PatternLayout(pattern);
75
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
76
 
         Logger.getRootLogger().addAppender(consoleAppender); 
77
 
         
78
 
         String host = InetAddress.getLocalHost().getHostAddress();
79
 
         
80
 
         connectors[0] = setupServer(host, 6666, "bisocket");
81
 
         locators.put("bisocket", connectors[0].getLocator().getLocatorURI());
82
 
         
83
 
         connectors[1] = setupServer(host, 6667, "http");
84
 
         locators.put("http", connectors[1].getLocator().getLocatorURI());
85
 
         
86
 
         connectors[2] = setupServer(host, 6668, "rmi");
87
 
         locators.put("rmi", connectors[2].getLocator().getLocatorURI());
88
 
         
89
 
         connectors[3] = setupServer(host, 6669, "socket");
90
 
         locators.put("socket", connectors[3].getLocator().getLocatorURI());
91
 
         
92
 
         log.info("SERVERS CREATED: " + locators);
93
 
         
94
 
         System.in.read();
95
 
         System.in.read();
96
 
         System.in.read();
97
 
         
98
 
         log.info("SHUTTING DOWN SERVERS");
99
 
         for (int i = 0; i < connectors.length; i++)
100
 
         {
101
 
            connectors[i].stop();
102
 
         }
103
 
         log.info("SERVERS SHUT DOWN");
104
 
         
105
 
      }
106
 
      catch (Exception e)
107
 
      {
108
 
         log.error("Error", e);
109
 
      }
110
 
   }
111
 
   
112
 
   
113
 
   protected static Connector setupServer(String host, int port, String transport) throws Exception
114
 
   {
115
 
      String locatorURI = transport + "://" + host + ":" + port + "/?timeout=0"; 
116
 
      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
117
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
118
 
      HashMap config = new HashMap();
119
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
120
 
      Connector connector = new Connector(serverLocator, config);
121
 
      connector.create();
122
 
      TestInvocationHandler invocationHandler = new TestInvocationHandler();
123
 
      connector.addInvocationHandler("test", invocationHandler);
124
 
      connector.start();
125
 
      return connector;
126
 
   }
127
 
   
128
 
   
129
 
   static class TestInvocationHandler implements ServerInvocationHandler
130
 
   {
131
 
      Map listeners = new HashMap();
132
 
      Object lock = new Object();
133
 
      
134
 
      public void addListener(InvokerCallbackHandler callbackHandler)
135
 
      {
136
 
         log.debug("entering addListener()");
137
 
         synchronized (lock)
138
 
         {
139
 
            String id = ((ServerInvokerCallbackHandler)callbackHandler).getClientSessionId();
140
 
            listeners.put(id, callbackHandler);
141
 
         }
142
 
         log.debug("added InvokerCallbackHandler: " + listeners);
143
 
      }
144
 
      
145
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
146
 
      {
147
 
         String command = (String) invocation.getParameter();
148
 
         log.debug("command: " + command);
149
 
         
150
 
         if (COPY.equals(command))
151
 
         {
152
 
            Object o = invocation.getRequestPayload().get(PAYLOAD);
153
 
            return o;
154
 
         }
155
 
         else if (SPIN.equals(command))
156
 
         {
157
 
            String s = (String) invocation.getRequestPayload().get(SPIN_TIME);
158
 
            int spinTime = Integer.parseInt(s);
159
 
            SpinThread t = new SpinThread();
160
 
            t.start();
161
 
            Thread.sleep(spinTime);
162
 
            t.setStop();
163
 
            return "done";
164
 
         }
165
 
         else if (CALLBACK.equals(command))
166
 
         {
167
 
            String id = invocation.getSessionId();
168
 
            Map requestPayload = invocation.getRequestPayload();
169
 
            String s = (String) requestPayload.get(NUMBER_OF_CALLBACKS);
170
 
            int callbacks = Integer.parseInt(s);
171
 
            InvokerCallbackHandler callbackHandler = null;
172
 
 
173
 
            synchronized (lock)
174
 
            {
175
 
               callbackHandler = (InvokerCallbackHandler) listeners.get(id);
176
 
            }
177
 
 
178
 
            if (callbackHandler == null)
179
 
            {
180
 
               log.debug("sessionId: " + id);
181
 
               log.debug("listeners: " + listeners);
182
 
            }
183
 
            Callback callback = new Callback("callback");
184
 
            for (int i = 0; i < callbacks; i++)
185
 
            {
186
 
               callbackHandler.handleCallback(callback);
187
 
            }
188
 
         }
189
 
 
190
 
         return command;
191
 
      }
192
 
      
193
 
      public void removeListener(InvokerCallbackHandler callbackHandler)
194
 
      {
195
 
         synchronized (lock)
196
 
         {
197
 
            String id = ((ServerInvokerCallbackHandler) callbackHandler).getClientSessionId();
198
 
            listeners.remove(id);
199
 
         }
200
 
      }
201
 
      
202
 
      public void setMBeanServer(MBeanServer server) {}
203
 
      public void setInvoker(ServerInvoker invoker) {}
204
 
   }
205
 
   
206
 
   static class SpinThread extends Thread
207
 
   {
208
 
      boolean stop;
209
 
      static int counter;
210
 
      static Object lock = new Object();
211
 
      
212
 
      public SpinThread()
213
 
      {
214
 
         synchronized (lock)
215
 
         {
216
 
            setName("spinThread:" + counter++);
217
 
         }
218
 
      }
219
 
      public void setStop()
220
 
      {
221
 
         stop = true;
222
 
         log.debug(this + " stop = " + stop);
223
 
      }
224
 
      
225
 
      public void run()
226
 
      {
227
 
         int n = 0;
228
 
         while (!stop)
229
 
         {
230
 
            n++;
231
 
            if ((n + 1) % 10000 == 0)
232
 
               log.debug(this + "stop = " + stop);
233
 
         }
234
 
         log.debug("SpinThread done");
235
 
      }
236
 
   }
237
 
}
238