我先用send.rb向rabbitmq发送100条消息,然后用rabbitmqctl list_queues
它来查看状态,队列中有100条消息。然后我启动 recv.rb 来接收消息,实际上我收到了 100 条消息。
然后,我先启动recv.rb,然后用send.rb发送100条消息,最后只能得到95条消息。我尝试了很多次,在这种情况下我从来没有收到过 100 条消息。
为什么我丢失了消息?
接收文件
require 'amqp'
AMQP.start(:host => '127.0.0.1') do |connection|
channel = AMQP::Channel.new(connection)
queue = channel.queue("test_queue", :durable => true)
Signal.trap("INT") do
connection.close do
EM.stop { exit }
end
end
channel.prefetch(1)
queue.subscribe(:ack => true) do |header, body|
puts body
header.ack # A
end
end
如果我将# A
行替换为
EM.add_timer(body.count(".")) do
puts " [x] Done"
header.ack
end
和 rabbitmq-tutorials 一样,这个脚本崩溃了:
/usr/local/lib/ruby/gems/1.9.1/gems/amq-client-0.9.10/lib/amq/client/async/adapter.rb:247:in `send_frame': Trying to send frame through a closed connection. Frame is #<AMQ::Protocol::MethodFrame:0x000000019dc6e0 @payload="\x00<\x00P\x00\x00\x00\x00\x00\x00\x00\x03\x00", @channel=2> (AMQ::Client::ConnectionClosedError)
from /usr/local/lib/ruby/gems/1.9.1/gems/amq-client-0.9.10/lib/amq/client/async/channel.rb:138:in `acknowledge'
from /usr/local/lib/ruby/gems/1.9.1/gems/amqp-0.9.8/lib/amqp/channel.rb:1003:in `acknowledge'
from /usr/local/lib/ruby/gems/1.9.1/gems/amqp-0.9.8/lib/amqp/header.rb:35:in `ack'
from recv.rb:22:in `block (3 levels) in <main>'
from /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `call'
from /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /usr/local/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from /usr/local/lib/ruby/gems/1.9.1/gems/amqp-0.9.8/lib/amqp/connection.rb:38:in `start'
from recv.rb:5:in `<main>'