更改 RabbitMQ 队列中的参数

Change the arguments in a RabbitMQ queue

我有一个 RabbitMQ 队列,最初是这样声明的:

var result = _channel.QueueDeclare("NewQueue", true, false, false, null);

我正在尝试添加死信交换,所以我将代码更改为:

_channel.ExchangeDeclare("dl.exchange", "direct");
Dictionary<string, object> args = new Dictionary<string, object>()
{
    { "x-dead-letter-exchange", "dl.exchange" }
};            

var result = _channel.QueueDeclare("NewQueue", true, false, false, args);

当我运行这个时,我得到错误:

Exception thrown: 'RabbitMQ.Client.Exceptions.OperationInterruptedException' in RabbitMQ.Client.dll

Additional information: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text="PRECONDITION_FAILED - inequivalent arg 'x-dead-letter-exchange' for queue 'NewQueue' in vhost '/': received the value 'dl.exchange' of type 'longstr' but current is none", classId=50, methodId=10, cause=

这个错误似乎很容易解释,如果我删除队列,当我重新创建它时,我没有得到错误,但我的问题是:有没有办法在不删除队列的情况下进行此更改排队?

不,默认情况下您必须删除队列并重新创建它。

但您可以使用政策:

rabbitmqctl set_policy DLX "NewQueue" '{"dead-letter-exchange":"my-dlx"}' --apply-to queues

通过这种方式,您可以添加或删除队列 args 而无需删除它。

Read here 了解更多详情。

Configuration using policy

To specify a DLX using policy, add the key "dead-letter-exchange" to a policy definition. Similarly, an explicit routing key can be specified by adding the key "dead-letter-routing-key" to the policy.

Policies can also be defined using the management plugin, see the policy documentation for more details.