Ruby Bunny - 从多个队列中消费
Ruby Bunny - Consuming from Multiple Queues
我刚开始使用 Ruby 并且正在写一篇文章来使用 RabbitMQ 队列中的一些消息。我正在使用 Bunny 这样做。
所以我创建了队列并将它们绑定到交换器。
但是我现在不确定我如何处理订阅它们并允许 ruby 应用程序继续 运行(希望消息继续通过,即不被阻止或至少不被阻止很长一段时间)直到我用 ctrl+c 真正退出它。
我试过使用 :block => true
但是因为我订阅了 2 个不同的队列,使用这意味着它仍然只消耗一个。
这就是我使用消息的方式:
def consumer
begin
puts ' [*] Waiting for messages. To exit press CTRL+C'
@oneQueue.subscribe(:manual_ack => true) do |delivery_info, properties, payload|
puts('Got One Queue')
puts "Received #{payload}, message properties are #{properties.inspect}"
end
@twoQueue.subscribe(:manual_ack => true) do |delivery_info, properties, payload|
puts('Got Two Queue')
puts "Received #{payload}, message properties are #{properties.inspect}"
end
rescue Interrupt => _
#TODO - close connections here
exit(0)
end
end
如有任何帮助,我们将不胜感激。
谢谢!
当您有两个订阅时,您不能使用 block: true
,因为只有第一个会阻塞;它永远不会进入第二个订阅。
你可以做的一件事是设置两个订阅没有阻塞(这将自动产生两个线程来处理消息),然后用等待循环阻塞你的主线程(在您的 rescue
):
之前添加
loop { sleep 5 }
我刚开始使用 Ruby 并且正在写一篇文章来使用 RabbitMQ 队列中的一些消息。我正在使用 Bunny 这样做。
所以我创建了队列并将它们绑定到交换器。
但是我现在不确定我如何处理订阅它们并允许 ruby 应用程序继续 运行(希望消息继续通过,即不被阻止或至少不被阻止很长一段时间)直到我用 ctrl+c 真正退出它。
我试过使用 :block => true
但是因为我订阅了 2 个不同的队列,使用这意味着它仍然只消耗一个。
这就是我使用消息的方式:
def consumer
begin
puts ' [*] Waiting for messages. To exit press CTRL+C'
@oneQueue.subscribe(:manual_ack => true) do |delivery_info, properties, payload|
puts('Got One Queue')
puts "Received #{payload}, message properties are #{properties.inspect}"
end
@twoQueue.subscribe(:manual_ack => true) do |delivery_info, properties, payload|
puts('Got Two Queue')
puts "Received #{payload}, message properties are #{properties.inspect}"
end
rescue Interrupt => _
#TODO - close connections here
exit(0)
end
end
如有任何帮助,我们将不胜感激。
谢谢!
当您有两个订阅时,您不能使用 block: true
,因为只有第一个会阻塞;它永远不会进入第二个订阅。
你可以做的一件事是设置两个订阅没有阻塞(这将自动产生两个线程来处理消息),然后用等待循环阻塞你的主线程(在您的 rescue
):
loop { sleep 5 }