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:
- Only visible to connections declared for the first time
- 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(); } }}