AMQP宝石指定死信交换

时间:2013-08-19 13:40:41

标签: ruby rabbitmq amqp

我在RabbitMQ服务器上指定了一个名为MyQueue的队列。它经久耐用,x-dead-letter-exchange设置为MyQueue.DLX

(我还有一个名为MyExchange的交换绑定到该队列,另一个交换名为MyQueue.DLX,但我不认为这对问题很重要。

如果我使用ruby的amqp gem订阅这些消息,我会这样做:

# Doing this before and in a new thread has to do with how my code is structured
# shown here in case it has a bearing on the question
Thread.new do
  AMQP.start('amqp://guest:guest@127.0.0.1:5672')
end

EventMachine.next_tick do
  channel = AMQP::Channel.new(AMQP.connection)

  queue = channel.queue("MyQueue", :durable => true, :'x-dead-letter-exchange' => "MyQueue.DLX")

  queue.subscribe(:ack => true) do |metadata, payload|
    p metadata
    p payload
  end
end

如果我使用已创建和绑定的队列和交换执行此代码(因为它们需要在我的设置中),那么RabbitMQ会在其日志中抛出以下错误:

=ERROR REPORT==== 19-Aug-2013::14:25:53 ===
connection <0.19654.2>, channel 2 - soft error:
{amqp_error,precondition_failed,
        "inequivalent arg 'x-dead-letter-exchange'for queue 'MyQueue' in vhost '/': received none but current is the value 'MyQueue.DLX' of type 'longstr'",
        'queue.declare'}

这似乎在说我没有指定与预先存在的队列相同的死信交换 - 但我相信我有queue = ...行。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

DLX信息在arguments选项中传递:

queue = channel.queue("MyQueue", {durable: true, arguments: {"x-dead-letter-exchange" => "MyQueue.DLX"}})

答案 1 :(得分:0)

我有同样的错误,即使使用@Karl Wilbur的格式作为选项。

看起来像你的&#34; MyQueue&#34;已存在于RabbitMQ服务器上(持久:true),并且它没有死信交换配置。

queue = channel.queue("MyQueue", :durable => true, :'x-dead-letter-exchange' => "MyQueue.DLX")

如果名称&#34; MyQueue&#34;已存在,则不会创建新队列。相反,它会尝试连接到现有的,但选项/参数等必须相同,否则你会得到一个错误,就像你得到的那样。

您所要做的就是删除旧代码并再次运行代码(根据Karl的建议)。

我使用RabbitMQ管理GUI来删除我的。见here re deleting queues