将 RabbitMQ 与 Bunny (Ruby) 一起使用,如果没有从 fanout 交换消耗的队列,我想发布并希望避免丢失消息。从我图书馆的角度来看,我愿意等待返回,如果消息已发送,我会向客户给出明确的答复。睡觉是实现这一目标的唯一方法吗?
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