[jboss-remoting-commits] JBoss Remoting SVN: r3635 - remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Fri Mar 14 04:56:11 EDT 2008


Author: ron.sigal at jboss.com
Date: 2008-03-14 04:56:11 -0400 (Fri, 14 Mar 2008)
New Revision: 3635

Added:
   remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ClientLauncher.java
   remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/HeavyComputeClient.java
   remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockEJBClient.java
   remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockJBMClient.java
   remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ServerLauncher.java
Log:
JBREM-931: New soak test.

Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ClientLauncher.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ClientLauncher.java	                        (rev 0)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ClientLauncher.java	2008-03-14 08:56:11 UTC (rev 3635)
@@ -0,0 +1,212 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.soak;
+
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+
+
+/**
+ * 
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Mar 13, 2008
+ * </p>
+ */
+public class ClientLauncher extends SoakConstants
+{
+   private static Logger log = Logger.getLogger(ClientLauncher.class);
+   
+   private static Counter mockEJBClientCurrentCounter = new Counter();
+   private static Counter mockJBMClientCurrentCounter = new Counter();
+   private static Counter heavyComputeClientCurrentCounter = new Counter();
+   
+   private static Counter mockEJBClientCounter = new Counter();
+   private static Counter mockJBMClientCounter = new Counter();
+   private static Counter heavyComputeClientCounter = new Counter();
+   
+   private static Counter mockEJBClientFailureCounter = new Counter();
+   private static Counter mockJBMClientFailureCounter = new Counter();
+   private static Counter heavyComputeClientFailureCounter = new Counter();
+   
+   private static Random random;
+   private static String[] transports = {"bisocket", "http", "rmi", "socket"};
+   private static int[] ports = {6666, 6667, 6668, 6669};
+   private static int[] transportCounters = new int[4];
+   private static String host;
+   private static boolean creationDone;
+   
+   private static long DURATION = 3000;
+   
+   public static void main(String[] args)
+   {
+      try
+      {
+         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
+         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+         PatternLayout layout = new PatternLayout(pattern);
+         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+         Logger.getRootLogger().addAppender(consoleAppender); 
+         
+         host = InetAddress.getLocalHost().getHostAddress();
+         random = new Random(System.currentTimeMillis());
+         Timer timer = new Timer(false);
+         timer.schedule(new DisplayTimerTask(), 5000, 5000);
+         long start = System.currentTimeMillis();
+         
+         while (System.currentTimeMillis() - start < DURATION)
+         {
+            createClient();
+            int n = random.nextInt(30) * 100;
+            log.debug("waiting " + n + " ms");
+            Thread.sleep(n);
+         }
+         
+         creationDone = true;
+         log.info("Client creation phase complete.");
+      }
+      catch (Throwable t)
+      {
+         log.error("Error", t);
+      }
+   }
+   
+   
+   protected static void createClient() throws Throwable
+   {
+      int k = random.nextInt(4);
+      String transport = transports[k];
+      int port = ports[k];
+      transportCounters[k]++;
+      int clientType = random.nextInt(25);
+      
+      if (clientType < 10)
+      {
+         createMockEJBClient(transport, port);
+      }
+      else if (clientType < 20)
+      {
+         createMockJBMClient(transport, port);
+      }
+      else
+      {
+         createHeavyComputeClient(transport, port);
+      }
+   }
+   
+   
+   protected static void createMockEJBClient(String transport, int port) throws Throwable
+   {
+      String locatorURI = transport + "://" + host + ":" + port;
+      Map metadata = new HashMap();
+      metadata.put(NAME, "MockEJBClient" + mockEJBClientCounter.increment());
+      metadata.put(COUNTER, mockEJBClientCurrentCounter);
+      metadata.put(FAILURE_COUNTER, mockEJBClientFailureCounter);
+      metadata.put(NUMBER_OF_CALLS, Integer.toString(random.nextInt(20000)));
+      MockEJBClient c = new MockEJBClient(locatorURI, metadata);
+      Thread t = new Thread(c);
+      t.start(); 
+   }
+   
+   
+   protected static void createMockJBMClient(String transport, int port) throws Throwable
+   {
+      String locatorURI = transport + "://" + host + ":" + port;
+      Map metadata = new HashMap();
+      metadata.put(NAME, "MockJBMClient" + mockJBMClientCounter.increment());
+      metadata.put(COUNTER, mockJBMClientCurrentCounter);
+      metadata.put(FAILURE_COUNTER, mockJBMClientFailureCounter);
+      metadata.put(NUMBER_OF_CALLS, Integer.toString(random.nextInt(5000)));
+      metadata.put(NUMBER_OF_CALLBACKS, Integer.toString(random.nextInt(8)));
+      MockJBMClient c = new MockJBMClient(locatorURI, metadata);
+      Thread t = new Thread(c);
+      t.start();
+   }
+   
+   
+   protected static void createHeavyComputeClient(String transport, int port) throws Throwable
+   {
+      String locatorURI = transport + "://" + host + ":" + port;
+      Map metadata = new HashMap();
+      metadata.put(NAME, "HeavyComputeClient" + heavyComputeClientCounter.increment());
+      metadata.put(COUNTER, heavyComputeClientCurrentCounter);
+      metadata.put(FAILURE_COUNTER, heavyComputeClientFailureCounter);
+      metadata.put(NUMBER_OF_CALLS, Integer.toString(random.nextInt(10)));
+      metadata.put(SPIN_TIME, "10000");
+      HeavyComputeClient c = new HeavyComputeClient(locatorURI, metadata);
+      heavyComputeClientCounter.increment();
+      Thread t = new Thread(c);
+      t.start();
+   }
+   
+   
+   static class Counter
+   {
+      int count;
+      
+      public synchronized int getCount() { return count; }
+      public synchronized int increment() { return count++; }
+      public synchronized int decrement() { return --count; }
+   }
+   
+   static class DisplayTimerTask extends TimerTask
+   {
+      public void run()
+      {
+         System.out.println("");
+         System.out.println("=========================================");
+         System.out.println("current MockEJBCLients:      " + mockEJBClientCurrentCounter.getCount());
+         System.out.println("current MockJBMClients:      " + mockJBMClientCurrentCounter.getCount());
+         System.out.println("current HeavyComputeClients: " + heavyComputeClientCurrentCounter.getCount());
+         System.out.println("-----------------------------------------");
+         System.out.println("bisocket clients:            " + transportCounters[0]);
+         System.out.println("http clients:                " + transportCounters[1]);
+         System.out.println("rmi clients:                 " + transportCounters[2]);
+         System.out.println("socket clients:              " + transportCounters[3]);
+         System.out.println("-----------------------------------------");
+         System.out.println("failed MockEJBCLients:       " + mockEJBClientFailureCounter.getCount());
+         System.out.println("failed MockJBMClients:       " + mockJBMClientFailureCounter.getCount());
+         System.out.println("failed HeavyComputeClients:  " + heavyComputeClientFailureCounter.getCount());
+         System.out.println("=========================================");
+         System.out.println("");
+         
+         if (creationDone && 
+               mockEJBClientCurrentCounter.getCount() == 0 &&
+               mockJBMClientCurrentCounter.getCount() == 0 &&
+               heavyComputeClientCurrentCounter.getCount() == 0)
+            cancel();
+      }
+   }
+}
+

Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/HeavyComputeClient.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/HeavyComputeClient.java	                        (rev 0)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/HeavyComputeClient.java	2008-03-14 08:56:11 UTC (rev 3635)
@@ -0,0 +1,98 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.soak;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvokerLocator;
+
+
+/**
+ * 
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Mar 13, 2008
+ * </p>
+ */
+public class HeavyComputeClient extends SoakConstants implements Runnable
+{
+   protected static Logger log = Logger.getLogger(HeavyComputeClient.class);
+   
+   protected InvokerLocator locator;
+   protected Map metadata;
+   protected String name;
+   protected boolean ok = true;
+   protected ClientLauncher.Counter counter;
+   protected ClientLauncher.Counter failureCounter;
+   
+   public HeavyComputeClient(String locator, Map metadata) throws Exception
+   {
+      this.locator = new InvokerLocator(locator);
+      this.metadata = metadata;
+      this.name = (String) metadata.get(SoakConstants.NAME);
+      counter = (ClientLauncher.Counter) metadata.remove(COUNTER);
+      counter.increment();
+      failureCounter = (ClientLauncher.Counter) metadata.remove(FAILURE_COUNTER);
+      log.info("created " + name);
+   }
+
+   public void run()
+   {
+      try
+      {
+         String s = (String) metadata.get(SoakConstants.NUMBER_OF_CALLS);
+         int calls = Integer.valueOf(s).intValue();
+         for (int i = 0; i < calls; i++)
+         {
+            makeCall(i);
+         }
+      }
+      catch (Throwable t)
+      {
+         log.error(name, t);
+      }
+      finally
+      {
+         log.info(name + ": " + (ok ? "PASS" : "FAIL"));
+         counter.decrement();
+         if (!ok) failureCounter.increment();
+      }
+   }
+   
+   protected void makeCall(int i) throws Throwable
+   {
+      Client client = new Client(locator);
+      client.connect();
+      Object response = client.invoke(SPIN, metadata);
+      if (!"done".equals(response))
+      {
+         ok = false;
+         log.info(name + ": failure on call " + i);
+      }
+      client.disconnect();
+   }
+}
+

Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockEJBClient.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockEJBClient.java	                        (rev 0)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockEJBClient.java	2008-03-14 08:56:11 UTC (rev 3635)
@@ -0,0 +1,100 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.soak;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvokerLocator;
+
+
+/**
+ * 
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Mar 13, 2008
+ * </p>
+ */
+public class MockEJBClient extends SoakConstants implements Runnable
+{
+   protected static Logger log = Logger.getLogger(MockEJBClient.class);
+   
+   protected InvokerLocator locator;
+   protected Map metadata;
+   protected String name;
+   protected boolean ok = true;
+   protected ClientLauncher.Counter counter;
+   protected ClientLauncher.Counter failureCounter;
+   
+   public MockEJBClient(String locator, Map metadata) throws Exception
+   {
+      this.locator = new InvokerLocator(locator);
+      this.metadata = metadata;
+      this.name = (String) metadata.get(SoakConstants.NAME);
+      counter = (ClientLauncher.Counter) metadata.remove(COUNTER);
+      counter.increment();
+      failureCounter = (ClientLauncher.Counter) metadata.remove(FAILURE_COUNTER);
+      log.info("created " + name);
+   }
+
+   public void run()
+   {
+      try
+      {
+         String s = (String) metadata.get(SoakConstants.NUMBER_OF_CALLS);
+         int calls = Integer.valueOf(s).intValue();
+         for (int i = 0; i < calls; i++)
+         {
+            makeCall(i);
+         }
+      }
+      catch (Throwable t)
+      {
+         log.error(name, t);
+      }
+      finally
+      {
+         counter.decrement();
+         log.info(name + ": " + (ok ? "PASS" : "FAIL"));
+         if (!ok) failureCounter.increment();
+      }
+   }
+   
+   protected void makeCall(int i) throws Throwable
+   {
+      Client client = new Client(locator);
+      client.connect();
+      Map metadata = new HashMap();
+      metadata.put(PAYLOAD, "abc");
+      Object response = client.invoke(COPY, metadata);
+      if (!"abc".equals(response))
+      {
+         ok = false;
+         log.info(name + ": failure on call " + i);
+      }
+      client.disconnect();
+   }
+}
+

Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockJBMClient.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockJBMClient.java	                        (rev 0)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/MockJBMClient.java	2008-03-14 08:56:11 UTC (rev 3635)
@@ -0,0 +1,117 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.soak;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+
+/**
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Mar 13, 2008
+ * </p>
+ */
+public class MockJBMClient extends SoakConstants implements Runnable
+{
+   protected static Logger log = Logger.getLogger(MockJBMClient.class);
+   
+   protected InvokerLocator locator;
+   protected Map metadata;
+   protected String name;
+   protected boolean ok = true;
+   protected Client client;
+   protected ClientLauncher.Counter counter;
+   protected ClientLauncher.Counter failureCounter;
+   
+   public MockJBMClient(String locator, Map metadata) throws Exception
+   {
+      this.locator = new InvokerLocator(locator);
+      this.metadata = metadata;
+      this.name = (String) metadata.get(NAME);
+      counter = (ClientLauncher.Counter) metadata.remove(COUNTER);
+      counter.increment();
+      failureCounter = (ClientLauncher.Counter) metadata.remove(FAILURE_COUNTER);
+      log.info("created " + name);
+   }
+   
+   public void run()
+   {
+      try
+      {
+         client = new Client(locator);
+         client.connect();
+         TestCallbackHandler callbackHandler = new TestCallbackHandler();
+         Map callbackMetadata = new HashMap();
+         client.addListener(callbackHandler, callbackMetadata, null, true);
+         String s = (String) metadata.get(NUMBER_OF_CALLS);
+         int calls = Integer.valueOf(s).intValue();
+         for (int i = 0; i < calls; i++)
+         {
+            makeCall(i);
+         }
+         client.removeListener(callbackHandler);
+         client.disconnect();
+         s = (String) metadata.get(NUMBER_OF_CALLBACKS);
+         int i = Integer.parseInt(s);
+         boolean ok = callbackHandler.counter == i * calls;
+         if (!ok)
+         {
+            log.info("expected: " + (i * calls) + ", received: " + callbackHandler.counter);
+         }
+      }
+      catch (Throwable t)
+      {
+         log.error(name, t);
+      }
+      finally
+      {
+         log.info(name + ": " + (ok ? "PASS" : "FAIL"));
+         counter.decrement();
+         if (!ok) failureCounter.increment();
+      }
+   }
+   
+   protected void makeCall(int i) throws Throwable
+   {
+      client.invoke(CALLBACK, metadata);
+   }
+   
+   static class TestCallbackHandler implements InvokerCallbackHandler
+   {
+      int counter;
+      
+      public void handleCallback(Callback callback) throws HandleCallbackException
+      {
+         counter++;
+         log.debug("received callback");
+      }  
+   }
+}
+

