The problem here is that rabbit will send you all the messages on the
first queue as soon as you subscribe, so you have a big backlog of
messages to consume on the connection.
The correct solution here is to use prefetch, but that is only
available in the upcoming 1.6 rabbitmq release (you can build it from
their hg repository if you want). With prefetch, you would do:
mq = MQ.new
mq.prefetch(1) # grab only one un-acked message at a time
mq.queue('first').subscribe(:ack=>true) do |headers, msg|
process(msg)
headers.ack
end
With rabbitmq 1.5, you can try subscribing on two different channels
(I'm not sure if this will help, but it might):
fmq = MQ.new
fmq.queue('first')....
smq = MQ.new
smq.queue('second')....
If multiple channels doesn't help and you're unable to upgrade
rabbitmq, you'll have to use pop instead of subscribe:
mq = MQ.new
f = mq.queue('first')
f.pop do |msg|
if msg
process(msg)
f.pop # get next message
else
# no message, poll again in one second
EM.add_timer(1){ f.pop }
end
end
Aman