diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java index fa111ebe55..8bab051a0e 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java @@ -439,6 +439,9 @@ public class QueueImpl implements Queue { @Override public void route(final Message message, final RoutingContext context) throws Exception { + if (purgeOnNoConsumers && getConsumerCount() == 0) { + return; + } context.addQueue(address, this); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/addressing/AddressingTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/addressing/AddressingTest.java index 34dd7d7239..0610829ae5 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/addressing/AddressingTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/addressing/AddressingTest.java @@ -227,13 +227,28 @@ public class AddressingTest extends ActiveMQTestBase { SimpleString address = new SimpleString("test.address"); SimpleString queueName = SimpleString.toSimpleString(UUID.randomUUID().toString()); server.createQueue(address, RoutingType.ANYCAST, queueName, null, null, true, false, false, false, false, 1, true, true); - assertNotNull(server.locateQueue(queueName)); + Queue queue = server.locateQueue(queueName); + assertNotNull(queue); ClientSession session = sessionFactory.createSession(); ClientProducer producer = session.createProducer(address); + + // there are no consumers so no messages should be routed to the queue producer.send(session.createMessage(true)); - session.createConsumer(queueName).close(); + assertEquals(0, queue.getMessageCount()); + + ClientConsumer consumer = session.createConsumer(queueName); + // there is a consumer now so the message should be routed + producer.send(session.createMessage(true)); + assertEquals(1, queue.getMessageCount()); + + consumer.close(); + // the last consumer was closed so the queue should exist but be purged assertNotNull(server.locateQueue(queueName)); - assertEquals(0, server.locateQueue(queueName).getMessageCount()); + assertEquals(0, queue.getMessageCount()); + + // there are no consumers so no messages should be routed to the queue + producer.send(session.createMessage(true)); + assertEquals(0, queue.getMessageCount()); } @Test