camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/3] git commit: CAMEL-6862: DefaultTimeoutMap - Should use start/stop to schedule the task, and cancel the task
Date Mon, 14 Oct 2013 13:31:44 GMT
Updated Branches:
  refs/heads/camel-2.11.x 1765f1a2e -> fed68b407
  refs/heads/camel-2.12.x 0fc72e85a -> e7bf1703d
  refs/heads/master 45dea9ae5 -> 3a5ffd5a7


CAMEL-6862: DefaultTimeoutMap - Should use start/stop to schedule the task, and cancel the
task


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3a5ffd5a
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3a5ffd5a
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3a5ffd5a

Branch: refs/heads/master
Commit: 3a5ffd5a748371ba538b3986dd9f6f9df6f6df0e
Parents: 45dea9a
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Mon Oct 14 15:24:49 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Mon Oct 14 15:24:49 2013 +0200

----------------------------------------------------------------------
 .../apache/camel/support/DefaultTimeoutMap.java | 12 ++++++++--
 .../camel/support/DefaultTimeoutMapTest.java    | 25 +++++++++++++++++++-
 2 files changed, 34 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3a5ffd5a/camel-core/src/main/java/org/apache/camel/support/DefaultTimeoutMap.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/support/DefaultTimeoutMap.java b/camel-core/src/main/java/org/apache/camel/support/DefaultTimeoutMap.java
index 4f252fe..7dd60ee 100644
--- a/camel-core/src/main/java/org/apache/camel/support/DefaultTimeoutMap.java
+++ b/camel-core/src/main/java/org/apache/camel/support/DefaultTimeoutMap.java
@@ -25,6 +25,7 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -43,6 +44,8 @@ import org.slf4j.LoggerFactory;
  * You must provide a {@link java.util.concurrent.ScheduledExecutorService} in the constructor
which is used
  * to schedule a background task which check for old entries to purge. This implementation
will shutdown the scheduler
  * if its being stopped.
