如何将消息从unacked推送到就绪

时间:2018-03-23 15:30:39

标签: ruby rabbitmq bunny

我的问题类似于之前提出的问题,但它没有找到答案,我有一个消费者,我想要处理一个称为Web服务的操作,但是,如果此Web服务由于某种原因没有响应,我希望消费者不要处理RabbitMQ的消息,但是我想让它稍后处理它,我的消费者是以下消费者:

require File.expand_path('../config/environment.rb', __FILE__)
conn=Rabbit.connect
conn.start
ch = conn.create_channel
x = ch.exchange("d_notification_ex", :type=> "x-delayed-message", :arguments=> { "x-delayed-type" => "direct"})
q  = ch.queue("d_notification_q", :durable =>true)
q.bind(x)
p 'Wait ....'
q.subscribe(:manual_ack => true, :block => true) do |delivery_info, properties, body|

  datos=JSON.parse(body)
  if datos['status']=='request'
    #I call a web service and process the json
    result=Notification.send_payment_notification(datos.to_json)
  else
    #I call a web service and process the body
    result=Notification.send_payment_notification(body)
  end
   #if the call to the web service, the web server is off the result will be equal to nil
   #therefore, he did not notify RabbitMQ, but he puts the message in UNACKED status
   # and does not process it later, when I want him to keep it in the queue and evaluate it afterwards.
  unless result.nil?
  ch.ack(delivery_info.delivery_tag)
  end

end

RabbitMQ的图片,

enter image description here

在语句中有一些方法:c hack(delivery_info.delivery_tag),这个而不是删除队列的元素可以在以后处理它,任何想法?感谢

2 个答案:

答案 0 :(得分:0)

RabbitMQ团队监控this mailing list,有时只回答StackOverflow上的问题。

试试这个:

if result.nil?
  ch.nack(delivery_info.delivery_tag)
else
  ch.ack(delivery_info.delivery_tag)
end

答案 1 :(得分:0)

我决定使用消费者"中的样式"生产者将数据发送回队列,我的代码现在看起来像这样:

  if result.eql? 'ok'
  ch.ack(delivery_info.delivery_tag)
  else
    if(datos['count'] < 5)
      datos['count'] += 1
      d_time=1000
      x.publish(datos.to_json,  :persistent => true, :headers=>{"x-delay" => d_time})
    end
  end

但是我被迫在JSON属性中再包含一个属性:Count!所以它不会停留在无限循环中。