From 946f459455ca90c8a0d737b6a31094767f43fd03 Mon Sep 17 00:00:00 2001 From: Robert Gemmell Date: Mon, 13 Feb 2012 00:44:01 +0000 Subject: QPID-3440: add test to verify issue by ensuring that the reply-to Destination cache results in use of the same Destination objects for subsequent messages. Implement equals() and hashCode() for the generated ReplyTo class to let it function properly as an index in the hashmap to resolve the issue. Update PropertyValueTest to remember the initial reply-to destination it sets and compare it with the value returned after sending/receiving the message, rather than constituting its own test destination for comparison. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1243380 13f79535-47bb-0310-9956-ffa450edef68 --- .../client/message/AMQMessageDelegate_0_10.java | 36 +++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'java/client/src/main') diff --git a/java/client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java b/java/client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java index 0973815231..cd69289492 100644 --- a/java/client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java +++ b/java/client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java @@ -46,12 +46,12 @@ import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageFormatException; import javax.jms.MessageNotWriteableException; -import java.lang.ref.SoftReference; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -65,7 +65,22 @@ import java.util.UUID; public class AMQMessageDelegate_0_10 extends AbstractAMQMessageDelegate { private static final Logger _logger = LoggerFactory.getLogger(AMQMessageDelegate_0_10.class); - private static final Map> _destinationCache = Collections.synchronizedMap(new HashMap>()); + + private static final float DESTINATION_CACHE_LOAD_FACTOR = 0.75f; + private static final int DESTINATION_CACHE_SIZE = 500; + private static final int DESTINATION_CACHE_CAPACITY = (int) (DESTINATION_CACHE_SIZE / DESTINATION_CACHE_LOAD_FACTOR); + + private static final Map _destinationCache = + Collections.synchronizedMap(new LinkedHashMap(DESTINATION_CACHE_CAPACITY, + DESTINATION_CACHE_LOAD_FACTOR, + true) + { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) + { + return size() >= DESTINATION_CACHE_SIZE; + } + }); public static final String JMS_TYPE = "x-jms-type"; @@ -241,12 +256,8 @@ public class AMQMessageDelegate_0_10 extends AbstractAMQMessageDelegate } else { - Destination dest = null; - SoftReference ref = _destinationCache.get(replyTo); - if (ref != null) - { - dest = ref.get(); - } + Destination dest = _destinationCache.get(replyTo); + if (dest == null) { String exchange = replyTo.getExchange(); @@ -254,14 +265,13 @@ public class AMQMessageDelegate_0_10 extends AbstractAMQMessageDelegate if (AMQDestination.getDefaultDestSyntax() == AMQDestination.DestSyntax.BURL) { - dest = generateDestination(new AMQShortString(exchange), new AMQShortString(routingKey)); } else { dest = convertToAddressBasedDestination(exchange,routingKey,null); } - _destinationCache.put(replyTo, new SoftReference(dest)); + _destinationCache.put(replyTo, dest); } return dest; @@ -355,13 +365,11 @@ public class AMQMessageDelegate_0_10 extends AbstractAMQMessageDelegate jmse.setLinkedException(e); throw jmse; } - } - + final ReplyTo replyTo = new ReplyTo(amqd.getExchangeName().toString(), amqd.getRoutingKey().toString()); - _destinationCache.put(replyTo, new SoftReference(destination)); + _destinationCache.put(replyTo, destination); _messageProps.setReplyTo(replyTo); - } public Destination getJMSDestination() throws JMSException -- cgit v1.2.1