diff --git a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireConnection.java b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireConnection.java index 8dc0b348c7..a984a1c140 100644 --- a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireConnection.java +++ b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireConnection.java @@ -65,6 +65,7 @@ import org.apache.activemq.artemis.core.server.Queue; import org.apache.activemq.artemis.core.server.ServerConsumer; import org.apache.activemq.artemis.core.server.ServerSession; import org.apache.activemq.artemis.core.server.SlowConsumerDetectionListener; +import org.apache.activemq.artemis.core.server.TempQueueObserver; import org.apache.activemq.artemis.core.server.impl.RefsOperation; import org.apache.activemq.artemis.core.transaction.Transaction; import org.apache.activemq.artemis.core.transaction.TransactionOperationAbstract; @@ -76,6 +77,7 @@ import org.apache.activemq.artemis.spi.core.remoting.Connection; import org.apache.activemq.artemis.utils.UUIDGenerator; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQMessage; +import org.apache.activemq.command.ActiveMQTempQueue; import org.apache.activemq.command.ActiveMQTopic; import org.apache.activemq.command.BrokerInfo; import org.apache.activemq.command.BrokerSubscriptionInfo; @@ -125,7 +127,7 @@ import org.jboss.logging.Logger; /** * Represents an activemq connection. */ -public class OpenWireConnection extends AbstractRemotingConnection implements SecurityAuth { +public class OpenWireConnection extends AbstractRemotingConnection implements SecurityAuth, TempQueueObserver { private static final Logger logger = Logger.getLogger(OpenWireConnection.class); @@ -789,6 +791,24 @@ public class OpenWireConnection extends AbstractRemotingConnection implements Se checkInactivity(); } + @Override + public void tempQueueDeleted(SimpleString bindingName) { + String amqName = OpenWireUtil.toAMQAddress(bindingName.toString()); + ActiveMQDestination dest = new ActiveMQTempQueue(amqName); + + if (!AdvisorySupport.isAdvisoryTopic(dest)) { + AMQConnectionContext context = getContext(); + DestinationInfo advInfo = new DestinationInfo(context.getConnectionId(), DestinationInfo.REMOVE_OPERATION_TYPE, dest); + + ActiveMQTopic topic = AdvisorySupport.getDestinationAdvisoryTopic(dest); + try { + protocolManager.fireAdvisory(context, topic, advInfo); + } catch (Exception e) { + logger.warn("Failed to fire advisory on " + topic, e); + } + } + } + class SlowConsumerDetection implements SlowConsumerDetectionListener { @Override diff --git a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/util/OpenWireUtil.java b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/util/OpenWireUtil.java index 05e1e34400..0ad6043cd1 100644 --- a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/util/OpenWireUtil.java +++ b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/util/OpenWireUtil.java @@ -58,6 +58,10 @@ public class OpenWireUtil { } } + public static String toAMQAddress(String coreAddress) { + return coreAddress.replace(JMS_QUEUE_ADDRESS_PREFIX, "").replace(JMS_TEMP_QUEUE_ADDRESS_PREFIX, "").replace(JMS_TOPIC_ADDRESS_PREFIX, "").replace(JMS_TEMP_TOPIC_ADDRESS_PREFIX, ""); + } + /** * We convert the core address to an ActiveMQ Destination. We use the actual address on the message rather than the * destination set on the consumer because it maybe different and the JMS spec says that it should be what ever was @@ -66,7 +70,7 @@ public class OpenWireUtil { */ public static ActiveMQDestination toAMQAddress(ServerMessage message, ActiveMQDestination actualDestination) { String address = message.getAddress().toString(); - String strippedAddress = address.replace(JMS_QUEUE_ADDRESS_PREFIX, "").replace(JMS_TEMP_QUEUE_ADDRESS_PREFIX, "").replace(JMS_TOPIC_ADDRESS_PREFIX, "").replace(JMS_TEMP_TOPIC_ADDRESS_PREFIX, ""); + String strippedAddress = toAMQAddress(address); if (actualDestination.isQueue()) { return new ActiveMQQueue(strippedAddress); } else { diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/TempQueueObserver.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/TempQueueObserver.java new file mode 100644 index 0000000000..91cc36d32b --- /dev/null +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/TempQueueObserver.java @@ -0,0 +1,24 @@ +/* + * 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.activemq.artemis.core.server; + +import org.apache.activemq.artemis.api.core.SimpleString; + +public interface TempQueueObserver { + + void tempQueueDeleted(SimpleString bindingName); +} diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java index 37d99bb657..627b20190e 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java @@ -75,6 +75,7 @@ import org.apache.activemq.artemis.core.server.RoutingContext; import org.apache.activemq.artemis.core.server.ServerConsumer; import org.apache.activemq.artemis.core.server.ServerMessage; import org.apache.activemq.artemis.core.server.ServerSession; +import org.apache.activemq.artemis.core.server.TempQueueObserver; import org.apache.activemq.artemis.core.server.management.ManagementService; import org.apache.activemq.artemis.core.server.management.Notification; import org.apache.activemq.artemis.core.transaction.ResourceManager; @@ -526,6 +527,9 @@ public class ServerSessionImpl implements ServerSession, FailureListener { // It is up to the user to delete the queue when finished with it TempQueueCleanerUpper cleaner = new TempQueueCleanerUpper(server, name); + if (remotingConnection instanceof TempQueueObserver) { + cleaner.setObserver((TempQueueObserver) remotingConnection); + } remotingConnection.addCloseListener(cleaner); remotingConnection.addFailureListener(cleaner); @@ -566,12 +570,18 @@ public class ServerSessionImpl implements ServerSession, FailureListener { private final ActiveMQServer server; + private TempQueueObserver observer; + public TempQueueCleanerUpper(final ActiveMQServer server, final SimpleString bindingName) { this.server = server; this.bindingName = bindingName; } + public void setObserver(TempQueueObserver observer) { + this.observer = observer; + } + private void run() { try { if (logger.isDebugEnabled()) { @@ -579,6 +589,9 @@ public class ServerSessionImpl implements ServerSession, FailureListener { } try { server.destroyQueue(bindingName, null, false); + if (observer != null) { + observer.tempQueueDeleted(bindingName); + } } catch (ActiveMQException e) { // that's fine.. it can happen due to queue already been deleted logger.debug(e.getMessage(), e); diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/openwire/SimpleOpenWireTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/openwire/SimpleOpenWireTest.java index 81f0f1bdd1..9798fa6507 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/openwire/SimpleOpenWireTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/openwire/SimpleOpenWireTest.java @@ -17,7 +17,9 @@ package org.apache.activemq.artemis.tests.integration.openwire; import javax.jms.Connection; +import javax.jms.DeliveryMode; import javax.jms.Destination; +import javax.jms.InvalidDestinationException; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; @@ -1146,6 +1148,52 @@ public class SimpleOpenWireTest extends BasicOpenWireTest { } + @Test + public void testTempQueueSendAfterConnectionClose() throws Exception { + + Connection connection1 = null; + Connection connection2 = null; + + try { + connection1 = factory.createConnection(); + connection2 = factory.createConnection(); + connection1.start(); + connection2.start(); + + Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE); + Queue tempQueue = session1.createTemporaryQueue(); + + Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = session2.createProducer(tempQueue); + producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + TextMessage m = session2.createTextMessage("Hello temp queue"); + producer.send(m); + + MessageConsumer consumer = session1.createConsumer(tempQueue); + TextMessage received = (TextMessage) consumer.receive(5000); + assertNotNull(received); + assertEquals("Hello temp queue", received.getText()); + + //close first connection, let temp queue die + connection1.close(); + + //send again + try { + producer.send(m); + fail("Send should fail since temp destination should not exist anymore."); + } catch (InvalidDestinationException e) { + //ignore + } + } finally { + if (connection1 != null) { + connection1.close(); + } + if (connection2 != null) { + connection2.close(); + } + } + } + private void checkQueueEmpty(String qName) { PostOffice po = server.getPostOffice(); LocalQueueBinding binding = (LocalQueueBinding) po.getBinding(SimpleString.toSimpleString("jms.queue." + qName));