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

Tom Elrod tom.elrod at jboss.com
Wed Sep 20 00:22:26 EDT 2006


  User: telrod  
  Date: 06/09/20 00:22:26

  Modified:    src/main/org/jboss/remoting        Client.java Lease.java
                        LeasePinger.java MicroRemoteClientInvoker.java
                        RemoteClientInvoker.java ServerInvoker.java
  Added:       src/main/org/jboss/remoting        ClientHolder.java
  Log:
  JBREM-596 - fix for leasing problem when have multiple clients using same client invoker.  Basically moved all lease managment for clients into the client invokers themselves.
  
  Revision  Changes    Path
  1.49      +32 -55    JBossRemoting/src/main/org/jboss/remoting/Client.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Client.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/Client.java,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -b -r1.48 -r1.49
  --- Client.java	15 Aug 2006 16:41:16 -0000	1.48
  +++ Client.java	20 Sep 2006 04:22:26 -0000	1.49
  @@ -59,7 +59,7 @@
    *
    * @author <a href="mailto:jhaynie at vocalocity.net">Jeff Haynie</a>
    * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
  - * @version $Revision: 1.48 $
  + * @version $Revision: 1.49 $
    */
   public class Client implements Externalizable
   {
  @@ -135,7 +135,6 @@
      private InvokerLocator locator;
   
      private ConnectionValidator connectionValidator = null;
  -   private LeasePinger leasePinger = null;
      private Map configuration = null;
   
      private boolean enableLease = false;
  @@ -378,26 +377,32 @@
            }
            invoker = InvokerRegistry.createClientInvoker(locator, configuration);
         }
  -      if (!isConnected())
  -      {
            connect(invoker);
         }
  -   }
   
      private void connect(ClientInvoker invoker)
      {
         if (invoker != null)
         {
            invoker.connect();
  +         try
  +         {
            setupClientLease(invoker);
         }
  +         catch (Throwable throwable)
  +         {
  +            RuntimeException e = new RuntimeException("Error setting up client lease upon performing connect.");
  +            e.initCause(throwable);
  +            throw e;
  +         }
  +      }
         else
         {
            throw new RuntimeException("Client invoker is null (may have used void constructor for Client, which should only be used for Externalization.");
         }
      }
   
  -   private void setupClientLease(ClientInvoker invoker)
  +   private void setupClientLease(ClientInvoker invoker) throws Throwable
      {
   
         // start with checking the locator url for hint as to if should do initial lease ping
  @@ -456,46 +461,24 @@
                  log.warn("Can not evaluate " + ENABLE_LEASE + " value (" + val + ") as a boolean type.");
               }
            }
  -      }
  -
  -      if (enableLease)
  +         String leasePeriodValue = (String) configuration.get(InvokerLocator.CLIENT_LEASE_PERIOD);
  +         if (leasePeriodValue != null && leasePeriodValue.length() > 0)
         {
  -         Object ret = null;
            try
            {
  -            ret = invoker.invoke(new InvocationRequest(sessionId, subsystem, "$PING$", configuration, new HashMap(), null));
  -            if (ret instanceof InvocationResponse)
  -            {
  -               InvocationResponse resp = (InvocationResponse) ret;
  -               Boolean shouldLease = (Boolean) resp.getResult();
  -               if (shouldLease.booleanValue())
  -               {
  -
  -                  // if lease period not set via locator param, check value returned by server
  -                  if (leasePeriod < 0)
  -                  {
  -                     Map respMap = resp.getPayload();
  -                     if (respMap != null)
  -                     {
  -                        Long leaseTimeoutValue = (Long) respMap.get("clientLeasePeriod");
  -                        leasePeriod = leaseTimeoutValue.longValue();
  -                     }
  +               leasePeriod = Long.parseLong(leasePeriodValue);
                     }
  -                  if (leasePeriod > 0)
  -                  {
  -                     if (leasePinger == null)
  +            catch (NumberFormatException e)
                        {
  -                        leasePinger = new LeasePinger(this);
  -                        leasePinger.startPing(leasePeriod);
  -                     }
  -                  }
  +               log.warn("Could not convert client lease period value (" + leasePeriodValue + ") to a number.");
                  }
               }
  +
            }
  -         catch (Throwable throwable)
  +
  +      if (enableLease)
            {
  -            log.error("Error setting up client lease.", throwable);
  -         }
  +         invoker.establishLease(sessionId, configuration, leasePeriod);
         }
      }
   
  @@ -508,20 +491,14 @@
       */
      public void disconnect()
      {
  -      if (leasePinger != null)
  -      {
  -         try
  +      if (invoker != null)
            {
  -            invoker.invoke(new InvocationRequest(sessionId, subsystem, "$DISCONNECT$", null, null, null));
  -         }
  -         catch (Throwable throwable)
  +         if(enableLease)
            {
  -            log.error("Error sending disconnect to server to end client lease.", throwable);
  +            invoker.terminateLease(sessionId);
  +            enableLease = false;
            }
  -         leasePinger.stopPing();
  -      }
  -      if (invoker != null)
  -      {
  +
            /**
             * Need to remove myself from registry so will not keep
             * reference to me since I am of no use now. Will have to create
  
  
  
  1.9       +122 -15   JBossRemoting/src/main/org/jboss/remoting/Lease.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Lease.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/Lease.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -b -r1.8 -r1.9
  --- Lease.java	7 Apr 2006 02:32:32 -0000	1.8
  +++ Lease.java	20 Sep 2006 04:22:26 -0000	1.9
  @@ -21,12 +21,14 @@
   */
   package org.jboss.remoting;
   
  -import java.util.Map;
  -import java.util.TimerTask;
  -
   import org.jboss.logging.Logger;
   import org.jboss.remoting.util.TimerUtil;
   
  +import java.util.Collection;
  +import java.util.Iterator;
  +import java.util.Map;
  +import java.util.TimerTask;
  +
   /**
    * This class is used on the remoting server to maintain lease information
    * for remoting clients.  Will generate callback to ConnectionListener interface
  @@ -50,7 +52,7 @@
      private boolean leaseUpdated = false;
      
      private static final Logger log = Logger.getLogger(Lease.class);
  -
  +   private static final boolean isTraceEnabled = log.isTraceEnabled();
   
      public Lease(String clientSessionId, long leasePeriod, String locatorurl, Map requestPayload,
                   ConnectionNotifier notifier, Map clientLeases)
  @@ -67,10 +69,20 @@
   
      public void startLease()
      {
  +      if(isTraceEnabled)
  +      {
  +         log.trace("Starting lease for client invoker (session id = " + clientSessionId + ") with lease window time of " + leaseWindow);
  +      }
         leaseTimerTask = new LeaseTimerTask();
         TimerUtil.schedule(leaseTimerTask, leaseWindow);
      }
   
  +   public void updateLease(long leasePeriod, Map requestMap)
  +   {
  +      this.requestPayload = requestMap;
  +      updateLease(leasePeriod);
  +   }
  +
      public void updateLease(long leasePeriod)
      {      
         leaseUpdated = true;      
  @@ -80,6 +92,10 @@
            this.leaseWindow = leasePeriod * 2;
            stopLease();
            startLease();
  +         if(isTraceEnabled)
  +         {
  +            log.trace("Lease for client invoker (session id = " + clientSessionId + ") updated with new lease window of " + leaseWindow + ".  Resetting timer.");
  +         }
         }
         else
         {
  @@ -100,10 +116,101 @@
         pingStart = System.currentTimeMillis();
      }
   
  -   public void terminateLease(long leasePeriod)
  +   public void terminateLease(String sessionId)
  +   {
  +      if(isTraceEnabled)
  +      {
  +         log.trace("Terminating lease for session id " + sessionId);
  +      }
  +      // is this terminate for all clients
  +      if (clientSessionId.equals(sessionId))
      {
         stopLease();
  -      notifier.connectionTerminated(locatorURL, clientSessionId, requestPayload);
  +         // should be ok to call this will null as all the client should have
  +         // already been disconnected and there been a notification for each
  +         // of these client disconnections (which would remove the client from
  +         // the lease, thus leaving the collection empty
  +         notifyClientTermination(null);
  +      }
  +      else
  +      {
  +         notifyClientTermination(sessionId);
  +      }
  +   }
  +
  +   private void notifyClientTermination(String sessionId)
  +   {
  +      // is for a particular client, so need to inspect request payload for client
  +      if (requestPayload != null)
  +      {
  +         // should notify for one client or all?
  +         if (sessionId != null)
  +         {
  +            Object clientHolderObj = requestPayload.remove(sessionId);
  +            if (clientHolderObj != null && clientHolderObj instanceof ClientHolder)
  +            {
  +               ClientHolder clientHolder = (ClientHolder) clientHolderObj;
  +               notifier.connectionTerminated(locatorURL, clientHolder.getSessionId(), clientHolder.getConfig());
  +               if(isTraceEnabled)
  +               {
  +                  log.trace("Notified connection listener of lease termination due to disconnect from client (client session id = " + clientHolder.getSessionId());
  +               }
  +            }
  +         }
  +         else
  +         {
  +            // loop through and notify for all clients
  +            Collection clientHoldersCol = requestPayload.values();
  +            if (clientHoldersCol != null && clientHoldersCol.size() > 0)
  +            {
  +               Iterator itr = clientHoldersCol.iterator();
  +               while (itr.hasNext())
  +               {
  +                  Object val = itr.next();
  +                  if (val != null && val instanceof ClientHolder)
  +                  {
  +                     ClientHolder clientHolder = (ClientHolder) val;
  +                     notifier.connectionTerminated(locatorURL, clientHolder.getSessionId(), clientHolder.getConfig());
  +                     if(isTraceEnabled)
  +                     {
  +                        log.trace("Notified connection listener of lease termination due to disconnect from client (client session id = " + clientHolder.getSessionId());
  +                     }
  +                  }
  +               }
  +            }
  +         }
  +      }
  +      else
  +      {
  +         log.warn("Tried to terminate lease for session id " + sessionId + ", but no collection of clients have been set.");
  +      }
  +   }
  +
  +   private void notifyClientLost()
  +   {
  +      // is not for a particular client (but all clients associated with client invoker), so need to inspect request payload for client
  +      if (requestPayload != null)
  +      {
  +         // loop through and notify for all clients
  +         Collection clientHoldersCol = requestPayload.values();
  +         if (clientHoldersCol != null && clientHoldersCol.size() > 0)
  +         {
  +            Iterator itr = clientHoldersCol.iterator();
  +            while (itr.hasNext())
  +            {
  +               Object val = itr.next();
  +               if (val != null && val instanceof ClientHolder)
  +               {
  +                  ClientHolder clientHolder = (ClientHolder) val;
  +                  notifier.connectionLost(locatorURL, clientHolder.getSessionId(), clientHolder.getConfig());
  +                  if(isTraceEnabled)
  +                  {
  +                     log.trace("Notified connection listener of lease expired due to lost connection from client (client session id = " + clientHolder.getSessionId());
  +                  }
  +               }
  +            }
  +         }
  +      }
      }
   
      private void stopLease()
  @@ -126,7 +233,7 @@
            else
            {
               stopLease();
  -            notifier.connectionLost(locatorURL, clientSessionId, requestPayload);
  +            notifyClientLost();
               if (clientLeases != null)
               {
                  clientLeases.remove(clientSessionId);
  
  
  
  1.6       +181 -16   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.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- LeasePinger.java	30 Mar 2006 19:49:07 -0000	1.5
  +++ LeasePinger.java	20 Sep 2006 04:22:26 -0000	1.6
  @@ -1,52 +1,217 @@
   package org.jboss.remoting;
   
  -import java.util.TimerTask;
  -
   import org.jboss.logging.Logger;
  -
  +import org.jboss.remoting.transport.ClientInvoker;
   import org.jboss.remoting.util.TimerUtil;
   
  +import java.util.Collection;
  +import java.util.HashMap;
  +import java.util.Iterator;
  +import java.util.Map;
  +import java.util.TimerTask;
  +
   /**
    * Internal agent class to ping the remote server to keep lease alive.
    *
    * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
    */
  -public class LeasePinger extends TimerTask
  +public class LeasePinger
   {
  -   private Client client = null;
  +   private ClientInvoker client = null;
      private long pingPeriod = -1;
  +   private long defaultPingPeriod = -1;
  +   private String invokerSessionId = null;
  +   private Map clients = new HashMap();
  +   private TimerTask timerTask = null;
   
      private static final Logger log = Logger.getLogger(LeasePinger.class);
  +   private static final boolean isTraceEnabled = log.isTraceEnabled();
   
  -   public LeasePinger(Client remotingClient)
  +   public LeasePinger(ClientInvoker remotingClient, String invokerSessionId, long defaultLeasePeriod)
      {
         this.client = remotingClient;
  +      this.pingPeriod = defaultLeasePeriod;
  +      this.defaultPingPeriod = defaultLeasePeriod;
  +      this.invokerSessionId = invokerSessionId;
      }
   
  -   public void startPing(long leaseTimeout)
  +   public void startPing()
      {
  -      this.pingPeriod = leaseTimeout;
  -      TimerUtil.schedule(this, pingPeriod);
  +      if(isTraceEnabled)
  +      {
  +         log.trace("Starting lease timer for client invoker (session id " + invokerSessionId + ") with ping period of " + pingPeriod);
  +      }
  +      timerTask = new LeaseTimerTask();
  +      TimerUtil.schedule(timerTask, pingPeriod);
      }
   
      public void stopPing()
      {
  -      cancel();
  +      if(isTraceEnabled)
  +      {
  +         log.trace("Stopping lease timer for client invoker (session id " + invokerSessionId + ")");
  +      }
  +      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;
  +         }
  +      }
      }
   
  -   public void run()
  +   protected void sendClientPing()
      {
  -      if(client != null)
  +      if (client != null)
         {
            try
            {
  -            client.invoke("$PING$");
  +            if(isTraceEnabled)
  +            {
  +               String clientSessionIds = "";
  +               if(clients != null)
  +               {
  +                  Collection col = clients.values();
  +                  Iterator itr = col.iterator();
  +                  while(itr.hasNext())
  +                  {
  +                     ClientHolder h = (ClientHolder)itr.next();
  +                     clientSessionIds = clientSessionIds + h.getSessionId() + "\n";
            }
  -         catch(Throwable throwable)
  +               }
  +               log.trace("Sending ping to server for client invoker (session id " + invokerSessionId + ".  " +
  +                         "Currently managing lease for following clients:\n" + clientSessionIds);
  +            } // end trace
  +
  +            client.invoke(new InvocationRequest(invokerSessionId, null, "$PING$", clients, null, null));
  +         }
  +         catch (Throwable throwable)
  +         {
  +            log.warn("Error sending lease ping to server for client invoker (session id " + invokerSessionId + ".");
  +         }
  +      }
  +   }
  +
  +   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 (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);
  +
  +      sendClientPing();
  +
  +      // if new client lease period is less than the current ping period, need to refresh to new one
  +      if (leasePeriod < pingPeriod)
  +      {
  +         pingPeriod = leasePeriod;
  +         // don't want to call stopPing() as that will send disconnect for client invoker
  +         if (timerTask != null)
            {
  -            //Don't swallow!
  -            log.warn("Error sending lease ping to server.");
  +            timerTask.cancel();
  +            timerTask = null;
  +         }
  +         startPing();
            }
         }
  +
  +   public boolean removeClient(String sessionId)
  +   {
  +      boolean isLastClientLease = false;
  +
  +      if(isTraceEnabled)
  +      {
  +         log.trace("Removing client (session id " + sessionId + ") from lease for client invoker (session id " + invokerSessionId + ")");
  +      }
  +      ClientHolder holder = (ClientHolder) clients.remove(sessionId);
  +      if (holder != null)
  +      {
  +         // send disconnect for this client
  +         try
  +         {
  +            Map clientMap = new HashMap();
  +            clientMap.put(ClientHolder.CLIENT_HOLDER_KEY, holder);
  +            client.invoke(new InvocationRequest(invokerSessionId, null, "$DISCONNECT$", clientMap, null, null));
  +            if(isTraceEnabled)
  +            {
  +               log.trace("Sent out disconnect message to server for lease tied to client session id " + sessionId);
  +            }
  +         }
  +         catch (Throwable throwable)
  +         {
  +            log.warn("Error sending disconnect for client lease where client session id is " + sessionId);
  +         }
  +      }
  +      else
  +      {
  +         log.warn("Tried to remove lease for client (session id " + sessionId + "), but did not exist for client invoker lease (session id " + invokerSessionId);
  +      }
  +
  +      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);
  +         }
  +      }
  +      else
  +      {
  +         // 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())
  +         {
  +            ClientHolder clientHolder = (ClientHolder) itr.next();
  +            long clientHolderLeasePeriod = clientHolder.getLeasePeriod();
  +            if (clientHolderLeasePeriod < tempPingPeriod)
  +            {
  +               tempPingPeriod = clientHolderLeasePeriod;
  +            }
  +         }
  +
  +         // was there a change in lease period?
  +         if (tempPingPeriod != pingPeriod)
  +         {
  +            // need to update to new ping period and reset timer
  +            pingPeriod = tempPingPeriod;
  +
  +            if (timerTask != null)
  +            {
  +               timerTask.cancel();
  +               timerTask = null;
  +            }
  +            startPing();
  +         }
  +
  +      }
  +      return isLastClientLease;
  +   }
  +
  +   private class LeaseTimerTask extends TimerTask
  +   {
  +
  +      public void run()
  +      {
  +         sendClientPing();
  +      }
      }
   }
  
  
  
  1.6       +88 -5     JBossRemoting/src/main/org/jboss/remoting/MicroRemoteClientInvoker.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: MicroRemoteClientInvoker.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/MicroRemoteClientInvoker.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- MicroRemoteClientInvoker.java	22 May 2006 13:35:07 -0000	1.5
  +++ MicroRemoteClientInvoker.java	20 Sep 2006 04:22:26 -0000	1.6
  @@ -1,5 +1,6 @@
   package org.jboss.remoting;
   
  +import org.jboss.logging.Logger;
   import org.jboss.remoting.loading.ClassByteClassLoader;
   import org.jboss.remoting.loading.RemotingClassLoader;
   import org.jboss.remoting.marshal.InvalidMarshallingResource;
  @@ -7,8 +8,10 @@
   import org.jboss.remoting.marshal.Marshaller;
   import org.jboss.remoting.marshal.UnMarshaller;
   import org.jboss.remoting.transport.ClientInvoker;
  +import org.jboss.util.id.GUID;
   
   import java.io.IOException;
  +import java.util.HashMap;
   import java.util.Map;
   
   /**
  @@ -24,7 +27,7 @@
    * 
    * @author <a href="mailto:jhaynie at vocalocity.net">Jeff Haynie</a>
    * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
  - * @version $Revision: 1.5 $
  + * @version $Revision: 1.6 $
    */
   public abstract class MicroRemoteClientInvoker extends AbstractInvoker implements ClientInvoker
   {
  @@ -32,6 +35,12 @@
      private Marshaller marshaller;
      private UnMarshaller unmarshaller;
      private String dataType;
  +   private final Object clientLeaseLock = new Object();
  +   private LeasePinger leasePinger = null;
  +   private String invokerSessionId = new GUID().toString();
  +
  +   private static final Logger log = Logger.getLogger(MicroRemoteClientInvoker.class);
  +   private static final boolean isTraceEnabled = log.isTraceEnabled();
   
      public MicroRemoteClientInvoker(InvokerLocator locator)
      {
  @@ -56,7 +65,7 @@
         Object returnValue = null;
         int invokeCount = 0;
   
  -      if (log.isTraceEnabled())
  +      if (isTraceEnabled)
         {
            log.trace((++invokeCount) + ") invoking =>" + invocationReq + " with parameter: " + invocationReq.getParameter());
         }
  @@ -115,7 +124,7 @@
   
         returnValue = transport(invocationReq.getSessionId(), payload, metadata, marshaller, unmarshaller);
   
  -      if (log.isTraceEnabled())
  +      if (isTraceEnabled)
         {
            log.trace("received result=>" + returnValue);
         }
  @@ -125,7 +134,7 @@
         {
            InvocationResponse response = (InvocationResponse) returnValue;
            returnValue = response.getResult();
  -         if (log.isTraceEnabled())
  +         if (isTraceEnabled)
            {
               log.trace("received result was an InvocationResponse so going to return response's return value of " + returnValue);
               log.trace("response is exception = " + response.isException());
  @@ -313,6 +322,80 @@
         return this.unmarshaller;
      }
   
  +   public void terminateLease(String sessionId)
  +   {
  +      synchronized(clientLeaseLock)
  +      {
  +         if(leasePinger != null)
  +         {
  +            boolean isLastClientLease = leasePinger.removeClient(sessionId);
  +            if(isLastClientLease)
  +            {
  +               leasePinger.stopPing();
  +               leasePinger = null;
  +            }
  +         }
  +      }
  +   }
  +
  +   public void establishLease(String sessionId, Map configuration, long leasePeriod) throws Throwable
  +   {
  +      synchronized (clientLeaseLock)
  +      {
  +         // if already have a lease pinger, then already have a client with an established
  +         // lease and just need to update the lease pinger
  +         if (leasePinger != null)
  +         {
  +            leasePinger.addClient(sessionId, configuration, leasePeriod);
  +         }
  +         else
  +         {
  +            try
  +            {
  +               if(isTraceEnabled)
  +               {
  +                  log.trace("Sending initial lease ping to server for client invoker (session id " + invokerSessionId + ") to determine " +
  +                            "if server has leasing enabled.");
  +               }
  +               // configuration should NOT be passed as want ping to be specific to client invoker
  +               // and NOT to the client.
  +               Object ret = invoke(new InvocationRequest(invokerSessionId, null, "$PING$", null, new HashMap(), null));
  +               if (ret instanceof InvocationResponse)
  +               {
  +                  InvocationResponse resp = (InvocationResponse) ret;
  +                  Boolean shouldLease = (Boolean) resp.getResult();
  +                  if (shouldLease.booleanValue())
  +                  {
  +                     long defaultLeasePeriod = 5000;
  +                     Map respMap = resp.getPayload();
  +                     if (respMap != null)
  +                     {
  +                        Long leaseTimeoutValue = (Long) respMap.get("clientLeasePeriod");
  +                        defaultLeasePeriod = leaseTimeoutValue.longValue();
  +                     }
  +
  +                     if(isTraceEnabled)
  +                     {
  +                        log.trace("Server does have leasing enabled (with default lease period of " + defaultLeasePeriod + ") and will start a new lease pinger.");
  +                     }
  +                     leasePinger = new LeasePinger(this, invokerSessionId, defaultLeasePeriod);
  +                     leasePinger.addClient(sessionId, configuration, leasePeriod);
  +                     leasePinger.startPing();
  +                  }
  +               }
  +            }
  +            catch (Throwable throwable)
  +            {
  +               Exception e = new Exception("Error setting up client lease.");
  +               e.initCause(throwable);
  +               throw e;
  +            }
  +
  +         }
  +      }
  +
  +   }
  +
      /**
       * Will get the data type for the marshaller factory so know which marshaller to
       * get to marshal the data.  Will first check the locator uri for a 'datatype'
  
  
  
  1.24      +1 -1      JBossRemoting/src/main/org/jboss/remoting/RemoteClientInvoker.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: RemoteClientInvoker.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/RemoteClientInvoker.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -b -r1.23 -r1.24
  --- RemoteClientInvoker.java	20 Jun 2006 15:14:57 -0000	1.23
  +++ RemoteClientInvoker.java	20 Sep 2006 04:22:26 -0000	1.24
  @@ -31,7 +31,7 @@
    * setting socket factories.
    *
    * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
  - * @version $Revision: 1.23 $
  + * @version $Revision: 1.24 $
    */
   public abstract class RemoteClientInvoker extends MicroRemoteClientInvoker
   {
  
  
  
  1.45      +51 -9     JBossRemoting/src/main/org/jboss/remoting/ServerInvoker.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ServerInvoker.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/ServerInvoker.java,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -b -r1.44 -r1.45
  --- ServerInvoker.java	2 Aug 2006 07:34:54 -0000	1.44
  +++ ServerInvoker.java	20 Sep 2006 04:22:26 -0000	1.45
  @@ -58,7 +58,7 @@
    *
    * @author <a href="mailto:jhaynie at vocalocity.net">Jeff Haynie</a>
    * @author <a href="mailto:tom.elrod at jboss.com">Tom Elrod</a>
  - * @version $Revision: 1.44 $
  + * @version $Revision: 1.45 $
    */
   public abstract class ServerInvoker extends AbstractInvoker implements ServerInvokerMBean
   {
  @@ -572,7 +572,7 @@
            {
               String sessionId = (String)itr.next();
               Lease clientLease = (Lease)clientLeases.get(sessionId);
  -            clientLease.terminateLease(-1);
  +            clientLease.terminateLease(sessionId);
            }
            clientLeases.clear();
         }
  @@ -1016,16 +1016,42 @@
   
      private void terminateLease(InvocationRequest invocation)
      {
  -      if(invocation != null)
  +      if (invocation != null)
         {
            String clientSessionId = invocation.getSessionId();
  -         Lease clientLease = (Lease)clientLeases.get(clientSessionId);
  -         if(clientLease != null)
  +         Lease clientLease = (Lease) clientLeases.get(clientSessionId);
  +         if (clientLease != null)
  +         {
  +            boolean clientOnlyTerminated = false;
  +            // now have to determine if is just Client that disconnected
  +            // or if all Clients disconnected, thus the client invoker
  +            // is also disconnected as well.
  +            Map reqMap = invocation.getRequestPayload();
  +            if (reqMap != null)
  +            {
  +               Object holderObj = reqMap.get(ClientHolder.CLIENT_HOLDER_KEY);
  +               if (holderObj != null && holderObj instanceof ClientHolder)
  +               {
  +                  // just a client that disconnected, so only need to terminate lease for
  +                  // that particular client (by client session id).
  +                  ClientHolder holder = (ClientHolder) holderObj;
  +                  clientLease.terminateLease(holder.getSessionId());
  +                  clientOnlyTerminated = true;
  +               }
  +            }
  +
  +            // now see if client invoker needs to be terminated
  +            if (!clientOnlyTerminated)
            {
  -            clientLease.terminateLease(leasePeriod);
  +               clientLease.terminateLease(clientSessionId);
               clientLeases.remove(clientSessionId);
            }
         }
  +         else
  +         {
  +            log.warn("Asked to terminate lease for client session id " + clientSessionId + ", but lease for this id could not be found.");
  +         }
  +      }
      }
   
      private void updateClientLease(InvocationRequest invocation)
  @@ -1035,6 +1061,11 @@
            String clientSessionId = invocation.getSessionId();
            if(clientSessionId != null)
            {
  +            if(isTraceEnabled)
  +            {
  +               log.trace("Getting lease for client session id: " + clientSessionId);
  +            }
  +
               Lease clientLease = (Lease)clientLeases.get(clientSessionId);
               if(clientLease == null)
               {
  @@ -1044,10 +1075,21 @@
                                                   clientLeases);
                  clientLeases.put(clientSessionId, newClientLease);
                  newClientLease.startLease();
  +
  +               if(isTraceEnabled)
  +               {
  +                  log.trace("No lease established for client session id (" + clientSessionId + "), so starting a new one.");
  +               }
               }
               else
               {
  -               clientLease.updateLease(leasePeriod);
  +               // including request payload from invocation as may contain updated list of clients.
  +               clientLease.updateLease(leasePeriod, invocation.getRequestPayload());
  +
  +               if(isTraceEnabled)
  +               {
  +                  log.trace("Updated lease for client session id (" + clientSessionId + ")");
  +               }
               }
            }
         }
  
  
  
  1.1      date: 2006/09/20 04:22:26;  author: telrod;  state: Exp;JBossRemoting/src/main/org/jboss/remoting/ClientHolder.java
  
  Index: ClientHolder.java
  ===================================================================
  /*
  * JBoss, a division of Red Hat
  * Copyright 2006, Red Hat Middleware, LLC, 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.remoting;
  
  import java.io.Serializable;
  import java.util.Map;
  
  /**
   * @author <a href="mailto:tom at jboss.org">Tom Elrod</a>
   */
  public class ClientHolder implements Serializable
     {
        private String sessionId;
        private Map config;
        private long leasePeriod;
  
        public static final String CLIENT_HOLDER_KEY = "ClientHolderKey";
  
        public ClientHolder(String sessionId, Map config, long leasePeriod)
        {
           this.sessionId = sessionId;
           this.config = config;
           this.leasePeriod = leasePeriod;
        }
  
        public String getSessionId()
        {
           return sessionId;
        }
  
        public Map getConfig()
        {
           return config;
        }
  
        public long getLeasePeriod()
        {
           return leasePeriod;
        }
     }
  
  
  
  



More information about the jboss-cvs-commits mailing list