diff options
author | Robert Gemmell <robbie@apache.org> | 2012-02-13 00:44:01 +0000 |
---|---|---|
committer | Robert Gemmell <robbie@apache.org> | 2012-02-13 00:44:01 +0000 |
commit | 946f459455ca90c8a0d737b6a31094767f43fd03 (patch) | |
tree | c1e8ecfe2f62fc2694e95b9ac03aef7448bb604c /java/client/src | |
parent | d15fc6f29484cbd05e95b584beeb6e1a59da1367 (diff) | |
download | qpid-python-946f459455ca90c8a0d737b6a31094767f43fd03.tar.gz |
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
Diffstat (limited to 'java/client/src')
-rw-r--r-- | java/client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java | 36 | ||||
-rw-r--r-- | java/client/src/test/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10Test.java | 63 |
2 files changed, 85 insertions, 14 deletions
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<ReplyTo, SoftReference<Destination>> _destinationCache = Collections.synchronizedMap(new HashMap<ReplyTo, SoftReference<Destination>>()); + + 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<ReplyTo, Destination> _destinationCache = + Collections.synchronizedMap(new LinkedHashMap<ReplyTo,Destination>(DESTINATION_CACHE_CAPACITY, + DESTINATION_CACHE_LOAD_FACTOR, + true) + { + @Override + protected boolean removeEldestEntry(Map.Entry<ReplyTo, Destination> 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<Destination> 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<Destination>(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>(destination)); + _destinationCache.put(replyTo, destination); _messageProps.setReplyTo(replyTo); - } public Destination getJMSDestination() throws JMSException diff --git a/java/client/src/test/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10Test.java b/java/client/src/test/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10Test.java new file mode 100644 index 0000000000..4636851c79 --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10Test.java @@ -0,0 +1,63 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.client.message; + +import javax.jms.Destination; + +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.transport.DeliveryProperties; +import org.apache.qpid.transport.MessageProperties; +import org.apache.qpid.transport.ReplyTo; + +public class AMQMessageDelegate_0_10Test extends QpidTestCase +{ + /** + * Tests that when two messages arrive with the same ReplyTo exchange and routingKey values, + * the cache returns the same Destination object from getJMSReplyTo instead of a new one. + */ + public void testDestinationCache() throws Exception + { + //create a message delegate and retrieve the replyTo Destination + AMQMessageDelegate_0_10 delegate1 = generateMessageDelegateWithReplyTo(); + Destination dest1 = delegate1.getJMSReplyTo(); + + //create a new message delegate with the same details, and retrieve the replyTo Destination + AMQMessageDelegate_0_10 delegate2 = generateMessageDelegateWithReplyTo(); + Destination dest2 = delegate2.getJMSReplyTo(); + + //verify that the destination cache means these are the same Destination object + assertSame("Should have received the same Destination objects", dest1, dest2); + } + + private AMQMessageDelegate_0_10 generateMessageDelegateWithReplyTo() + { + MessageProperties mesProps = new MessageProperties(); + ReplyTo reply = new ReplyTo("amq.direct", "myReplyQueue"); + mesProps.setReplyTo(reply); + + DeliveryProperties delProps = new DeliveryProperties(); + delProps.setExchange("amq.direct"); + delProps.setRoutingKey("myRequestQueue"); + + AMQMessageDelegate_0_10 delegate = new AMQMessageDelegate_0_10(mesProps,delProps,1L); + return delegate; + } +} |