【发布时间】:2011-11-25 16:25:14
【问题描述】:
我正在尝试了解 RabbitMQ。我希望一个队列监听,当它收到一条消息时,我希望它回复一个匿名队列,该队列通过 reply_to 标头指定,包含多条消息。
到目前为止,我有以下任务,它既是 reply_to 消息的消费者也是订阅者:
desc "start_consumer", "start the test consumer"
def start_consumer
puts "Running #{AMQP::VERSION} version of the gem"
AMQP.start(:host => "localhost", :user => "guest", :password => "guest", :vhost => "/",
:logging => true, :port => 5672) do |connection|
channel = AMQP::Channel.new(connection)
requests_queue = channel.queue("one", :exclusive => true, :auto_delete => true)
Signal.trap("INT") do
connection.close do
EM.stop{exit}
end
end
channel.prefetch(1)
requests_queue.subscribe(:ack => true) do |header, body|
puts "received in server #{body.inspect}"
(0..5).each do |n|
header.ack
reply = {:reply => "Respone #{n}", :is_last => (n == 5)}
AMQP::Exchange.default.publish(
MultiJson.encode(reply),
:routing_key => header.reply_to,
:correlation_id => header.correlation_id
)
sleep(2)
end
end
puts " [x] Awaiting RPC requests"
end
end
我的电话号码是:
def publish(urlSearch, routing_key)
EM.run do
corr_id = rand(10_000_000).to_s
requests ||= Hash.new
connection = AMQP.connect(:host => "localhost")
callback_queue = AMQP::Channel.new(connection).queue("", :exclusive => true)
callback_queue.subscribe do |header, body|
reply = MultiJson.decode(body)
if reply[:is_last.to_s]
connection.close do
EM.stop{exit}
end
end
end
callback_queue.append_callback(:declare) do
AMQP::Exchange.default.publish(MultiJson.encode(urlSearch), :routing_key => routing_key, :reply_to => callback_queue.name, :correlation_id => corr_id)
end
end
问题是直到迭代中的所有消息都发布后才会发送消息。
即在(0..5)循环中的迭代完成之后,所有的消息都会被发布。
有人告诉我,使用 EventMachine::defer 可能是一种选择,但我不确定如何将其应用于循环。
任何人都可以提出如何解决这个问题的指针吗? EventMachine::defer 是一个不错的选择吗?如果是,在使用 AMQP.start 时我该怎么做?
【问题讨论】:
标签: ruby rabbitmq eventmachine