2016-03-04 39 views
0

有什麼方法暫時暫停消費者,並在稍後恢復?如何暫停和恢復兔子的消費

這裏是我想要做的一個例子:

require "bunny" 
conn = Bunny.new 
conn.start 

ch1 = conn.create_channel 
publisher = ch.direct('test', :auto_delete => false) 

consumer1 = nil 
Thread.new do 
    ch2 = conn.create_channel(nil, 8) #Using eight worker 
    queue1 = ch2.queue('', :exclusive => true) 
    queue1.bind(publisher, :routing_key => 'low_priority') 
    consumer1 = queue1.subscribe(:block => true) do |delivery_info, properties, payload| 
     #do some work 
    end 
end 

Thread.new do 
    ch3 = conn.create_channel 
    queue2 = ch3.queue('', :exclusive => true) 
    queue2.bind(publisher, :routing_key => 'high_priority') 
    consumer2 = queue2.subscribe(:block => true) do |delivery_info, properties, payload| 
     consumer1.pause #pause the other consumer 
     #do other things 
     consumer1.resume #resume the consumer 
    end 
end 
#rest of the code 

我想暫停時,我做的consumer2工作consumer1。有沒有任何有效的方法來做到這一點?

回答

0

如果你想創建優先政策,兔子已經實現了這個:

http://rubybunny.info/articles/queues.html#consumer_priorities

q = ch.queue("a.queue") 
q.subscribe(:manual_ack => true, :arguments => {"x-priority" => 5}) do |delivery_info, properties, payload| 
    # ... 
end 
q.subscribe(:manual_ack => true, :arguments => {"x-priority" => 2}) do |delivery_info, properties, payload| 
    # ... 
end 

如果你要處理它在並行我建議這種寶石:

https://github.com/grosser/parallel

示例:

results = Parallel.map(['a','b','c'], in_processes: 3) { |one_letter| ... } 

希望它有幫助。

+0

您能否提供您提供的兩個鏈接的簡介?如果鏈接在未來破裂,那麼答案就在這裏。您可以編輯您的答案以包含此信息。 – Theresa

+0

@Theresa更新:)謝謝 –