streadway / amqp

Go client for AMQP 0.9.1
http://godoc.org/github.com/streadway/amqp
BSD 2-Clause "Simplified" License
4.88k stars 621 forks source link

How to return message to the end of queue in rabbitmq? #414

Open lzyrapx opened 5 years ago

lzyrapx commented 5 years ago

Hi, all. I'd like to ask how to return message to the end of queue in rabbitmq?

Example:

The Code below did not work.

    for delivery := range msg {
               exist := check()
        if exist != nil { //yes
                        update()
            err := delivery.Ack(false)
            logger.Info("Ack error : ", err)
        } else { // no
            // err := delivery.Reject(true)
            err := delivery.Nack(false, true)
            logger.Info("Reject error : ", err)
        }
   }

Thanks in advance.

shekhar-kamble commented 5 years ago

When requeue is true, request the server to deliver this message to a different consumer. If it is not possible or requeue is false, the message will be dropped or delivered to a server configured dead-letter queue.

refer to https://godoc.org/github.com/streadway/amqp#Delivery.Nack

houseofcat commented 5 years ago

@LzyRapx A popular strategy is to pull out the message from a Channel with Confirm enabled, so you can perform Acks/Nacks like you have sample code for. Then you try to perform some operation against the message - handle when things did not go as planned by publishing the message to an alternative queue. Once successfully published to other queue, acknowledge message from the original channel. In your case, you would publish to the source Queue (instead of another destination) and it would naturally be at the bottom for replay later.

lariknep commented 3 years ago

The way to requeue the message to the beginning of the source queue (republish) is create a queue dead-letter-exchange policy without Exchange Name specification. In this case a rejected message will be republished back to the original Exchange with the original Routing Key. This kind of policy should be established through the broker HTTP API.