Repository: camel Updated Branches: refs/heads/camel-2.13.x 4ba6d4164 -> 1e7118362 refs/heads/camel-2.14.x ad73ccec9 -> 97f92e59e
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/97f92e59 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/97f92e59 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/97f92e59 Branch: refs/heads/camel-2.14.x Commit: 97f92e59e95544db4ea313c73be9b75a40851f30 Parents: ad73cce Author: Willem Jiang <willem.ji...@gmail.com> Authored: Fri Jan 9 16:30:19 2015 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Fri Jan 9 16:35:09 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/97f92e59/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/97f92e59/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/97f92e59/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;