Skip to main content

Consumer

Struct Consumer 

Source
pub struct Consumer { /* private fields */ }

Implementations§

Source§

impl Consumer

Source

pub async fn subscribe(&mut self, topics: Vec<String>) -> Result<()>

Subscribe to topics.

In group mode (non-empty group_id), this triggers a consumer group join to receive partition assignments from the group coordinator.

In direct mode (empty group_id), this fetches all partitions of the given topics from the cluster metadata and starts consuming them.

This method blocks until partition assignment and offset initialization are complete. In group mode, this may take up to the rebalance timeout.

Source

pub async fn assign( &mut self, topic: impl Into<String>, partitions: Vec<i32>, ) -> Result<()>

Manually assign specific partitions to consume (direct mode only).

This is the direct-mode equivalent of subscribe(), giving you fine-grained control over which topic-partitions to consume.

This method blocks until offset initialization completes.

Source

pub async fn seek( &self, topic: impl Into<String>, partition: i32, offset: i64, ) -> Result<()>

Seek to a specific offset for a partition (direct mode only).

Source

pub async fn unsubscribe(&mut self) -> Result<()>

Unsubscribe from all topics.

In direct mode, this clears all partition assignments and offsets. In group mode, this also sends a LeaveGroup request and stops the heartbeat loop.

After unsubscribe(), you can call subscribe() or assign() again to start consuming different topics.

Source

pub async fn poll(&mut self) -> Result<Vec<ConsumerRecord>>

Block until records are available or the channel is closed.

The first call signals the background task to start fetching (if not already started by into_stream). Subsequent calls continue reading from the prefetched buffer.

§Errors

Returns [KafkaError::ConnectionClosed] if the background consumer task has terminated (e.g. due to a fatal error).

Source

pub async fn poll_timeout( &mut self, timeout: Duration, ) -> Result<Vec<ConsumerRecord>>

Poll with a timeout.

Returns an empty Vec on timeout, or [KafkaError::ConnectionClosed] if the background consumer task has terminated.

Source

pub async fn try_poll(&mut self) -> Result<Vec<ConsumerRecord>>

Non-blocking poll: returns immediately with whatever is buffered.

Returns an empty Vec if no data is currently buffered. Does NOT indicate a closed connection — use poll to distinguish between “no data” and “consumer terminated”.

Source

pub async fn close(&self) -> Result<()>

Close the consumer, shutting down the background task.

Source

pub fn offsets(&self) -> &OffsetHandle

Access offset management handle.

Source

pub fn group(&self) -> &GroupHandle

Access group coordination handle.

Source

pub fn is_group(&self) -> bool

Whether this consumer is in group mode.

Source

pub fn into_stream(self) -> ConsumerStream

Convert this consumer into a streaming interface that yields individual ConsumerRecord values, one at a time.

The returned ConsumerStream shuts down the background task automatically when dropped.

§Examples
let mut stream = consumer.into_stream();
while let Some(record) = stream.recv().await {
    println!("Got offset: {}", record.offset);
}

Auto Trait Implementations§

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

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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