等待RabbitMQ强制发布?

时间:2014-10-07 03:55:57

标签: rabbitmq messaging amqp bunny

将RabbitMQ与Bunny(Ruby)一起使用,我想发布并希望避免在扇出交换中没有队列消耗时丢失消息。从我的图书馆角度来看,我愿意等待回复,并在发送消息时给客户一个明确的答案。在这方面睡觉是实现这一目标的唯一途径吗?

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

1 个答案:

答案 0 :(得分:0)

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