RabbitMQ提供了Priority Queue,其中消息可能具有优先级,并以相反的优先级传递给消费者。
使用Bunny gem,我创建了一个按优先级排序的队列。然后,我发布了5条没有优先级的消息和2条优先级为1的消息,并检查了我的使用者的日志。不幸的是,我的客户告诉我,它先处理5条无优先级消息,然后处理2条有优先级的消息。通过添加休眠,我确保每条消息至少需要2秒来处理。我的通道的预取也设置为1。
require "bunny"
require "logger"
logger = Logger.new(STDERR)
bunny = Bunny.new(ENV["AMQP_URL"], logger: logger)
bunny.start
at_exit { bunny.stop }
channel = bunny.channel
channel.prefetch 1
routing_key = "build-show-report"
exchange = channel.exchange("signals", passive: true)
queue = channel.queue("signal.#{routing_key}", durable: true, arguments: {"x-max-priority" => 3})
queue.bind(exchange, routing_key: routing_key)
queue.subscribe(manual_ack: true, block: false) do |delivery_info, properties, payload|
logger.info "Received #{payload}"
sleep 2
channel.acknowledge(delivery_info.delivery_tag, false)
end
5.times {|n| exchange.publish(n.to_s, routing_key: "build-show-report")}
2.times {|n| exchange.publish((10*n).to_s, routing_key: "build-show-report", priority: 1)}
sleep 30我希望看到第一个低优先级的消息,然后是两个高优先级的消息,然后是剩下的低优先级消息。
#publish上的priority选项似乎被忽略了。我做错了什么?
发布于 2016-04-05 20:34:35
最可能的原因是提到了in the docs in the "Interaction with consumers" section:在没有首先到达消息库的情况下立即传递给消费者的消息没有优先级。
https://stackoverflow.com/questions/29304827
复制相似问题