+ * You must also invoke {@link #start()} to startup the timeout map, before its ready to
be used.
+ * And you must invoke {@link #stop()} to stop the map when no longer in use.
  *
  * @version 
  */
@@ -52,6 +55,7 @@ public class DefaultTimeoutMap<K, V> extends ServiceSupport implements
TimeoutMa
 
     private final ConcurrentMap<K, TimeoutMapEntry<K, V>> map = new ConcurrentHashMap<K,
TimeoutMapEntry<K, V>>();
     private final ScheduledExecutorService executor;
+    private volatile ScheduledFuture future;
     private final long purgePollTime;
     private final Lock lock = new ReentrantLock();
     private boolean useLock = true;
@@ -69,7 +73,6 @@ public class DefaultTimeoutMap<K, V> extends ServiceSupport implements
TimeoutMa
         this.executor = executor;
         this.purgePollTime = requestMapPollTimeMillis;
         this.useLock = useLock;
-        schedulePoll();
     }
 
     public V get(K key) {
@@ -248,7 +251,7 @@ public class DefaultTimeoutMap<K, V> extends ServiceSupport implements
TimeoutMa
      * lets schedule each time to allow folks to change the time at runtime
      */
     protected void schedulePoll() {
-        executor.scheduleWithFixedDelay(this, 0, purgePollTime, TimeUnit.MILLISECONDS);
+        future = executor.scheduleWithFixedDelay(this, 0, purgePollTime, TimeUnit.MILLISECONDS);
     }
 
     /**
@@ -276,10 +279,15 @@ public class DefaultTimeoutMap<K, V> extends ServiceSupport implements
TimeoutMa
         if (executor.isShutdown()) {
             throw new IllegalStateException("The ScheduledExecutorService is shutdown");
         }
+        schedulePoll();
     }
 
     @Override
     protected void doStop() throws Exception {
+        if (future != null) {
+            future.cancel(false);
+            future = null;
+        }
         // clear map if we stop
         map.clear();
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/3a5ffd5a/camel-core/src/test/java/org/apache/camel/support/DefaultTimeoutMapTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/support/DefaultTimeoutMapTest.java
b/camel-core/src/test/java/org/apache/camel/support/DefaultTimeoutMapTest.java
index 51e0330..fcf52b8 100644
--- a/camel-core/src/test/java/org/apache/camel/support/DefaultTimeoutMapTest.java
+++ b/camel-core/src/test/java/org/apache/camel/support/DefaultTimeoutMapTest.java
@@ -31,15 +31,19 @@ public class DefaultTimeoutMapTest extends TestCase {
 
     private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);
 
-    public void testDefaultTimeoutMap() {
+    public void testDefaultTimeoutMap() throws Exception {
         DefaultTimeoutMap<?, ?> map = new DefaultTimeoutMap<Object, Object>(executor);
+        map.start();
         assertTrue(map.currentTime() > 0);
 
         assertEquals(0, map.size());
+
+        map.stop();
     }
 
     public void testDefaultTimeoutMapPurge() throws Exception {
         DefaultTimeoutMap<String, Integer> map = new DefaultTimeoutMap<String, Integer>(executor,
100);
+        map.start();
         assertTrue(map.currentTime() > 0);
 
         assertEquals(0, map.size());
@@ -50,10 +54,13 @@ public class DefaultTimeoutMapTest extends TestCase {
         Thread.sleep(250);
 
         assertEquals(0, map.size());
+
+        map.stop();
     }
 
     public void testDefaultTimeoutMapForcePurge() throws Exception {
         DefaultTimeoutMap<String, Integer> map = new DefaultTimeoutMap<String, Integer>(executor,
100);
+        map.start();
         assertTrue(map.currentTime() > 0);
 
         assertEquals(0, map.size());
@@ -71,6 +78,7 @@ public class DefaultTimeoutMapTest extends TestCase {
 
     public void testDefaultTimeoutMapGetRemove() throws Exception {
         DefaultTimeoutMap<String, Integer> map = new DefaultTimeoutMap<String, Integer>(executor,
100);
+        map.start();
         assertTrue(map.currentTime() > 0);
 
         assertEquals(0, map.size());
@@ -84,10 +92,13 @@ public class DefaultTimeoutMapTest extends TestCase {
         assertEquals(123, old);
         assertEquals(null, map.get("A"));
         assertEquals(0, map.size());
+
+        map.stop();
     }
 
     public void testDefaultTimeoutMapGetKeys() throws Exception {
         DefaultTimeoutMap<String, Integer> map = new DefaultTimeoutMap<String, Integer>(executor,
100);
+        map.start();
         assertTrue(map.currentTime() > 0);
 
         assertEquals(0, map.size());
@@ -105,6 +116,7 @@ public class DefaultTimeoutMapTest extends TestCase {
         ScheduledExecutorService e = Executors.newScheduledThreadPool(2);
 
         DefaultTimeoutMap<String, Integer> map = new DefaultTimeoutMap<String, Integer>(e,
50);
+        map.start();
         assertEquals(50, map.getPurgePollTime());
 
         map.put("A", 123, 100);
@@ -116,6 +128,8 @@ public class DefaultTimeoutMapTest extends TestCase {
         assertEquals(0, map.size());
 
         assertSame(e, map.getExecutor());
+
+        map.stop();
     }
 
     public void testExpiredInCorrectOrder() throws Exception {
@@ -130,6 +144,7 @@ public class DefaultTimeoutMapTest extends TestCase {
                 return true;
             }
         };
+        map.start();
         assertEquals(0, map.size());
 
         map.put("A", 1, 50);
@@ -157,6 +172,8 @@ public class DefaultTimeoutMapTest extends TestCase {
         assertEquals(1, values.get(4).intValue());
 
         assertEquals(1, map.size());
+
+        map.stop();
     }
 
     public void testExpiredNotEvicted() throws Exception {
@@ -175,6 +192,7 @@ public class DefaultTimeoutMapTest extends TestCase {
                 return true;
             }
         };
+        map.start();
         assertEquals(0, map.size());
 
         map.put("A", 1, 90);
@@ -197,10 +215,13 @@ public class DefaultTimeoutMapTest extends TestCase {
         // and keep the gold in the map
         assertEquals(1, map.size());
         assertEquals(Integer.valueOf(9), map.get("gold"));
+
+        map.stop();
     }
 
     public void testDefaultTimeoutMapStopStart() throws Exception {
         DefaultTimeoutMap<String, Integer> map = new DefaultTimeoutMap<String, Integer>(executor,
100);
+        map.start();
         map.put("A", 1, 500);
 
         assertEquals(1, map.size());
@@ -220,6 +241,8 @@ public class DefaultTimeoutMapTest extends TestCase {
         Thread.sleep(250);
         // now it should be gone
         assertEquals(0, map.size());
+
+        map.stop();
     }
 
 }


Mime
View raw message