Added: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ServerLauncher.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ServerLauncher.java	                        (rev 0)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/soak/ServerLauncher.java	2008-03-14 08:56:11 UTC (rev 3635)
@@ -0,0 +1,207 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.soak;
+
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanServer;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+
+
+/**
+ * 
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Mar 13, 2008
+ * </p>
+ */
+public class ServerLauncher extends SoakConstants
+{
+   private static Logger log = Logger.getLogger(ServerLauncher.class);
+   private static Map locators = new HashMap();
+   
+   public static Map getLocators()
+   {
+      return locators;
+   }
+   
+   public static void main(String[] args)
+   {
+      try
+      {
+         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
+         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+         PatternLayout layout = new PatternLayout(pattern);
+         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+         Logger.getRootLogger().addAppender(consoleAppender); 
+         
+         String host = InetAddress.getLocalHost().getHostAddress();
+         
+         Connector connector = setupServer(host, 6666, "bisocket");
+         locators.put("bisocket", connector.getLocator().getLocatorURI());
+         
+         connector = setupServer(host, 6667, "http");
+         locators.put("http", connector.getLocator().getLocatorURI());
+         
+         connector = setupServer(host, 6668, "rmi");
+         locators.put("rmi", connector.getLocator().getLocatorURI());
+         
+         connector = setupServer(host, 6669, "socket");
+         locators.put("socket", connector.getLocator().getLocatorURI());
+         
+         log.info("servers: " + locators);
+      }
+      catch (Exception e)
+      {
+         log.error("Error", e);
+      }
+   }
+   
+   
+   protected static Connector setupServer(String host, int port, String transport) throws Exception
+   {
+      String locatorURI = transport + "://" + host + ":" + port; 
+      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
+      log.info("Starting remoting server with locator uri of: " + locatorURI);
+      HashMap config = new HashMap();
+      config.put(InvokerLocator.FORCE_REMOTE, "true");
+      Connector connector = new Connector(serverLocator, config);
+      connector.create();
+      TestInvocationHandler invocationHandler = new TestInvocationHandler();
+      connector.addInvocationHandler("test", invocationHandler);
+      connector.start();
+      return connector;
+   }
+   
+   
+   static class TestInvocationHandler implements ServerInvocationHandler
+   {
+      Map listeners = new HashMap();
+      Object lock = new Object();
+      
+      public void addListener(InvokerCallbackHandler callbackHandler)
+      {
+         synchronized (lock)
+         {
+            String id = ((ServerInvokerCallbackHandler)callbackHandler).getClientSessionId();
+            listeners.put(id, callbackHandler);
+         }
+         log.debug("added InvokerCallbackHandler");
+      }
+      
+      public Object invoke(final InvocationRequest invocation) throws Throwable
+      {
+         String command = (String) invocation.getParameter();
+         log.debug("command: " + command);
+         
+         if (COPY.equals(command))
+         {
+            Object o = invocation.getRequestPayload().get(PAYLOAD);
+            return o;
+         }
+         else if (SPIN.equals(command))
+         {
+            String s = (String) invocation.getRequestPayload().get(SPIN_TIME);
+            int spinTime = Integer.parseInt(s);
+            SpinThread t = new SpinThread();
+            t.start();
+            Thread.sleep(spinTime);
+            t.setStop();
+            return "done";
+         }
+         else if (CALLBACK.equals(command))
+         {
+            String id = invocation.getSessionId();
+            Map requestPayload = invocation.getRequestPayload();
+            String s = (String) requestPayload.get(NUMBER_OF_CALLBACKS);
+            int callbacks = Integer.parseInt(s);
+            InvokerCallbackHandler callbackHandler = null;
+
+            synchronized (lock)
+            {
+               callbackHandler = (InvokerCallbackHandler) listeners.get(id);
+            }
+
+            Callback callback = new Callback("callback");
+            for (int i = 0; i < callbacks; i++)
+            {
+               callbackHandler.handleCallback(callback);
+            }
+         }
+
+         return command;
+      }
+      
+      public void removeListener(InvokerCallbackHandler callbackHandler)
+      {
+         synchronized (lock)
+         {
+            String id = ((ServerInvokerCallbackHandler) callbackHandler).getClientSessionId();
+            listeners.remove(id);
+         }
+      }
+      
+      public void setMBeanServer(MBeanServer server) {}
+      public void setInvoker(ServerInvoker invoker) {}
+   }
+   
+   static class SpinThread extends Thread
+   {
+      boolean stop;
+      
+      public void setStop()
+      {
+         stop = true;
+      }
+      
+      public void run()
+      {
+         while (!stop)
+         {
+            int n = 2 + 2;
+            n = n * n;
+         }
+         log.debug("SpinThread done");
+      }
+   }
+}
+




More information about the jboss-remoting-commits mailing list