RabbitMQ:如何防止QueueDeclare自动生成新的Queue

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

对于 RabbitMQ,我正在做类似的事情:

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

默认情况下,如果 none 个现有队列与提供的名称匹配,则 RabbitMQ 会创建一个新队列。我想抛出异常。 这可能吗?

谢谢

您可以绑定到现有队列而无需声明新队列。

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

如果您尝试绑定的队列不存在,则抛出异常的示例:

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=

为此进行了被动声明。使用 IModel.QueueDeclarePassive():

model.QueueDeclarePassive("queue-name");

如果队列已经存在,这将不执行任何操作,否则会引发异常。