RabbitMQ: Exclusive Queue)

Source: Internet
Author: User
Tags rabbitmq

If you want to create a Queue that is only visible to you, that is, other users are not allowed to access it, RabbitMQ allows you to declare a Queue as Exclusive ).

This queue has the following features:

  1. Only visible to connections declared for the first time
  2. It is automatically deleted when the connection is disconnected.

The first point is to emphasize the first declaration, because another connection cannot declare the same exclusive queue; the second isOnly distinguish Connection from Channel)Different channels created from the same connection can access an exclusive queue at the same time. The connection here refers to an AMQPConnection. Taking the Java client of RabbitMQ as an example:

Connection conn = factory.newConnection();    

If you try to re-declare or access the exclusive queue in a different connection (such as publish, consume), the error of resource locking will be returned:

ESOURCE_LOCKED-cannot obtain exclusive access to locked queue 'userlogin2'

For the second point, RabbitMQ automatically deletes the queue, regardless of whether the queue is declared persistent (Durable = true ). That is to say, even if the client program declares an exclusive queue as Durable, RabbitMQ will delete this queue as long as the connection Close method is called or the client program exits.Note that the connection is disconnected, not the channel.. In fact, the previous point is the same, only the connection is different, not the channel.

 

The following is an example code that demonstrates how to access exclusive queues in different channels of the same connection:

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.