From 33b88d34a9c242787c5878c9192f77bbb163e446 Mon Sep 17 00:00:00 2001 From: gtully Date: Thu, 6 Mar 2014 22:39:16 +0000 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-5087 fix and test --- .../org/apache/activemq/RedeliveryPolicy.java | 8 ++--- .../apache/activemq/RedeliveryPolicyTest.java | 33 +++++++++++++++---- .../activemq/broker/BrokerRedeliveryTest.java | 5 +-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/RedeliveryPolicy.java b/activemq-client/src/main/java/org/apache/activemq/RedeliveryPolicy.java index c6824f7aef..91f2b71923 100644 --- a/activemq-client/src/main/java/org/apache/activemq/RedeliveryPolicy.java +++ b/activemq-client/src/main/java/org/apache/activemq/RedeliveryPolicy.java @@ -98,18 +98,14 @@ public class RedeliveryPolicy extends DestinationMapEntry implements Cloneable, } public long getNextRedeliveryDelay(long previousDelay) { - long nextDelay; + long nextDelay = redeliveryDelay; - if (previousDelay == 0) { - nextDelay = redeliveryDelay; - } else if (useExponentialBackOff && backOffMultiplier > 1) { + if (previousDelay > 0 && useExponentialBackOff && backOffMultiplier > 1) { nextDelay = (long) (previousDelay * backOffMultiplier); if(maximumRedeliveryDelay != -1 && nextDelay > maximumRedeliveryDelay) { // in case the user made max redelivery delay less than redelivery delay for some reason. nextDelay = Math.max(maximumRedeliveryDelay, redeliveryDelay); } - } else { - nextDelay = previousDelay; } if (useCollisionAvoidance) { diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/RedeliveryPolicyTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/RedeliveryPolicyTest.java index f77e4baf18..02db378c7e 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/RedeliveryPolicyTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/RedeliveryPolicyTest.java @@ -43,6 +43,27 @@ public class RedeliveryPolicyTest extends JmsTestSupport { junit.textui.TestRunner.run(suite()); } + + public void testGetNext() throws Exception { + + RedeliveryPolicy policy = new RedeliveryPolicy(); + policy.setInitialRedeliveryDelay(0); + policy.setRedeliveryDelay(500); + policy.setBackOffMultiplier((short) 2); + policy.setUseExponentialBackOff(true); + + long delay = policy.getNextRedeliveryDelay(0); + assertEquals(500, delay); + delay = policy.getNextRedeliveryDelay(delay); + assertEquals(500*2, delay); + delay = policy.getNextRedeliveryDelay(delay); + assertEquals(500*4, delay); + + policy.setUseExponentialBackOff(false); + delay = policy.getNextRedeliveryDelay(delay); + assertEquals(500, delay); + } + /** * @throws Exception */ @@ -185,7 +206,7 @@ public class RedeliveryPolicyTest extends JmsTestSupport { assertEquals("1st", m.getText()); session.rollback(); - m = (TextMessage)consumer.receive(1000); + m = (TextMessage)consumer.receive(2000); assertNotNull(m); assertEquals("1st", m.getText()); session.rollback(); @@ -245,27 +266,27 @@ public class RedeliveryPolicyTest extends JmsTestSupport { assertEquals("1st", m.getText()); session.rollback(); - m = (TextMessage)consumer.receive(1000); + m = (TextMessage)consumer.receive(2000); assertNotNull(m); assertEquals("1st", m.getText()); session.rollback(); - m = (TextMessage)consumer.receive(1000); + m = (TextMessage)consumer.receive(2000); assertNotNull(m); assertEquals("1st", m.getText()); session.rollback(); - m = (TextMessage)consumer.receive(1000); + m = (TextMessage)consumer.receive(2000); assertNotNull(m); assertEquals("1st", m.getText()); session.rollback(); - m = (TextMessage)consumer.receive(1000); + m = (TextMessage)consumer.receive(2000); assertNotNull(m); assertEquals("1st", m.getText()); session.commit(); - m = (TextMessage)consumer.receive(1000); + m = (TextMessage)consumer.receive(2000); assertNotNull(m); assertEquals("2nd", m.getText()); session.commit(); diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/BrokerRedeliveryTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/BrokerRedeliveryTest.java index c61a5eefb0..1bec47940c 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/BrokerRedeliveryTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/BrokerRedeliveryTest.java @@ -40,6 +40,7 @@ public class BrokerRedeliveryTest extends org.apache.activemq.TestSupport { final ActiveMQQueue destination = new ActiveMQQueue("Redelivery"); final String data = "hi"; final long redeliveryDelayMillis = 2000; + final long initialRedeliveryDelayMillis = 4000; int maxBrokerRedeliveries = 2; public void testScheduledRedelivery() throws Exception { @@ -79,7 +80,7 @@ public class BrokerRedeliveryTest extends org.apache.activemq.TestSupport { LOG.info("got: " + brokerRedeliveryMessage); assertNotNull("got message via broker redelivery after delay", brokerRedeliveryMessage); assertEquals("message matches", message.getStringProperty("data"), brokerRedeliveryMessage.getStringProperty("data")); - assertEquals("has expiryDelay specified", redeliveryDelayMillis, brokerRedeliveryMessage.getLongProperty(RedeliveryPlugin.REDELIVERY_DELAY)); + assertEquals("has expiryDelay specified", i == 0 ? initialRedeliveryDelayMillis : redeliveryDelayMillis, brokerRedeliveryMessage.getLongProperty(RedeliveryPlugin.REDELIVERY_DELAY)); consumerSession.rollback(); } @@ -149,7 +150,7 @@ public class BrokerRedeliveryTest extends org.apache.activemq.TestSupport { RedeliveryPolicy brokerRedeliveryPolicy = new RedeliveryPolicy(); brokerRedeliveryPolicy.setRedeliveryDelay(redeliveryDelayMillis); - brokerRedeliveryPolicy.setInitialRedeliveryDelay(redeliveryDelayMillis); + brokerRedeliveryPolicy.setInitialRedeliveryDelay(initialRedeliveryDelayMillis); brokerRedeliveryPolicy.setMaximumRedeliveries(maxBrokerRedeliveries); RedeliveryPolicyMap redeliveryPolicyMap = new RedeliveryPolicyMap();