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

#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 message)=0
 Override this method in derived class and implement message handling logic.
 

Member Function Documentation

◆ Process()

virtual void urabbitmq::ConsumerBase::Process ( std::string message)
protectedpure virtual

Override this method in derived class and implement message handling logic.

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.

◆ 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: