Struct carrot_cake::consumers::ConsumerGroup

source ·
pub struct ConsumerGroup<Context, Error>
where Context: Send + Sync + 'static, Error: Send + Sync + 'static,
{ /* 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>
where Context: Send + Sync + 'static, Error: Send + Sync + 'static,

source

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”.

source

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.
source

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.

Auto Trait Implementations§

§

impl<Context, Error> Freeze for ConsumerGroup<Context, Error>

§

impl<Context, Error> !RefUnwindSafe for ConsumerGroup<Context, Error>

§

impl<Context, Error> Send for ConsumerGroup<Context, Error>

§

impl<Context, Error> Sync for ConsumerGroup<Context, Error>

§

impl<Context, Error> Unpin for ConsumerGroup<Context, Error>

§

impl<Context, Error> !UnwindSafe for ConsumerGroup<Context, Error>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

source§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more