私のタスクのほとんどはネットワークに依存しているため、一度に 1 つのメッセージだけでなく、キューを並行して処理したいと考えています。
だから、私は次のコードを使用しています:
#!/usr/bin/env ruby
# encoding: utf-8
require "rubygems"
require 'amqp'
EventMachine.run do
connection = AMQP.connect(:host => '127.0.0.1')
channel = AMQP::Channel.new(connection)
channel.prefetch 5
queue = channel.queue("pending_checks", :durable => true)
exchange = channel.direct('', :durable => true)
queue.subscribe(:ack => true) do |metadata, payload|
time = rand(3..9)
puts 'waiting ' + time.to_s + ' for message ' + payload
sleep(time)
puts 'done with '+ payoad
metadata.ack
end
end
プリフェッチ設定が使用されないのはなぜですか? 5 つのメッセージを取得し、それらを並行して処理する必要があると思います。