[jboss-remoting-commits] JBoss Remoting SVN: r3490 - remoting2/branches/2.x/src/main/org/jboss/remoting.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Sat Feb 23 01:30:51 EST 2008


Author: ron.sigal at jboss.com
Date: 2008-02-23 01:30:51 -0500 (Sat, 23 Feb 2008)
New Revision: 3490

Modified:
   remoting2/branches/2.x/src/main/org/jboss/remoting/Client.java
Log:
JBREM-877: Added Timer for delayed invoker destruction.

Modified: remoting2/branches/2.x/src/main/org/jboss/remoting/Client.java
===================================================================
--- remoting2/branches/2.x/src/main/org/jboss/remoting/Client.java	2008-02-22 22:32:34 UTC (rev 3489)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/Client.java	2008-02-23 06:30:51 UTC (rev 3490)
@@ -59,6 +59,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
 
 /**
  * Client is a convience class for invoking remote methods for a given subsystem. It is intended to
@@ -151,6 +153,11 @@
     */
    public static final int DEFAULT_DISCONNECT_TIMEOUT = -1;
    
+   /**
+    * Key for setting delay before client invoker is destroyed by disconnect().
+    */
+   public static final String INVOKER_DESTRUCTION_DELAY = "invokerDestructionDelay";
+   
    public static final String THROW_CALLBACK_EXCEPTION = "throwCallbackException";
 
    private static final Logger log = Logger.getLogger(Client.class);
@@ -187,6 +194,10 @@
    private int disconnectTimeout = DEFAULT_DISCONNECT_TIMEOUT;
 
    private boolean connected = false;
+   
+   private int invokerDestructionDelay = 0;
+   private Timer invokerDestructionTimer;
+   private Object invokerDestructionTimerLock = new Object();
 
    // Constructors ---------------------------------------------------------------------------------
 
@@ -266,6 +277,7 @@
          this.configuration = new HashMap(configuration);
       }
       this.sessionId = new GUID().toString();
+      processParameters();
    }
 
    /**
@@ -486,7 +498,35 @@
          // Need to remove myself from registry so will not keep reference to me since I am of no
          // use now. Will have to create a new one.
 
-         InvokerRegistry.destroyClientInvoker(invoker.getLocator(), configuration);
+         if (invokerDestructionDelay > 0)
+         {
+            synchronized (invokerDestructionTimerLock)
+            {
+               InvokerDestructionTimerTask task = new InvokerDestructionTimerTask(invoker);
+               if (invokerDestructionTimer == null)
+               {
+                  invokerDestructionTimer = new Timer(true);
+               }
+
+               try
+               {
+                  invokerDestructionTimer.schedule(task, invokerDestructionDelay);
+               }
+               catch (IllegalStateException e)
+               {
+                  log.debug("Unable to schedule InvokerDestructionTimerTask on existing Timer", e);
+                  invokerDestructionTimer = new Timer(true);
+                  invokerDestructionTimer.schedule(task, invokerDestructionDelay);
+               }
+               
+               log.trace(this + " scheduled destruction of " + invoker);
+            }
+         }
+         else
+         {
+            InvokerRegistry.destroyClientInvoker(invoker.getLocator(), configuration);
+         }
+         
          invoker = null;
       }
 
@@ -1725,7 +1765,49 @@
       }
       return listenerId;
    }
+   
+   private void processParameters()
+   {
+      Map params = new HashMap();
+      if (configuration != null)
+         params.putAll(configuration);
+      if (locator.getParameters() != null)
+         params.putAll(locator.getParameters());
+      
+      Object param = params.get(INVOKER_DESTRUCTION_DELAY);
+      if (param instanceof String)
+      {
+         try
+         {
+            invokerDestructionDelay = Integer.parseInt((String) param);
+            log.debug(this + " setting invokerDestructionDelay to " + invokerDestructionDelay);
+         }
+         catch (NumberFormatException  e)
+         {
+            log.error("invokerDestructionDelay parameter has invalid format: " + param);
+         }
+      }
+      else if (param != null)
+      {
+         log.error("invokerDestructionDelay parameter must be a string in integer format: " + param);
+      }
+   }
 
 
    // Inner classes --------------------------------------------------------------------------------
+   class InvokerDestructionTimerTask extends TimerTask
+   {
+      private ClientInvoker invoker;
+      
+      public InvokerDestructionTimerTask(ClientInvoker invoker)
+      {
+         this.invoker = invoker;
+      }
+      
+      public void run()
+      {
+         log.trace("calling InvokerRegistry.destroyClientInvoker() for " + invoker);
+         InvokerRegistry.destroyClientInvoker(invoker.getLocator(), configuration);
+      }
+   }
 }




More information about the jboss-remoting-commits mailing list