Struct carrot_cake::consumers::ConsumerGroup
source · pub struct ConsumerGroup<Context, Error>{ /* private fields */ }
Expand description
A collection of RabbitMq consumers sharing the same context and connection settings.
ConsumerGroup
is the main entrypoint if you want to pull and process messages from queues.
Each ConsumerGroup
connects to a single host, but each consumer uses a unique connection
to that host.
§Learn by doing
Check out the consumer
example on GitHub
to see ConsumerGroup
in action.
The example showcases most of the available knobs and what they are used for.
§How do I build a ConsumerGroup
?
ConsumerGroup
provides a fluent API to add configuration step-by-step, known as
“builder pattern” in Rust.
The starting point is ConsumerGroup::builder
.
Once you are done with group-level configuration, you can start adding message handlers using
ConsumerGroupBuilder::message_handler
.
§Layered configuration
ConsumerGroup
supports a layered approach to configuring message handlers.
Certain types of configuration values can only be added at the group level (e.g. connection factory, context, queue name prefix) while others can be set both at the group and message handler level (e.g. lifecycle hooks).
Check out the builder methods for an in-depth explanation for each configuration option.
Implementations§
source§impl<Context, Error> ConsumerGroup<Context, Error>
impl<Context, Error> ConsumerGroup<Context, Error>
sourcepub fn builder(
transport_factory: ConnectionFactory,
context: impl Into<Arc<Context>>
) -> ConsumerGroupConfigurationBuilder<Context, Error>
pub fn builder( transport_factory: ConnectionFactory, context: impl Into<Arc<Context>> ) -> ConsumerGroupConfigurationBuilder<Context, Error>
Start building a ConsumerGroup
.
You will need a connection factory and a context.
§Context
In message handlers you will often need to use resources with a significant initialisation cost - e.g. a HTTP client, a database connection, etc. Instead of creating a new instance of these expensive resources every single time you handle a message, you can put those resources in the context.
The context is created once, before the consumer group is built, and each message handler gets a shared reference (&) to the context together with the incoming message. You can therefore retrieve the HTTP client or the database connection pool from the context without having to initialise them from scratch.
§Implementation Notes
The context is wrapped in an Arc
by ConsumerGroup
- if your context is already behind
an Arc
pointer, it won’t be “double-wrapped”.
sourcepub async fn run_until_sigterm(self) -> Result<(), Error>
pub async fn run_until_sigterm(self) -> Result<(), Error>
You can call run_until_sigterm
to start consuming messages from the queues you bound.
As the name implies, run_until_sigterm
returns control to the caller only if:
- one the message handlers crashes (e.g. disconnection);
- the application is stopped via SIGTERM.
sourcepub async fn run_until_shutdown(
self,
shutdown: Arc<ShutdownHandler>
) -> Result<(), Error>
pub async fn run_until_shutdown( self, shutdown: Arc<ShutdownHandler> ) -> Result<(), Error>
You can call run_until_shutdown
to start consuming messages from the queues you bound.
As the name implies, run_until_shutdown
returns control to the caller only if:
- one the message handlers crashes (e.g. disconnection);
- the application is stopped via the shutdown handler.