[jbosscache-commits] JBoss Cache SVN: r6480 - in core/trunk/src: main/java/org/jboss/cache/eviction and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Aug 1 09:12:47 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-08-01 09:12:46 -0400 (Fri, 01 Aug 2008)
New Revision: 6480

Added:
   core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java
Modified:
   core/trunk/src/main/java/org/jboss/cache/RegionImpl.java
   core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java
Log:
JBCACHE-1268 - allow disabling of eviction thread

Modified: core/trunk/src/main/java/org/jboss/cache/RegionImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/RegionImpl.java	2008-08-01 11:03:23 UTC (rev 6479)
+++ core/trunk/src/main/java/org/jboss/cache/RegionImpl.java	2008-08-01 13:12:46 UTC (rev 6480)
@@ -203,7 +203,7 @@
       }
       catch (InterruptedException e)
       {
-         log.debug("trace", e);
+         Thread.currentThread().interrupt();
       }
       return null;
    }

Modified: core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java	2008-08-01 11:03:23 UTC (rev 6479)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java	2008-08-01 13:12:46 UTC (rev 6480)
@@ -10,12 +10,13 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.jboss.cache.Region;
+import org.jboss.cache.util.concurrent.ConcurrentHashSet;
 
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.Set;
-import java.util.Timer;
-import java.util.TimerTask;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -32,13 +33,13 @@
    private final Set<Region> processedRegions;
    private static AtomicInteger tcount = new AtomicInteger();
    private long wakeupInterval;
-   private Timer evictionThread;
+   ScheduledExecutorService scheduledExecutor;
 
    public EvictionTimerTask()
    {
       // synchronized set because we need to maintain thread safety
       // for dynamic configuration purposes.
-      processedRegions = Collections.synchronizedSet(new HashSet<Region>());
+      processedRegions = new ConcurrentHashSet<Region>();
    }
 
    public void init(long wakeupInterval)
@@ -81,32 +82,40 @@
 
    public void stop()
    {
-      log.debug("Stopping eviction timer");
+      if (log.isDebugEnabled()) log.debug("Stopping eviction timer");
 
-      if (evictionThread != null)
+      if (scheduledExecutor != null)
       {
-         evictionThread.cancel();
+         scheduledExecutor.shutdownNow();
       }
-      evictionThread = null;
+      scheduledExecutor = null;
    }
 
    private void start()
    {
-      evictionThread = new Timer("EvictionTimer-" + tcount.getAndIncrement(), true);
-      TimerTask tt = new TimerTask()
+      if (wakeupInterval < 1)
       {
-         /**
-          * Run the eviction thread.
-          * <p/>
-          * This thread will synchronize the set of regions and iterate through every MarshRegion registered w/ the
-          * Eviction thread. It also synchronizes on each individual region as it is being processed.
-          */
+         if (log.isInfoEnabled())
+            log.info("Wakeup Interval set to " + wakeupInterval + ".  Not starting an eviction thread!");
+         return;
+      }
+      scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory()
+      {
+         public Thread newThread(Runnable r)
+         {
+            return new Thread(r, "EvictionTimer-" + tcount.getAndIncrement());
+         }
+      });
+      scheduledExecutor.scheduleWithFixedDelay(new Runnable()
+      {
          public void run()
          {
+            // Run the eviction thread.
+            // This thread will synchronize the set of regions and iterate through every MarshRegion registered w/ the
+            // Eviction thread. It also synchronizes on each individual region as it is being processed.
             processRegions();
          }
-      };
-      evictionThread.schedule(tt, wakeupInterval, wakeupInterval);
+      }, wakeupInterval, wakeupInterval, TimeUnit.MILLISECONDS);
    }
 
    private void processRegions()

Added: core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java	2008-08-01 13:12:46 UTC (rev 6480)
@@ -0,0 +1,53 @@
+package org.jboss.cache.eviction;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.RegionManager;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.Configuration.CacheMode;
+import org.jboss.cache.factories.ComponentRegistry;
+import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
+import org.jboss.cache.util.TestingUtil;
+import org.testng.annotations.Test;
+
+ at Test(groups = "functional")
+public class DisabledEvictionThreadTest
+{
+   public void testDisabledEvictionTimer()
+   {
+      Cache<String, String> c = null;
+      try
+      {
+         Configuration cfg = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.LOCAL, true);
+         cfg.getEvictionConfig().setWakeupInterval(0);
+         c = new DefaultCacheFactory<String, String>().createCache(cfg);
+         ComponentRegistry cr = TestingUtil.extractComponentRegistry(c);
+         RegionManager rm = cr.getComponent(RegionManager.class);
+         EvictionTimerTask ett = rm.getEvictionTimerTask();
+         assert ett.scheduledExecutor == null;
+      }
+      finally
+      {
+         TestingUtil.killCaches(c);
+      }
+   }
+
+   public void testControl()
+   {
+      Cache<String, String> c = null;
+      try
+      {
+         Configuration cfg = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.LOCAL, true);
+         cfg.getEvictionConfig().setWakeupInterval(10);
+         c = new DefaultCacheFactory<String, String>().createCache(cfg);
+         ComponentRegistry cr = TestingUtil.extractComponentRegistry(c);
+         RegionManager rm = cr.getComponent(RegionManager.class);
+         EvictionTimerTask ett = rm.getEvictionTimerTask();
+         assert ett.scheduledExecutor != null;
+      }
+      finally
+      {
+         TestingUtil.killCaches(c);
+      }
+   }
+}




More information about the jbosscache-commits mailing list