Mac上のRubyのrabbitmqがメッセージを受信しませんでした
概要
私はrabbitmqを初めて使用します。 Rubyとrabbitmqを使用してMacでチュートリアルに従ってみました。 2 つの .rb ファイルを作成しました。
ワーカー.rb
require 'bunny'
connection=Bunny.new
connection.start
channel= connection.create_channel
queue=channel.queue('hello')
begin
puts ' Waiting for messages. To exit press CTRL+C'
queue.subscribe(block: true) do |delivery_info, _properties, body|
puts " [x] Received #{body}"
# imitate some work
sleep body.count('.').to_i
puts ' [x] Done'
end
rescue Interrupt => _
conn.close
exit(0)
end
そしてreceive.rb
require 'bunny'
connection=Bunny.new
connection.start
channel= connection.create_channel
queue=channel.queue('hello')
begin
puts ' Waiting for messages. To exit press CTRL+C'
queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts " Received #{body}"
end
rescue Interrupt => _
conn.close
exit(0)
end
RabbitMQ サーバーが開いています。 Ruby new_task.rb を実行すると、予想どおりの出力 [x] Sent Hello World! が表示されます。 Ruby worker.rb を実行すると、メッセージを待っていると表示されますが、メッセージを受信しません。誰か助けてくれませんか?
解決策
チュートリアルのコードを注意深く調べて、自分が書いたものと比較することをお勧めします。
new_task.rb のコードを提供していないため、ここに表示されているコードを使用していると思います。このコードは task_queue という名前のキューを想定していますが、上記で指定したコードでは hello という名前のキューが使用されています。