You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2015/01/09 09:34:37 UTC
camel git commit: CAMEL-8204 Added the putIfAbsent method to avoid
overriding the old key value as Claus suggested
Repository: camel
Updated Branches:
refs/heads/master 4a01d60cc -> 761a878e5
CAMEL-8204 Added the putIfAbsent method to avoid overriding the old key value as Claus suggested
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/761a878e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/761a878e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/761a878e
Branch: refs/heads/master
Commit: 761a878e557b7a42c4338492caca33877cc69724
Parents: 4a01d60
Author: Willem Jiang <wi...@gmail.com>
Authored: Fri Jan 9 16:30:19 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Fri Jan 9 16:33:36 2015 +0800
----------------------------------------------------------------------
.../main/java/org/apache/camel/TimeoutMap.java | 12 ++++++++++++
.../apache/camel/support/DefaultTimeoutMap.java | 19 ++++++++++++++++++-
.../component/jms/reply/ReplyManagerSupport.java | 4 ++--
3 files changed, 32 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/761a878e/camel-core/src/main/java/org/apache/camel/TimeoutMap.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/TimeoutMap.java b/camel-core/src/main/java/org/apache/camel/TimeoutMap.java
index ef5bda3..366f5c7 100644
--- a/camel-core/src/main/java/org/apache/camel/TimeoutMap.java
+++ b/camel-core/src/main/java/org/apache/camel/TimeoutMap.java
@@ -56,6 +56,18 @@ public interface TimeoutMap<K, V> extends Runnable {
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
*/
V put(K key, V value, long timeoutMillis);
+
+ /**
+ * Adds the key value pair into the map if the specified key is not already associated with a value
+ * such that some time after the given timeout the entry will be evicted
+ *
+ * @param key the key
+ * @param value the value
+ * @param timeoutMillis timeout in millis
+ * @return the value associated with <tt>key</tt>, or
+ * <tt>null</tt> if there was no mapping for <tt>key</tt>.
+ */
+ V putIfAbsent(K key, V value, long timeoutMillis);
/**
* Callback when the value has been evicted
http://git-wip-us.apache.org/repos/asf/camel/blob/761a878e/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 57462f1..29afe34 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
@@ -93,7 +93,7 @@ public class DefaultTimeoutMap<K, V> extends ServiceSupport implements TimeoutMa
}
return entry.getValue();
}
-
+
public V put(K key, V value, long timeoutMillis) {
TimeoutMapEntry<K, V> entry = new TimeoutMapEntry<K, V>(key, value, timeoutMillis);
if (useLock) {
@@ -109,6 +109,23 @@ public class DefaultTimeoutMap<K, V> extends ServiceSupport implements TimeoutMa
}
}
}
+
+ public V putIfAbsent(K key, V value, long timeoutMillis) {
+ TimeoutMapEntry<K, V> entry = new TimeoutMapEntry<K, V>(key, value, timeoutMillis);
+ if (useLock) {
+ lock.lock();
+ }
+ try {
+ updateExpireTime(entry);
+ //Just make sure we don't override the old entry
+ TimeoutMapEntry<K, V> result = map.putIfAbsent(key, entry);
+ return result != null ? result.getValue() : null;
+ } finally {
+ if (useLock) {
+ lock.unlock();
+ }
+ }
+ }
public V remove(K key) {
TimeoutMapEntry<K, V> entry;
http://git-wip-us.apache.org/repos/asf/camel/blob/761a878e/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/ReplyManagerSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/ReplyManagerSupport.java b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/ReplyManagerSupport.java
index ff2f344..30e5919 100644
--- a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/ReplyManagerSupport.java
+++ b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/ReplyManagerSupport.java
@@ -101,10 +101,10 @@ public abstract class ReplyManagerSupport extends ServiceSupport implements Repl
// add to correlation map
QueueReplyHandler handler = new QueueReplyHandler(replyManager, exchange, callback,
originalCorrelationId, correlationId, requestTimeout);
- ReplyHandler result = correlation.put(correlationId, handler, requestTimeout);
+ // Just make sure we don't override the old value of the correlationId
+ ReplyHandler result = correlation.putIfAbsent(correlationId, handler, requestTimeout);
if (result != null) {
String logMessage = String.format("The correlationId [%s] is not unique.", correlationId);
- log.warn("{}, some reply message would be ignored and the request thread could be blocked.", logMessage);
throw new IllegalArgumentException(logMessage);
}
return correlationId;