[jboss-cvs] JBossRemoting/src/main/org/jboss/remoting ...

Ron Sigal ron_sigal at yahoo.com
Fri Jan 19 03:41:12 EST 2007


  User: rsigal  
  Date: 07/01/19 03:41:12

  Modified:    src/main/org/jboss/remoting  Tag: remoting_2_x
                        ConnectionValidator.java
  Log:
  JBREM-662:  (1)  Now creates a client invoker once in start() instead of recreating it for each ping. (2) Implements StoppableTimerTask to insure that stop() gets called and client invoker is destroyed.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.13.2.6  +96 -26    JBossRemoting/src/main/org/jboss/remoting/ConnectionValidator.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ConnectionValidator.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/ConnectionValidator.java,v
  retrieving revision 1.13.2.5
  retrieving revision 1.13.2.6
  diff -u -b -r1.13.2.5 -r1.13.2.6
  --- ConnectionValidator.java	18 Jan 2007 02:39:25 -0000	1.13.2.5
  +++ ConnectionValidator.java	19 Jan 2007 08:41:12 -0000	1.13.2.6
  @@ -24,6 +24,7 @@
   
   import org.jboss.logging.Logger;
   import org.jboss.remoting.transport.ClientInvoker;
  +import org.jboss.remoting.util.StoppableTimerTask;
   import org.jboss.remoting.util.TimerUtil;
   
   import java.util.ArrayList;
  @@ -37,16 +38,19 @@
    * @author <a href="mailto:tom.elrod at jboss.com">Tom Elrod</a>
    * @author <a href="mailto:ovidiu at jboss.org">Ovidiu Feodorov</a>
    */
  -public class ConnectionValidator extends TimerTask
  +public class ConnectionValidator extends TimerTask implements StoppableTimerTask
   {
      private List listeners = new ArrayList();
      private Client client = null;
  +   private InvokerLocator locator;
  +   private Map configMap;
   
      private long pingPeriod = DEFAULT_PING_PERIOD;
   
      protected static final Logger log = Logger.getLogger(ConnectionValidator.class.getName());
      private static boolean trace = log.isTraceEnabled();
   
  +   private ClientInvoker clientInvoker;
      private Object lock = new Object();
      private boolean stopped = false;
   
  @@ -70,6 +74,35 @@
   
      private void start()
      {
  +      if (client.getConfiguration() == null)
  +      {
  +         configMap = new HashMap();
  +      }
  +      else
  +      {
  +         configMap = new HashMap(client.getConfiguration());
  +      }
  +      configMap.put("connection_checker", "true");
  +      configMap.put("timeout", "1000");
  +      configMap.put("NumberOfRetries", "1");
  +      locator = client.getInvoker().getLocator();
  +      
  +      try
  +      {
  +         clientInvoker = InvokerRegistry.createClientInvoker(locator, configMap);
  +      }
  +      catch (Exception e)
  +      {
  +         log.error("Unable to create client invoker for locator: " + locator);
  +         throw new RuntimeException("Unable to create client invoker for locator: " + locator, e);
  +      }
  +      
  +      if (!clientInvoker.isConnected())
  +      {
  +         if (trace) { log.trace("inner client invoker not connected, connecting ..."); }
  +         clientInvoker.connect();
  +      }
  +      
         TimerUtil.schedule(this, pingPeriod);
         stopped = false;
   
  @@ -78,9 +111,21 @@
   
      public void stop()
      {
  +      if (stopped)
  +         return;
  +      
  +      doStop();
  +   }
  +   
  +   public boolean cancel()
  +   {
  +      return doStop();
  +   }
  +
  +   protected boolean doStop()
  +   {
         synchronized(lock)
         {
  -         cancel();
            if (!listeners.isEmpty())
            {
               listeners.clear();
  @@ -88,7 +133,14 @@
            stopped = true;
         }
   
  +      if (clientInvoker != null)
  +      {
  +         InvokerRegistry.destroyClientInvoker(locator, configMap);
  +      }
  +      
  +      TimerUtil.unschedule(this);
         log.debug(this + " stopped");
  +      return super.cancel();
      }
   
   
  @@ -137,8 +189,7 @@
               {
                  if (trace) { log.trace(this + " pinging ..."); }
   
  -               boolean isValid =
  -                  checkConnection(client.getInvoker().getLocator(), client.getConfiguration());
  +               boolean isValid = doCheckConnection(clientInvoker);
   
                  if (!isValid)
                  {
  @@ -156,6 +207,7 @@
         }
      }
   
  +   
      private void notifyListeners(Throwable thr)
      {
         final Throwable t = thr;
  @@ -192,7 +244,7 @@
         boolean pingWorked = false;
   
         Map configMap = null;
  -      if (configMap == null)
  +      if (config == null)
         {
            configMap = new HashMap();
         }
  @@ -215,37 +267,55 @@
               innerClientInvoker.connect();
            }
   
  -         if (trace)
  +         pingWorked = doCheckConnection(innerClientInvoker);
  +      }
  +      catch (Throwable throwable)
            {
  -            log.trace("ConnectionValidator pinging " +
  +         log.debug("ConnectionValidator to connect to server " +
                  innerClientInvoker.getLocator().getProtocol() + "://" +
                  innerClientInvoker.getLocator().getHost() + ":" +
  -               innerClientInvoker.getLocator().getPort());
  +            innerClientInvoker.getLocator().getPort(), throwable);
  +      }
  +      finally
  +      {
  +         if (innerClientInvoker != null)
  +         {
  +            InvokerRegistry.destroyClientInvoker(locator, configMap);
  +         }
  +      }
  +
  +      return pingWorked;
  +   }
  +   
  +   
  +   protected static boolean doCheckConnection(ClientInvoker clientInvoker) throws Throwable
  +   {
  +      boolean pingWorked = false;
  +
  +      if (trace)
  +      {
  +         log.trace("ConnectionValidator pinging " +
  +               clientInvoker.getLocator().getProtocol() + "://" +
  +               clientInvoker.getLocator().getHost() + ":" +
  +               clientInvoker.getLocator().getPort());
            }
   
  +      try
  +      {
            /**
             * Sending null client id as don't want to trigger lease on server side.
             * This also means that client connection validator will NOT impact client
             * lease, so can not depend on it to maintain client lease with the server.
             */
  -         innerClientInvoker.
  -            invoke(new InvocationRequest(null, Subsystem.SELF, "$PING$", null, null, null));
  -
  +         clientInvoker.invoke(new InvocationRequest(null, Subsystem.SELF, "$PING$", null, null, null));
            pingWorked = true;
         }
         catch (Throwable throwable)
         {
            log.debug("ConnectionValidator failed to ping server " +
  -            innerClientInvoker.getLocator().getProtocol() + "://" +
  -            innerClientInvoker.getLocator().getHost() + ":" +
  -            innerClientInvoker.getLocator().getPort(), throwable);
  -      }
  -      finally
  -      {
  -         if (innerClientInvoker != null)
  -         {
  -            InvokerRegistry.destroyClientInvoker(locator, configMap);
  -         }
  +            clientInvoker.getLocator().getProtocol() + "://" +
  +            clientInvoker.getLocator().getHost() + ":" +
  +            clientInvoker.getLocator().getPort(), throwable);
         }
   
         return pingWorked;
  
  
  



More information about the jboss-cvs-commits mailing list