连接关闭时,ActiveMQ NMS临时队列未被销毁

本文关键字:队列 NMS ActiveMQ 连接 | 更新日期: 2023-09-27 18:29:14

我在Active MQ文档中读到,当用于创建临时队列的连接关闭时,代理会删除临时队列。

我使用的是Apache NMS v1.5.0和Active MQ 5.1.3,即使在连接超出范围后,临时队列也始终保持不变。

我有一个客户端/服务器场景,其中客户端创建一个临时队列并创建一条消息,在消息的ReplyTo属性中指定临时队列。然后,服务器组件读取消息,并开始向回复队列发送消息。

不幸的是,当客户端关闭其连接时,它创建的临时队列不会被删除。

下面的代码片段应该说明我的意思。

我创建一个连接,并使用该连接创建一个临时队列。我关闭连接并创建第二个连接。我应该无法使用会话在临时队列上生成和使用消息由第二个连接创建,但我可以。

有人能告诉我我是不是做错了什么吗。如何让Active MQ删除临时队列。

非常感谢您的帮助。

[Test]
public void TempQueueTest()
{
    var cf = new ConnectionFactory("tcp://activemq-broker:61616");

    using (var connection = cf.CreateConnection())
    {
        connection.Start();
        using (var session = connection.CreateSession())
        {
            var normalQueue = session.GetQueue("normalQueue");
            ITemporaryQueue tempQueue = session.CreateTemporaryQueue();
            using (var producer = session.CreateProducer(normalQueue))
            {
                // create a messasge and put on a normal queue
                //specify the temp queue as the reply to queue
                var mesage = new ActiveMQTextMessage("hello");
                mesage.ReplyTo = tempQueue as ActiveMQDestination;
                producer.Send(mesage);
            }
        }
        connection.Stop();
    }

    // ok, connection has been disposed, so the temp queue should be destroyed
    // create a new connection
    using (var connection = cf.CreateConnection())
    {
        connection.Start();
        using (var session = connection.CreateSession())
        {
            var normalQueue = session.GetQueue("normalQueue");
            using (var consumer = session.CreateConsumer(normalQueue))
            {
                var message = consumer.Receive() as ActiveMQTextMessage;
                // replyToDest is the temp queue created with the previous connection
                var replyToDest = message.ReplyTo;

                using (var producer = session.CreateProducer(replyToDest))
                {
                    // i shouldn't be able to send a message to this temp queue
                    producer.Send(new ActiveMQTextMessage("this shouldn't work"));
                }
                using (var tempConsumer = session.CreateConsumer(replyToDest))
                {
                    // is shouldn't be able to receive messages on the temp queue as it should be destroyed
                    var message1 = tempConsumer.Receive() as ActiveMQTextMessage;
                }
            }
        }
        connection.Stop();
    }
}

连接关闭时,ActiveMQ NMS临时队列未被销毁

考虑到您正在使用的古代版本,我不知道有任何方法可以解决这里发生的问题。代码看起来是正确的,但在NMS库的v1.5.0版本和当前的1.6.0版本之间有大量修复,其中许多修复了Temp Destinations的问题。我建议你试着继续使用更高版本,看看你的问题是否消失了。

现在,您可能必须使用JMX来访问代理并删除旧的临时目的地。