0

我的问题类似于之前提出的问题,但是它没有找到答案,我有一个消费者,我想处理一个称为 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 的图像,

在此处输入图像描述

语句中有某种方式:c hack(delivery_info.delivery_tag),这样可以代替删除队列的元素,以后再处理,有什么想法吗?谢谢

4

2 回答 2

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!这样它就不会停留在无限循环中。

于 2018-03-23T21:25:51.073 回答
0

RabbitMQ 团队会监控这个邮件列表,有时只会在 StackOverflow 上回答问题。


尝试这个:

if result.nil?
  ch.nack(delivery_info.delivery_tag)
else
  ch.ack(delivery_info.delivery_tag)
end
于 2018-03-23T19:12:37.237 回答