2014-10-07 44 views
0

将RabbitMQ与Bunny(Ruby)结合使用,我想发布并希望避免在扇出交换中不消耗队列时丢失消息。从我的图书馆角度来看,如果邮件发送与否,我愿意等待回复,并给予客户明确的答复。是在这个睡觉,实现这一目标的唯一途径?等待RabbitMQ强制发布?

def publish(topic, message_type, message, key = nil) 
    ch = @conn.create_channel 
    exchange = ch.fanout(topic, :durable => true, :auto_delete => false) 

    sent = true 
    exchange.on_return do |return_info, properties, content| 
     sent = false 
    end 

    exchange.publish(message, 
     :mandatory => true, 
     :persistent => true, 
     :type => message_type, 
     :app_id => Emque::Producing.configuration.app_name, 
     :content_type => "application/json") 

    # Give message a chance to return if it can't be routed 
    sleep 0.5 

    ch.close 
    return sent 
end 

回答

0

basic.return是协议中的异步操作。您可以使用同步原语(例如,Bunny具有Concurrent :: Condition,因为Ruby缺乏与java.util.concurrent类似的任何东西),但您确实希望发布商确认。请参阅“何时会确认消息?”在http://www.rabbitmq.com/confirms.html