Skip to content

Commit

Permalink
CAMEL-8204 Polish the code as Claus suggested.
Browse files Browse the repository at this point in the history
  • Loading branch information
WillemJiang committed Jan 5, 2015
1 parent ad1bce1 commit e1f7fde
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
4 changes: 3 additions & 1 deletion camel-core/src/main/java/org/apache/camel/TimeoutMap.java
Expand Up @@ -52,8 +52,10 @@ public interface TimeoutMap<K, V> extends Runnable {
* @param key the key
* @param value the value
* @param timeoutMillis timeout in millis
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
*/
void put(K key, V value, long timeoutMillis);
V put(K key, V value, long timeoutMillis);

/**
* Callback when the value has been evicted
Expand Down
Expand Up @@ -94,14 +94,15 @@ public V get(K key) {
return entry.getValue();
}

public void put(K key, V value, long timeoutMillis) {
public V put(K key, V value, long timeoutMillis) {
TimeoutMapEntry<K, V> entry = new TimeoutMapEntry<K, V>(key, value, timeoutMillis);
if (useLock) {
lock.lock();
}
try {
map.put(key, entry);
updateExpireTime(entry);
TimeoutMapEntry<K, V> result = map.put(key, entry);
return result != null ? result.getValue() : null;
} finally {
if (useLock) {
lock.unlock();
Expand Down
Expand Up @@ -70,7 +70,7 @@ public ReplyHandler get(String key) {
}

@Override
public void put(String key, ReplyHandler value, long timeoutMillis) {
public ReplyHandler put(String key, ReplyHandler value, long timeoutMillis) {
try {
if (listener != null) {
listener.onPut(key);
Expand All @@ -79,13 +79,15 @@ public void put(String key, ReplyHandler value, long timeoutMillis) {
// ignore
}

ReplyHandler result;
if (timeoutMillis <= 0) {
// no timeout (must use Integer.MAX_VALUE)
super.put(key, value, Integer.MAX_VALUE);
result = super.put(key, value, Integer.MAX_VALUE);
} else {
super.put(key, value, timeoutMillis);
result = super.put(key, value, timeoutMillis);
}
log.trace("Added correlationID: {} to timeout after: {} millis", key, timeoutMillis);
return result;
}

@Override
Expand Down
Expand Up @@ -51,10 +51,10 @@ public String registerReply(ReplyManager replyManager, Exchange exchange, AsyncC
// add to correlation map
QueueReplyHandler handler = new QueueReplyHandler(replyManager, exchange, callback,
originalCorrelationId, correlationId, requestTimeout);
if (correlation.get(correlationId) != null) {
log.error("The correlationId [{}] is not unique, some reply message would be ignored and the request thread could be blocked.", correlationId);
ReplyHandler result = correlation.put(correlationId, handler, requestTimeout);
if (result != null) {
log.warn("The correlationId [{}] is not unique, some reply message would be ignored and the request thread could be blocked.", correlationId);
}
correlation.put(correlationId, handler, requestTimeout);
return correlationId;
}

Expand Down
Expand Up @@ -61,10 +61,10 @@ public String registerReply(ReplyManager replyManager, Exchange exchange, AsyncC
String originalCorrelationId, String correlationId, long requestTimeout) {
// add to correlation map
TemporaryQueueReplyHandler handler = new TemporaryQueueReplyHandler(this, exchange, callback, originalCorrelationId, correlationId, requestTimeout);
if (correlation.get(correlationId) != null) {
ReplyHandler result = correlation.put(correlationId, handler, requestTimeout);
if (result != null) {
log.error("The correlationId [{}] is not unique, some reply message would be ignored and the request thread could be blocked.", correlationId);
}
correlation.put(correlationId, handler, requestTimeout);
return correlationId;
}

Expand Down

0 comments on commit e1f7fde

Please sign in to comment.