wagslane / go-rabbitmq

A wrapper of streadway/amqp that provides reconnection logic and sane defaults
https://blog.boot.dev/golang/connecting-to-rabbitmq-in-golang-easy/
MIT License
805 stars 128 forks source link

use manual Ack #126

Closed parsibox closed 1 year ago

parsibox commented 1 year ago

hi can we do Ack manualy? for example we pass d variable with go to another method and then on that method directly do ACK?

popwalker commented 1 year ago

you need to define a handler function when NewConsumer, the func type is type Handler func(d Delivery) (action Action).

return param: Action, you can choonse Manual to ACK manualy

consumer, err := rabbitmq.NewConsumer(
        conn,
        func(d rabbitmq.Delivery) rabbitmq.Action {
            log.Printf("consumed: %v", string(d.Body))

            // if You choose return rabbitmq.Manual at the end of the method,you must call d.Ack() or d.Nack() to ack or nack this msg
            d.Ack(true)

            // rabbitmq.Ack, rabbitmq.NackDiscard, rabbitmq.NackRequeue
            return rabbitmq.Manual
        },
        "my_queue",
        rabbitmq.WithConsumerOptionsRoutingKey("my_routing_key"),
        rabbitmq.WithConsumerOptionsExchangeName("events"),
        rabbitmq.WithConsumerOptionsExchangeDeclare,
    )
parsibox commented 1 year ago

i now this but i mean send d.Ack(true) in another method not in handler function ,

vishal-android-freak commented 1 year ago

Surely, you need to pass the delivery instance in the other function as well

parsibox commented 1 year ago

when i do that i get this error :

 failed to acknowledge message: Exception (504) Reason: "channel/connection is not open
parsibox commented 1 year ago

i call other function with go ( goroutine ) and get error after some work

wagslane commented 1 year ago

No making it simple to Ack outside of the handler is out of scope of this library. Just use AMQP for that