Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using exclusive + durable queues, for RabbitMQ

Tags:

rabbitmq

If I have made a queue which is exclusive and durable (not auto-delete). Now, if the consumer subscribes to that queue and then it goes down. Then that queue gets deleted.

I have checked the scenario, when the queue is only durable (i.e. neither exclusive nor auto-delete). Now, if the consumer subscribes to that queue and then it goes down. Then that queue gets deleted.

Please explain the 1st case, 2nd case is giving expected result. In both the scenario only 1 consumer is subscribed to one queue, and there is only one queue bound to one direct_exchange.

like image 605
Gurpreet Singh Avatar asked Jul 06 '11 13:07

Gurpreet Singh


2 Answers

If you have a queue that is exclusive, then when the channel that declared the queue is closed, the queue is deleted.

If you have a queue that is auto-deleted, then when there are no subscriptions left on that queue it will be deleted.

These two rules apply even for durable queues.

like image 118
Rob Harrop Avatar answered Sep 22 '22 11:09

Rob Harrop


One thing to correct, the exclusive queue will be deleted after the connection is closed not the channel is closed. you can run this test:

package rabbitmq.java.sample.exclusivequeue;

import java.io.IOException;

import com.rabbitmq.client.*;
import com.rabbitmq.client.AMQP.Queue.DeclareOk;

public class Producer {

    private final static String QUEUE_NAME = "UserLogin2";
    private final static String EXCHANGE_NAME = "user.login";
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("CNCDS108");
        try {
            Connection conn = factory.newConnection();          
            Channel channel =conn.createChannel();
            DeclareOk declareOk = channel.queueDeclare(QUEUE_NAME, false, true, false, null);
                
            channel.basicPublish("", QUEUE_NAME, null, "Hello".getBytes());
            
            //close the channel, check if the queue is deleted
            System.out.println("Try to close channel");
            channel.close();
            System.out.println("Channel closed");
            
            System.out.println("Create a new channel");
            Channel channel2 =conn.createChannel();
            DeclareOk declareOk2 = channel2.queueDeclarePassive(QUEUE_NAME);
            
            **//we can access the exclusive queue from another channel
            System.out.println(declareOk2.getQueue()); //will output "UserLogin2"
            channel2.basicPublish("", QUEUE_NAME, null, "Hello2".getBytes());
            System.out.println("Message published through the new channel");**
            
//          System.out.println("Try to close Connection");
//          conn.close();
//          System.out.println("Connection closed");
            
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
like image 20
Rader Avatar answered Sep 24 '22 11:09

Rader