pub struct Consumer { /* private fields */ }Expand description
High-level consumer that manages offset tracking and auto-commit.
Wraps one or more partitions across subscribed topics, polling them round-robin and tracking the latest consumed offset per partition.
Implementations§
Source§impl Consumer
impl Consumer
Sourcepub async fn new(config: ConsumerConfig) -> Result<Self>
pub async fn new(config: ConsumerConfig) -> Result<Self>
Create and connect a new consumer.
Connects to the first available bootstrap server, authenticates if configured, and discovers partition assignments for subscribed topics.
§Auto-commit semantics
When auto_commit_interval is set, offsets are committed periodically
at the next-fetch position. This provides at-most-once semantics:
if the application crashes between poll() returning and the records
being processed, those records will be skipped on restart.
For at-least-once semantics, disable auto-commit and call
commit() explicitly after processing each batch.
Sourcepub fn set_rebalance_listener(&mut self, listener: Arc<dyn RebalanceListener>)
pub fn set_rebalance_listener(&mut self, listener: Arc<dyn RebalanceListener>)
Register a rebalance listener for partition revocation/assignment events.
The listener is invoked during discover_assignments():
on_partitions_revokedis called with the old assignment before reassignmenton_partitions_assignedis called with the new assignment after reassignment
Sourcepub async fn poll(&mut self) -> Result<Vec<ConsumerRecord>>
pub async fn poll(&mut self) -> Result<Vec<ConsumerRecord>>
Poll for new records across all assigned partitions.
Automatically reconnects with exponential backoff on connection errors and periodically re-discovers partition assignments.
Sourcepub async fn commit(&mut self) -> Result<()>
pub async fn commit(&mut self) -> Result<()>
Commit current offsets to the server.
Automatically reconnects on connection errors.
Sourcepub fn seek(&mut self, topic: impl Into<String>, partition: u32, offset: u64)
pub fn seek(&mut self, topic: impl Into<String>, partition: u32, offset: u64)
Seek a specific partition to a given offset.
The next poll() will fetch from this offset for the specified partition.
Sourcepub fn seek_to_beginning(&mut self, topic: &str)
pub fn seek_to_beginning(&mut self, topic: &str)
Seek all partitions of a topic to the beginning (offset 0).