5

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?

François Beausoleil
  • 16,265
  • 11
  • 67
  • 90
  • I'm diving into this issue today, so hopefully I'll be able to report back! From what I've read though, the priority option on publish is not used by RabbitMQ. `:priority (Integer) — Message priority, 0 to 9. Not used by RabbitMQ, only applications` – Teddy May 07 '15 at 09:31

1 Answers1

1

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.

Michael Klishin
  • 786
  • 3
  • 6