ab_732
ab_732

Reputation: 3897

RabbitMQ: How to prevent QueueDeclare to automatically generate a new Queue

With RabbitMQ I am doing something similar to this:

channel.QueueDeclare(QueueName, true, false, false, null);

By default RabbitMQ creates a new queue if none of the existing matches the name provided. I would like to have an exception thrown instead. Is that possible?

Thanks

Upvotes: 15

Views: 17821

Answers (2)

Fung
Fung

Reputation: 3558

You can bind to existing queue without declaring a new one.

try
{
    channel.QueueBind(queueName, exchange, routingKey);
}
catch (RabbitMQ.Client.Exceptions.OperationInterruptedException ex)
{
    // Queue not found
}

An example of the exception thrown if the queue you're trying to bind does not exist:

RabbitMQ.Client.Exceptions.OperationInterruptedException: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=404, text="NOT_FOUND - no queue 'TestQueue' in vhost '/'", classId=50, methodId=20, cause=

Upvotes: 16

Cyrille Claustre
Cyrille Claustre

Reputation: 131

Passive declarations are made for this. Use IModel.QueueDeclarePassive():

model.QueueDeclarePassive("queue-name");

This does nothing if the queue already exists, and raises an exception otherwise.

Upvotes: 13

Related Questions