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

Ron Sigal ron_sigal at yahoo.com
Sun Mar 11 16:03:30 EDT 2007


  User: rsigal  
  Date: 07/03/11 16:03:30

  Modified:    src/main/org/jboss/remoting  LeasePinger.java
  Log:
  JBREM-721:  Made LeaseTimerrTask a static class that sets its LeasePinger reference to null when cancelled.  Also, synchronized with 2_x branch.
  
  Revision  Changes    Path
  1.12      +194 -90   JBossRemoting/src/main/org/jboss/remoting/LeasePinger.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: LeasePinger.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/LeasePinger.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -b -r1.11 -r1.12
  --- LeasePinger.java	18 Jan 2007 21:47:39 -0000	1.11
  +++ LeasePinger.java	11 Mar 2007 20:03:30 -0000	1.12
  @@ -1,127 +1,118 @@
   package org.jboss.remoting;
   
  -import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
   import org.jboss.logging.Logger;
   import org.jboss.remoting.transport.ClientInvoker;
   
  -import java.util.Collection;
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.Map;
   import java.util.Timer;
   import java.util.TimerTask;
   
  +import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
  +
   /**
    * Internal agent class to ping the remote server to keep lease alive.
    *
    * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
  + * @author <a href="mailto:ovidiu at ejboss.org">Ovidiu Feodorov</a>
    */
   public class LeasePinger
   {
  +   // Constants ------------------------------------------------------------------------------------
  +
  +   private static final Logger log = Logger.getLogger(LeasePinger.class);
  +
  +   public static final long DEFAULT_LEASE_PERIOD = 5000;
  +   public static final int DEFAULT_DISCONNECT_TIMEOUT = -1;
  +
  +   // Static ---------------------------------------------------------------------------------------
  +
  +   private static boolean trace = log.isTraceEnabled();
  +
      private static Timer timer = new Timer(true);
   
  -   private ClientInvoker client = null;
  -   private long pingPeriod = -1;
  +   // Attributes -----------------------------------------------------------------------------------
  +
      private long defaultPingPeriod = -1;
  -   private String invokerSessionId = null;
  +
  +   private ClientInvoker invoker = null;
  +   private String invokerSessionID = null;
  +
      private Map clients = new ConcurrentHashMap();
      private TimerTask timerTask = null;
   
  -   private static final Logger log = Logger.getLogger(LeasePinger.class);
  -   private static final boolean isTraceEnabled = log.isTraceEnabled();
  +   private long pingPeriod = -1;
  +   private int disconnectTimeout = DEFAULT_DISCONNECT_TIMEOUT;
  +
  +   // Constructors ---------------------------------------------------------------------------------
   
  -   public LeasePinger(ClientInvoker remotingClient, String invokerSessionId, long defaultLeasePeriod)
  +   public LeasePinger(ClientInvoker invoker, String invokerSessionID, long defaultLeasePeriod)
      {
  -      this.client = remotingClient;
  +      this.invoker = invoker;
  +      this.invokerSessionID = invokerSessionID;
         this.pingPeriod = defaultLeasePeriod;
         this.defaultPingPeriod = defaultLeasePeriod;
  -      this.invokerSessionId = invokerSessionId;
      }
   
  -   public synchronized void startPing()
  -   {
  -      if(isTraceEnabled)
  +   // Public ---------------------------------------------------------------------------------------
  +
  +   public void startPing()
         {
  -         log.trace("Starting lease timer for client invoker with session ID " + invokerSessionId + " with ping period of " + pingPeriod);
  -      }
  -      timerTask = new LeaseTimerTask();
  +      if(trace) { log.trace(this + " starting lease timer with ping period of " + pingPeriod); }
  +
  +      timerTask = new LeaseTimerTask(this);
         timer.schedule(timerTask, pingPeriod, pingPeriod);
      }
   
  -   public synchronized void stopPing()
  +   public void stopPing()
      {
  -      if(isTraceEnabled)
  -      {
  -         log.trace("Stopping lease timer for client invoker with session ID " + invokerSessionId);
  -      }
  +      if(trace) { log.trace(this + " stopping lease timer"); }
  +
         if (timerTask != null)
         {
            timerTask.cancel();
            timerTask = null;
  -         try
  -         {
  -            // sending null for the request map, will indicate to server that is full disconnect (for client invoker)
  -            client.invoke(new InvocationRequest(invokerSessionId, null, "$DISCONNECT$", null, null, null));
  -         }
  -         catch (Throwable throwable)
  -         {
  -            RuntimeException e = new RuntimeException("Error tearing down lease with server.");
  -            e.initCause(throwable);
  -            throw e;
  -         }
  -      }
  -   }
   
  -   protected synchronized void sendClientPing()
  -   {
  -      if (client != null)
  -      {
            try
            {
  -            if(isTraceEnabled)
  -            {
  -               String clientSessionIds = "";
  -               if(clients != null)
  +            // sending request map with no ClientHolders will indicate to server
  +            // that is full disconnect (for client invoker)
  +            HashMap metadata = null;
  +            
  +            // If disconnectTimeout == 0, skip network i/o.
  +            if (disconnectTimeout != 0)
                  {
  -                  Collection col = clients.values();
  -                  Iterator itr = col.iterator();
  -                  while(itr.hasNext())
  +               if (disconnectTimeout > 0)
                     {
  -                     ClientHolder h = (ClientHolder)itr.next();
  -                     clientSessionIds = clientSessionIds + h.getSessionId() + "\n";
  +                  metadata = new HashMap(1);
  +                  metadata.put(ServerInvoker.TIMEOUT, Integer.toString(disconnectTimeout));
                     }
  +               InvocationRequest ir =
  +                  new InvocationRequest(invokerSessionID, null, "$DISCONNECT$", metadata, null, null);
  +               invoker.invoke(ir);
                  }
  -               log.trace("Sending ping to server for client invoker with session ID " + invokerSessionId + ".  " +
  -                         "Currently managing lease for following clients:\n" + clientSessionIds);
  -            } // end trace
  -
  -            Map clientsClone = new ConcurrentHashMap();
  -            clientsClone.putAll(clients);
  -            Map requestClients = new ConcurrentHashMap();
  -            requestClients.put(ClientHolder.CLIENT_HOLDER_KEY, clientsClone);
  -            client.invoke(new InvocationRequest(invokerSessionId, null, "$PING$", requestClients, null, null));
            }
            catch (Throwable throwable)
            {
  -            log.warn("Error sending lease ping to server for client invoker with session ID " + invokerSessionId);
  +            RuntimeException e = new RuntimeException("Error tearing down lease with server.");
  +            e.initCause(throwable);
  +            throw e;
            }
         }
      }
   
  -   public synchronized void addClient(String sessionId, Map configuration, long leasePeriod)
  +   public void addClient(String sessionID, Map configuration, long leasePeriod)
      {
         if (leasePeriod <= 0)
         {
            leasePeriod = defaultPingPeriod;
         }
   
  -      if(isTraceEnabled)
  -      {
  -         log.trace("Adding new client to lease for client invoker with session ID " + invokerSessionId + " where " +
  -                   "client session ID is " + sessionId + " and lease period is " + leasePeriod);
  -      }
  -      ClientHolder newClient = new ClientHolder(sessionId, configuration, leasePeriod);
  -      clients.put(sessionId, newClient);
  +      if(trace) { log.trace(this + " adding new client with session ID " + sessionID + " and lease period " + leasePeriod); }
  +
  +      ClientHolder newClient = new ClientHolder(sessionID, configuration, leasePeriod);
  +      clients.put(sessionID, newClient);
   
         sendClientPing();
   
  @@ -129,6 +120,7 @@
         if (leasePeriod < pingPeriod)
         {
            pingPeriod = leasePeriod;
  +
            // don't want to call stopPing() as that will send disconnect for client invoker
            if (timerTask != null)
            {
  @@ -139,15 +131,14 @@
         }
      }
   
  -   public synchronized boolean removeClient(String sessionId)
  +   public boolean removeClient(String sessionID)
      {
         boolean isLastClientLease = false;
   
  -      if(isTraceEnabled)
  -      {
  -         log.trace("Removing client with session ID " + sessionId + " from lease for client invoker with session ID " + invokerSessionId);
  -      }
  -      ClientHolder holder = (ClientHolder) clients.remove(sessionId);
  +      if(trace) { log.trace(this + " removing client with session ID " + sessionID); }
  +
  +      ClientHolder holder = (ClientHolder)clients.remove(sessionID);
  +      
         if (holder != null)
         {
            // send disconnect for this client
  @@ -155,39 +146,47 @@
            {
               Map clientMap = new HashMap();
               clientMap.put(ClientHolder.CLIENT_HOLDER_KEY, holder);
  -            client.invoke(new InvocationRequest(invokerSessionId, null, "$DISCONNECT$", clientMap, null, null));
  -            if(isTraceEnabled)
  +            
  +            // If disconnectTimeout == 0, skip network i/o.
  +            if (disconnectTimeout != 0)
               {
  -               log.trace("Sent out disconnect message to server for lease tied to client session ID " + sessionId);
  +               if (disconnectTimeout > 0)
  +                  clientMap.put(ServerInvoker.TIMEOUT, Integer.toString(disconnectTimeout));
  +               
  +               InvocationRequest ir = new InvocationRequest(invokerSessionID, null, "$DISCONNECT$",
  +                     clientMap, null, null);
  +               invoker.invoke(ir);
  +               
  +               if(trace) { log.trace(this + " sent out disconnect message to server for lease tied to client with session ID " + sessionID); }
               }
            }
            catch (Throwable throwable)
            {
  -            log.warn("Error sending disconnect for client lease where client session ID is " + sessionId);
  +            log.warn(this + " failed sending disconnect for client lease for " +
  +                  "client with session ID " + sessionID);
            }
         }
         else
         {
  -         log.warn("Tried to remove lease for client with session ID " + sessionId + ", but did not exist for client invoker lease (session ID " + invokerSessionId + ")");
  +         log.warn(this + " tried to remove lease for client with session ID " + sessionID +
  +         ", but no such lease was found");
         }
   
         if (clients.isEmpty())
         {
            isLastClientLease = true;
  -         if(isTraceEnabled)
  -         {
  -            log.trace("There are no more client leases tied to this client invoker's lease (session ID " + invokerSessionId + ")");
  -         }
  +         if(trace) { log.trace(this + " has no more client leases"); }
         }
         else
         {
  -         // now need to see if any of the other client holders have a lower lease period than default
  +         // now need to see if any of the other client holders have a lower lease period than
  +         // default
  +
            long tempPingPeriod = defaultPingPeriod;
  -         Collection clientHolders = clients.values();
  -         Iterator itr = clientHolders.iterator();
  -         while (itr.hasNext())
  +
  +         for (Iterator i = clients.values().iterator(); i.hasNext(); )
            {
  -            ClientHolder clientHolder = (ClientHolder) itr.next();
  +            ClientHolder clientHolder = (ClientHolder)i.next();
               long clientHolderLeasePeriod = clientHolder.getLeasePeriod();
               if (clientHolderLeasePeriod > 0 && clientHolderLeasePeriod < tempPingPeriod)
               {
  @@ -213,12 +212,117 @@
         return isLastClientLease;
      }
   
  -   private class LeaseTimerTask extends TimerTask
  +   public long getLeasePeriod(String sessionID)
  +   {
  +      if (timerTask == null)
      {
  +         return -1;
  +      }
  +
  +      // look to see if the client is still amont those serviced by this lease pinger
  +      if (clients.containsKey(sessionID))
  +      {
  +         return pingPeriod;
  +      }
  +      else
  +      {
  +         return -1;
  +      }
  +   }
  +
  +   public String toString()
  +   {
  +      return "LeasePinger[" + invoker + "(" + invokerSessionID + ")]";
  +   }
  +
  +   // Package protected ----------------------------------------------------------------------------
  +
  +   // Protected ------------------------------------------------------------------------------------
  +
  +   
  +   protected int getDisconnectTimeout()
  +   {
  +      return disconnectTimeout;
  +   }
  +   
  +   protected void setDisconnectTimeout(int disconnectTimeout)
  +   {
  +      this.disconnectTimeout = disconnectTimeout;
  +      log.debug(this + " setting disconnect timeout to: " + disconnectTimeout);
  +   }
  +   
  +   // Private --------------------------------------------------------------------------------------
  +
  +   private void sendClientPing()
  +   {
  +      try
  +      {
  +         if(trace)
  +         {
  +            StringBuffer sb = new StringBuffer();
  +            if(clients != null)
  +            {
  +               for(Iterator i = clients.values().iterator(); i.hasNext(); )
  +               {
  +                  ClientHolder h = (ClientHolder)i.next();
  +                  sb.append("    ").append(h.getSessionId()).append('\n');
  +               }
  +            }
  +
  +            log.trace(this + " sending ping to server. Currently managing lease " +
  +               "for following clients:\n" + sb.toString());
  +         }
  +
  +         Map clientsClone = new ConcurrentHashMap(clients);
  +         Map requestClients = new ConcurrentHashMap();
  +         requestClients.put(ClientHolder.CLIENT_HOLDER_KEY, clientsClone);
  +
  +         InvocationRequest ir =
  +            new InvocationRequest(invokerSessionID, null, "$PING$", requestClients, null, null);
  +
  +         invoker.invoke(ir);
  +
  +         if(trace) { log.trace(this + " successfully pinged the server"); }
  +      }
  +      catch (Throwable t)
  +      {
  +         log.debug(this + " failed to ping to server", t);
  +         log.warn(this + " failed to ping to server: " + t.getMessage());
  +      }
  +   }
  +
  +   // Inner classes --------------------------------------------------------------------------------
  +
  +   static private class LeaseTimerTask extends TimerTask
  +   {
  +      private LeasePinger pinger;
  +
  +      LeaseTimerTask(final LeasePinger pinger)
  +      {
  +          this.pinger = pinger;
  +      }
   
         public void run()
         {
  -         sendClientPing();
  +         final LeasePinger currentPinger;
  +         synchronized(this)
  +         {
  +             currentPinger = pinger;
  +         }
  +
  +         if (currentPinger != null)
  +         {
  +             currentPinger.sendClientPing();
  +         }
  +      }
  +
  +      public boolean cancel()
  +      {
  +          synchronized(this)
  +          {
  +              pinger = null;
  +          }
  +          return super.cancel();
         }
      }
   }
  
  
  



More information about the jboss-cvs-commits mailing list