camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject camel git commit: CAMEL-8733 Camel-Hazelcast: Add the possibility to specify a ttl related to a specific key
Date Sun, 03 May 2015 08:36:14 GMT
Repository: camel
Updated Branches:
  refs/heads/master 8af7e6517 -> 1c20c435e


CAMEL-8733 Camel-Hazelcast: Add the possibility to specify a ttl related to a specific key


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

Branch: refs/heads/master
Commit: 1c20c435e84a43b24ab69138165be6c06128b1c6
Parents: 8af7e65
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sun May 3 10:34:19 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sun May 3 10:34:19 2015 +0200

----------------------------------------------------------------------
 .../component/hazelcast/HazelcastConstants.java |  4 +++-
 .../hazelcast/map/HazelcastMapProducer.java     | 25 +++++++++++++++++++-
 .../hazelcast/HazelcastMapProducerTest.java     | 11 +++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1c20c435/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
index dcdcf6f..ecc63e3 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
@@ -36,6 +36,8 @@ public final class HazelcastConstants {
     public static final String OBJECT_ID = "CamelHazelcastObjectId";
     public static final String OBJECT_POS = "CamelHazelcastObjectIndex";
     public static final String OBJECT_VALUE = "CamelHazelcastObjectValue";
+    public static final String TTL_VALUE = "CamelHazelcastObjectTtlValue";
+    public static final String TTL_UNIT = "CamelHazelcastObjectTtlUnit";
     public static final String QUERY = "CamelHazelcastQuery";
 
     /*
@@ -49,7 +51,7 @@ public final class HazelcastConstants {
     public static final String CACHE_NAME = "CamelHazelcastCacheName";
     public static final String CACHE_TYPE = "CamelHazelcastCacheType";
 
-    // actions (put, delete, get, getAll, replace, update, clear)
+    // actions (put, delete, get, getAll, update, clear)
     public static final String OPERATION = "CamelHazelcastOperationType";
     public static final int PUT_OPERATION = 1;
     public static final int DELETE_OPERATION = 2;

http://git-wip-us.apache.org/repos/asf/camel/blob/1c20c435/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
index dfc917c..6d0921d 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.hazelcast.map;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import com.hazelcast.core.HazelcastInstance;
 import com.hazelcast.core.IMap;
@@ -46,6 +47,8 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
         // get header parameters
         Object oid = null;
         Object ovalue = null;
+        Object ttl = null;
+        Object ttlUnit = null;
         String query = null;
 
         if (headers.containsKey(HazelcastConstants.OBJECT_ID)) {
@@ -56,6 +59,14 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
             ovalue = headers.get(HazelcastConstants.OBJECT_VALUE);
         }
 
+        if (headers.containsKey(HazelcastConstants.TTL_VALUE)) {
+            ttl = headers.get(HazelcastConstants.TTL_VALUE);
+        }
+
+        if (headers.containsKey(HazelcastConstants.TTL_UNIT)) {
+        	ttlUnit = headers.get(HazelcastConstants.TTL_UNIT);
+        }
+        
         if (headers.containsKey(HazelcastConstants.QUERY)) {
             query = (String) headers.get(HazelcastConstants.QUERY);
         }
@@ -64,7 +75,11 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
         switch (operation) {
 
         case HazelcastConstants.PUT_OPERATION:
-            this.put(oid, exchange);
+            if (ObjectHelper.isEmpty(ttl) && ObjectHelper.isEmpty(ttlUnit)) {
+                this.put(oid, exchange);
+            } else {
+                this.put(oid, ttl, ttlUnit, exchange);            	
+            }
             break;
 
         case HazelcastConstants.GET_OPERATION:
@@ -168,6 +183,14 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
     }
     
     /**
+     * put a new object into the cache with a specific time to live
+     */
+    private void put(Object oid, Object ttl, Object ttlUnit, Exchange exchange) {
+        Object body = exchange.getIn().getBody();
+        this.cache.put(oid, body, (long) ttl, (TimeUnit) ttlUnit);
+    }
+    
+    /**
      * Clear all the entries
      */
     private void clear(Exchange exchange) {

http://git-wip-us.apache.org/repos/asf/camel/blob/1c20c435/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
index e820420..2879843 100644
--- a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
+++ b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
@@ -23,6 +23,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import com.hazelcast.core.HazelcastInstance;
 import com.hazelcast.core.IMap;
@@ -82,6 +83,16 @@ public class HazelcastMapProducerTest extends HazelcastCamelTestSupport
implemen
     }
 
     @Test
+    public void testPutWithTTL() throws InterruptedException {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(HazelcastConstants.OBJECT_ID, "4711");
+        headers.put(HazelcastConstants.TTL_VALUE, new Long(1));
+        headers.put(HazelcastConstants.TTL_UNIT, TimeUnit.MINUTES);
+        template.sendBodyAndHeaders("direct:put", "test", headers);
+        verify(map).put("4711", "test", 1, TimeUnit.MINUTES);
+    }
+    
+    @Test
     public void testUpdate() {
         template.sendBodyAndHeader("direct:update", "my-fooo", HazelcastConstants.OBJECT_ID,
"4711");
         verify(map).lock("4711");


Mime
View raw message