开发者

Using exclusive + durable queues, for 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 th开发者_开发技巧at 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.


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.


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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