ARTEMIS-864 Sending to a destroyed temp queue didn't get exception

This commit is contained in:
Howard Gao 2016-11-22 15:47:37 +08:00
parent 442b8ef659
commit 1487fe6bdd
5 changed files with 111 additions and 2 deletions

View File

@ -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.ServerConsumer;
import org.apache.activemq.artemis.core.server.ServerSession; import org.apache.activemq.artemis.core.server.ServerSession;
import org.apache.activemq.artemis.core.server.SlowConsumerDetectionListener; 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.server.impl.RefsOperation;
import org.apache.activemq.artemis.core.transaction.Transaction; import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.core.transaction.TransactionOperationAbstract; 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.artemis.utils.UUIDGenerator;
import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQMessage; import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQTempQueue;
import org.apache.activemq.command.ActiveMQTopic; import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.command.BrokerInfo; import org.apache.activemq.command.BrokerInfo;
import org.apache.activemq.command.BrokerSubscriptionInfo; import org.apache.activemq.command.BrokerSubscriptionInfo;
@ -125,7 +127,7 @@ import org.jboss.logging.Logger;
/** /**
* Represents an activemq connection. * 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); private static final Logger logger = Logger.getLogger(OpenWireConnection.class);
@ -789,6 +791,24 @@ public class OpenWireConnection extends AbstractRemotingConnection implements Se
checkInactivity(); 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 { class SlowConsumerDetection implements SlowConsumerDetectionListener {
@Override @Override

View File

@ -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 * 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 * 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) { public static ActiveMQDestination toAMQAddress(ServerMessage message, ActiveMQDestination actualDestination) {
String address = message.getAddress().toString(); 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()) { if (actualDestination.isQueue()) {
return new ActiveMQQueue(strippedAddress); return new ActiveMQQueue(strippedAddress);
} else { } else {

View File

@ -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);
}

View File

@ -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.ServerConsumer;
import org.apache.activemq.artemis.core.server.ServerMessage; import org.apache.activemq.artemis.core.server.ServerMessage;
import org.apache.activemq.artemis.core.server.ServerSession; 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.ManagementService;
import org.apache.activemq.artemis.core.server.management.Notification; import org.apache.activemq.artemis.core.server.management.Notification;
import org.apache.activemq.artemis.core.transaction.ResourceManager; 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 // It is up to the user to delete the queue when finished with it
TempQueueCleanerUpper cleaner = new TempQueueCleanerUpper(server, name); TempQueueCleanerUpper cleaner = new TempQueueCleanerUpper(server, name);
if (remotingConnection instanceof TempQueueObserver) {
cleaner.setObserver((TempQueueObserver) remotingConnection);
}
remotingConnection.addCloseListener(cleaner); remotingConnection.addCloseListener(cleaner);
remotingConnection.addFailureListener(cleaner); remotingConnection.addFailureListener(cleaner);
@ -566,12 +570,18 @@ public class ServerSessionImpl implements ServerSession, FailureListener {
private final ActiveMQServer server; private final ActiveMQServer server;
private TempQueueObserver observer;
public TempQueueCleanerUpper(final ActiveMQServer server, final SimpleString bindingName) { public TempQueueCleanerUpper(final ActiveMQServer server, final SimpleString bindingName) {
this.server = server; this.server = server;
this.bindingName = bindingName; this.bindingName = bindingName;
} }
public void setObserver(TempQueueObserver observer) {
this.observer = observer;
}
private void run() { private void run() {
try { try {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
@ -579,6 +589,9 @@ public class ServerSessionImpl implements ServerSession, FailureListener {
} }
try { try {
server.destroyQueue(bindingName, null, false); server.destroyQueue(bindingName, null, false);
if (observer != null) {
observer.tempQueueDeleted(bindingName);
}
} catch (ActiveMQException e) { } catch (ActiveMQException e) {
// that's fine.. it can happen due to queue already been deleted // that's fine.. it can happen due to queue already been deleted
logger.debug(e.getMessage(), e); logger.debug(e.getMessage(), e);

View File

@ -17,7 +17,9 @@
package org.apache.activemq.artemis.tests.integration.openwire; package org.apache.activemq.artemis.tests.integration.openwire;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination; import javax.jms.Destination;
import javax.jms.InvalidDestinationException;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.MapMessage; import javax.jms.MapMessage;
import javax.jms.Message; 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) { private void checkQueueEmpty(String qName) {
PostOffice po = server.getPostOffice(); PostOffice po = server.getPostOffice();
LocalQueueBinding binding = (LocalQueueBinding) po.getBinding(SimpleString.toSimpleString("jms.queue." + qName)); LocalQueueBinding binding = (LocalQueueBinding) po.getBinding(SimpleString.toSimpleString("jms.queue." + qName));