If you want a queue to be visible only to yourself, you can use the exclusive queue feature provided by ApsaraMQ for RabbitMQ to declare the specified queue as exclusive.
What is an exclusive queue?
An exclusive queue has the following characteristics:
An exclusive queue is visible only to the channels in the earliest connection in which you declare the queue as exclusive.
After a queue is declared exclusive in one connection, you cannot declare an exclusive queue that has the same name in other connections, and channels in other connections cannot access the exclusive queue.
An exclusive queue is automatically deleted if the connection of the queue is closed.
Even if you set the
durable
parameter totrue
on the client to declare the exclusive queue as persistent, the queue is deleted and the data in the queue is cleared if the connection of the queue is closed.
Common scenarios
Exclusive queues are suitable for scenarios in which producers and consumers are in the same process and are not sensitive to data loss.
Risks
An exclusive queue is deleted and the data in the queue is cleared if the connection of the queue is accidentally closed in one of the following scenarios:
The calling of the
Close()
method by the clientBroker updates or abnormal restarts
Connection heartbeat timeout
Other errors such as throttling
Exercise caution when you use an exclusive queue.
How to declare an exclusive queue
You can use the channel.queueDeclare
method and set the exclusive
parameter to true
to declare an exclusive queue in a client program.
The following sample code provides an example on how to declare an exclusive queue in Java:
/**
* queue: the queue name.
* durable: specifies whether to persist the queue.
* exclusive: specifies whether the queue is exclusive.
* autoDelete: specifies whether to automatically delete the queue.
* arguments: other parameters.
*/
channel.queueDeclare("queueName", true, true, false, null);