RabbitMQ offers a Priority Queue, where messages may have a priority and are delivered to consumers in reverse priority.
Using the Bunny gem, I create a prioritized queue. Then, I publish 5 messages with no priority, and 2 messages with priority 1, and check my consumer's log. Unfortunately, my consumer tells me it processes the 5 no priority messages, then the 2 messages with priority. I made sure that each message takes at least 2 seconds to process, by adding a sleep. My channel's prefetch is also set to 1. Here's sample code I used
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
I expected to see the first low-priority message, then the 2 high-priority ones, then the remaining low-priority ones.
It seems like the priority
option on #publish
is ignored. What am I doing wrong?
The mostly likely reason is mentioned in the docs in the "Interaction with consumers" section: messages that are delivered to consumers immediately without hitting the message store first are not prioritised.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With