CopernicaMarketingSoftware / AMQP-CPP

C++ library for asynchronous non-blocking communication with RabbitMQ
Apache License 2.0
884 stars 340 forks source link

Doc doesn't explicitely say if Channel::publish() methods are async or not #503

Closed PierreBarnabe closed 1 year ago

PierreBarnabe commented 1 year ago

Hello,

Thanks a lot for this very nice library.

I have one single remark/question, to understand the exact behavior of the set of Channel::publish() methods: Are those also "non blocking" methods, such as all the others Channel methods? The reason I'm still unsure is that all the other methods return a Deferred object, while those return bool values.

Maybe it would be useful to explicitly say it in the doc (in case it confuses other people) ?

Thanks!

Pierre

EmielBruijntjes commented 1 year ago

Yes, they are also non-blocking. But normally, RabbitMQ does not send a confirmation back, so there is by default no deferred object with handlers to check for success or failure. And there is also no guarantee that the publish() call succeeded.

If you do need more reliability, you can however use publish-confirms. This is a feature that has to be explicitly enabled, to instruct RabbitMQ to send an "ack" for every message published. You can enable this by calling the Channel::confirmSelect(), or you wrap your channel in an AMQP::Reliable object, which implicitly enables publisher-confirms, and offers a convenient alternative publish() method:

// wrap the channel in an AMQP::Reliable for confirmed publishing
AMQP::Reliable<> reliable(mychannel);

// publish the message
reliable.publish(exchange, routingkey, envelope).onAck([]() {
    // @todo add your own handling (the message has been processed by rabbitmq)
}).onLost([]() {
    // @todo add your own handling (rabbitmq was unable to process the message)
});

Above is pseudo-code, I am not 100% sure that all classnames, methods, functions and parameters are exactly named like that, but you'll manage to find out.

PierreBarnabe commented 1 year ago

Thanks a lot for the very clear answer!