userver: urabbitmq::ConsumerBase Class Reference
Loading...
Searching...
No Matches
urabbitmq::ConsumerBase Class Reference

#include <userver/urabbitmq/consumer_base.hpp>

Detailed Description

Base class for your consumers. You should derive from it and override Process method, which gets called when a new message arrives from the broker.

If your configuration is known upfront and doesn't change ar runtime consider using ConsumerComponentBase instead.

Library takes care of handling start failures and runtime failures (connection breakage/broker node downtime etc.) and will try it's best to restart the consumer.

Note
Since messages are delivered asynchronously in the background you must call Stop before derived class is destroyed, otherwise a race is possible, when Process is called concurrently with derived class destructor, which is UB.
Library guarantees at least once delivery, hence some deduplication might be needed ou your side.

Definition at line 39 of file consumer_base.hpp.

Public Member Functions

 ConsumerBase (std::shared_ptr< Client > client, const ConsumerSettings &settings)
 
void Start ()
 Start consuming messages from the broker. Calling this method on running consumer has no effect.
 
void Stop ()
 Stop consuming messages from the broker. Calling this method on stopped consumer has no effect.
 

Protected Member Functions

virtual void Process (std::string)
 You may override this method in derived class and implement message handling logic. By default it does nothing.
 
virtual void Process (ConsumedMessage msg)
 You may override this method in derived class and implement message handling logic. By default it just calls Process(std::string) with message body.
 

Member Function Documentation

◆ Process() [1/2]

virtual void urabbitmq::ConsumerBase::Process ( ConsumedMessage msg)
inlineprotectedvirtual

You may override this method in derived class and implement message handling logic. By default it just calls Process(std::string) with message body.

Definition at line 77 of file consumer_base.hpp.

◆ Process() [2/2]

virtual void urabbitmq::ConsumerBase::Process ( std::string )
inlineprotectedvirtual

You may override this method in derived class and implement message handling logic. By default it does nothing.

If this method returns successfully message would be acked (best effort) to the broker, if this method throws the message would be requeued.

Please keep in mind that it is possible for the message to be delivered again even if Process returns successfully: sadly we can't guarantee that ack ever reached the broker (network issues or unexpected shutdown, for example). It is however guaranteed for message to be requeued if Process fails.

Definition at line 70 of file consumer_base.hpp.

◆ Start()

void urabbitmq::ConsumerBase::Start ( )

Start consuming messages from the broker. Calling this method on running consumer has no effect.

Should not throw, in case of initial setup failure library will restart the consumer in the background.

◆ Stop()

void urabbitmq::ConsumerBase::Stop ( )

Stop consuming messages from the broker. Calling this method on stopped consumer has no effect.

Note
You must call this method before your derived class is destroyed, otherwise it's UB.

The documentation for this class was generated from the following file: