Rabbitmq 我怎么可能比工人们更放松呢?
我有一个工人在排队。impl。看起来是这样的:Rabbitmq 我怎么可能比工人们更放松呢?,rabbitmq,bunny,Rabbitmq,Bunny,我有一个工人在排队。impl。看起来是这样的: def work connection = get_connection connection.start channel = connection.create_channel queue = channel.queue("crawl", :durable => true) multi_log " [*] Worker waiting for messages in #{queue.name}.
def work
connection = get_connection
connection.start
channel = connection.create_channel
queue = channel.queue("crawl", :durable => true)
multi_log " [*] Worker waiting for messages in #{queue.name}. To exit press CTRL+C"
begin
queue.subscribe(:ack => true, :block => true) do |delivery_info, properties, message|
multi_log " [x] Worker received #{message}"
process_work message
channel.ack(delivery_info.delivery_tag)
multi_log " [x] Worker job done for #{message}"
end
rescue => e
log.error e.message
log.error e.backtrace.join("\n")
connection.close
end
end
只要作业被执行,使用者就会阻塞。据我所知,该消费者一次只处理一份工作。但让我困惑的是,在RabbitMQ管理UI中,我有时会看到2或3个队列未被确认。但这怎么可能呢 解决方案的发布端正在填充队列(通过exchange间接填充-无法说明如何填充,因为您没有提供详细信息)。订阅者(工作者)的数量与未确认消息的数量无关。在你的例子中,似乎很简单:发布者比订阅者快。我昨天晚上就知道了:)问题是预回迁没有明确设置。如果设置
频道。预回迁(1)
,则消费者一次只接收一条消息。如果未设置预取值,则使用者将尽可能多地接收消息。所以完整的代码现在看起来是这样的
def work
connection = get_connection
connection.start
channel = connection.create_channel
channel.prefetch(1)
queue = channel.queue("crawl", :durable => true)
multi_log " [*] Worker waiting for messages in #{queue.name}. To exit press CTRL+C"
begin
queue.subscribe(:ack => true, :block => true) do |delivery_info, properties, message|
multi_log " [x] Worker received #{message}"
process_work message
channel.ack(delivery_info.delivery_tag)
multi_log " [x] Worker job done for #{message}"
end
rescue => e
log.error e.message
log.error e.backtrace.join("\n")
connection.close
end
end
例如,现在RabbitMQ管理UI显示有9条消息准备就绪,1条未确认,总共10条。这是意料之中的事
顺便说一下,在Java中,您可以这样设置预取
channel.basicQos(1)代码> 文档中还提到了其他内容:。未确认的消息是已发送给消费者/工作人员但消费者尚未回退确认的消息。我发现我写了一些错误的东西:我的意思是,未确认的MSG数量由于发布者的原因而增加,但订阅者当然会减少。只需在没有工作人员的情况下运行场景,但控制已发布消息的数量,然后查看发生了什么情况。不,我错了。您是指从服务器到消费者的ACK?