ActiveMQ NMS Temporary Queue not destroyed when Connection is closed

1.7k views Asked by At

I've read in the Active MQ documentation that Temporary Queues are deleted by the broker when the connection that was used to create them is closed.

I'm using Apache NMS v1.5.0 and Active MQ 5.1.3, and temporary queues are always persisting even after the connection has gone out of scope.

I have a client / server scenario, whereby the client creates a temp queue and creates a message, specifying the temp queue in the ReplyTo property of the message. The server component then reads the message and starts sending messages to the reply to queue.

Unfortunately, when the client closes it's connection, the temporary queue it created does not get deleted.

The following code snippet should illustrate what i mean.

I create one connection, and create a temporary queue using that connection. I close the connection and create a second one. I should not be able to produce and consume messages on the temporary queue using a session created by the second connection, and yet i can.

Can someone tell me if i'm doing something wrong here. How can i get Active MQ to delete the Temporary Queue.

Any help much appreciated.

[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();
    }

}
1

There are 1 answers

1
Tim Bish On

Given the ancient versions you are using I don't know there's any way fix what's going on here. The code looks correct but there were a large number of fixes between the v1.5.0 release of the NMS libs and the current 1.6.0 many of which fixed issues with Temp Destinations. I'd suggest you try and move on to later versions to see if your problem goes away.

Right now you'd probably have to use JMX to access the broker and remove old temp destinations.