[exo-jcr-commits] exo-jcr SVN: r4069 - jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Mar 10 07:34:44 EST 2011


Author: nfilotto
Date: 2011-03-10 07:34:43 -0500 (Thu, 10 Mar 2011)
New Revision: 4069

Modified:
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionRegistry.java
Log:
EXOJCR-1231: Help applications to prevent memory leaks by enabling the SessionCleaner by default

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionRegistry.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionRegistry.java	2011-03-10 12:28:09 UTC (rev 4068)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionRegistry.java	2011-03-10 12:34:43 UTC (rev 4069)
@@ -18,6 +18,7 @@
  */
 package org.exoplatform.services.jcr.impl.core;
 
+import org.exoplatform.container.ExoContainerContext;
 import org.exoplatform.management.annotations.Managed;
 import org.exoplatform.management.annotations.ManagedDescription;
 import org.exoplatform.management.jmx.annotations.NameTemplate;
@@ -44,12 +45,17 @@
    // 1 min
    public final static int DEFAULT_CLEANER_TIMEOUT = 60 * 1000;
 
+   // 10 mins
+   public final static int DEFAULT_SESSION_TIMEOUT = 10 * 60 * 1000;
+
    protected static Log log = ExoLogger.getLogger("exo.jcr.component.core.SessionRegistry");
 
-   private SessionCleaner sessionCleaner;
+   private volatile SessionCleaner sessionCleaner;
 
-   protected long timeOut;
+   private String repositoryId;
 
+   protected volatile long timeOut;
+
    @Managed
    @ManagedDescription("How many sessions are currently active")
    public int getSize()
@@ -65,26 +71,61 @@
    }
 
    @Managed
-   @ManagedDescription("Perform a cleanup of timed out sessions")
-   public void runCleanup()
+   @ManagedDescription("Set the session time out in seconds")
+   public void setTimeOut(long timeout)
    {
-      try
+      this.timeOut = timeout <= 0 ? 0 : timeout * 1000;
+      if (timeOut == 0 && sessionCleaner != null)
       {
-         sessionCleaner.callPeriodically();
+         // We set a time out to 0 so we disable the cleaner
+         this.sessionCleaner.halt();
+         this.sessionCleaner = null;
+         if (log.isDebugEnabled())
+         {
+            log.debug("Stop the previous session cleaner");
+         }
       }
-      catch (Exception e)
+      else if (timeOut > 0 && sessionCleaner == null)
       {
-         e.printStackTrace();
+         // We set a time out greater than 0, so we enable the cleaner
+         this.sessionCleaner = new SessionCleaner(repositoryId, DEFAULT_CLEANER_TIMEOUT, timeOut);      
+         if (log.isDebugEnabled())
+         {
+            log.debug("Start a new session cleaner");            
+         }
       }
    }
 
+   @Managed
+   @ManagedDescription("Perform a cleanup of timed out sessions")
+   public void runCleanup()
+   {
+      if (sessionCleaner != null)
+      {
+         try
+         {
+            sessionCleaner.callPeriodically();
+         }
+         catch (Exception e)
+         {
+            log.warn("Could not execute the cleanup command", e);
+         }
+      }
+   }
+
    public SessionRegistry(RepositoryEntry entry)
    {
+      this(null, entry);
+   }
+
+   public SessionRegistry(ExoContainerContext ctx, RepositoryEntry entry)
+   {
       sessionsMap = new ConcurrentHashMap<String, SessionImpl>();
       if (entry != null)
       {
-         this.timeOut = entry.getSessionTimeOut() > 0 ? entry.getSessionTimeOut() : 0;
+         this.timeOut = entry.getSessionTimeOut() > 0 ? entry.getSessionTimeOut() : DEFAULT_SESSION_TIMEOUT;
       }
+      this.repositoryId = ctx != null ? ctx.getName() : (entry == null ? null : entry.getName());
    }
 
    public void registerSession(SessionImpl session)
@@ -128,7 +169,7 @@
       sessionsMap.clear();
 
       if (timeOut > 0)
-         sessionCleaner = new SessionCleaner(DEFAULT_CLEANER_TIMEOUT, timeOut);
+         sessionCleaner = new SessionCleaner(repositoryId, DEFAULT_CLEANER_TIMEOUT, timeOut);
    }
 
    public void stop()
@@ -167,11 +208,11 @@
 
       private final long sessionTimeOut;
 
-      public SessionCleaner(long workTime, long sessionTimeOut)
+      public SessionCleaner(String id, long workTime, long sessionTimeOut)
       {
          super(workTime);
          this.sessionTimeOut = sessionTimeOut;
-         setName("SessionCleaner " + getId());
+         setName("SessionCleaner " + (id == null ? getId() : id));
          setPriority(Thread.MIN_PRIORITY);
          setDaemon(true);
          start();



More information about the exo-jcr-commits mailing list