为什么rabbitmq丢失了消息?

时间:2013-02-21 06:09:10

标签: ruby rabbitmq amqp

我首先使用send.rb向rabbitmq发送100条消息,然后使用rabbitmqctl list_queues查看状态,队列中有100条消息。然后我开始recv.rb来重新发送消息,实际上我收到了100条消息。

然后,我首先启动recv.rb,然后使用send.rb发送100条消息,最后我只能获得95条消息。我尝试了很多次,在这种情况下我从来没有收到过100条消息。

为什么我丢失了消息?

recv.rb

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>'

1 个答案:

答案 0 :(得分:0)

这意味着您尝试通过已关闭的连接进行发布。请发布用于发布的脚本以及RabbitMQ日志中的最新消息。