Skip to main content

MessageConsumer

Trait MessageConsumer 

Source
pub trait MessageConsumer: Send + Sync {
    // Required methods
    fn receive_batch<'life0, 'async_trait>(
        &'life0 mut self,
        _max_messages: usize,
    ) -> Pin<Box<dyn Future<Output = Result<ReceivedBatch, ConsumerError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn as_any(&self) -> &dyn Any;

    // Provided methods
    fn on_connect_hook(&self) -> Option<BoxFuture<'_, Result<()>>> { ... }
    fn on_disconnect_hook(&self) -> Option<BoxFuture<'_, Result<()>>> { ... }
    fn receive<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = Result<Received, ConsumerError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn receive_batch_helper<'life0, 'async_trait>(
        &'life0 mut self,
        _max_messages: usize,
    ) -> Pin<Box<dyn Future<Output = Result<ReceivedBatch, ConsumerError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn status<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = EndpointStatus> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}

Required Methods§

Source

fn receive_batch<'life0, 'async_trait>( &'life0 mut self, _max_messages: usize, ) -> Pin<Box<dyn Future<Output = Result<ReceivedBatch, ConsumerError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Receives a batch of messages.

This method must be implemented by all consumers. If in doubt, implement receive_batch to return a single message as a vector.

Source

fn as_any(&self) -> &dyn Any

Provided Methods§

Source

fn on_connect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Returns an optional lifecycle hook that runs once after the consumer connection is created.

The route awaits this hook before it reports itself as ready. Returning an error fails route startup and lets the outer route runner reconnect or surface the startup failure.

Use this for per-connection setup that should be shared by all messages read through this consumer, such as warming a connection pool, creating SQLite tables or indexes, setting up a Kafka consumer group, or authenticating a RabbitMQ channel.

fn on_connect_hook(&self) -> Option<BoxFuture<'_, anyhow::Result<()>>> {
    Some(Box::pin(async move {
        self.pool.get().await?;
        self.db.execute("CREATE TABLE IF NOT EXISTS embeddings (...)").await?;
        Ok(())
    }))
}
Source

fn on_disconnect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Returns an optional lifecycle hook that runs before the consumer is dropped.

The route awaits this hook during shutdown or reconnect cleanup. Errors are logged as warnings and do not replace the route’s original result.

Source

fn receive<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<Received, ConsumerError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Receives a single message.

Source

fn receive_batch_helper<'life0, 'async_trait>( &'life0 mut self, _max_messages: usize, ) -> Pin<Box<dyn Future<Output = Result<ReceivedBatch, ConsumerError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source

fn status<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = EndpointStatus> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§