Package org.apache.pulsar.client.api.v5
Interface StreamConsumer<T>
- Type Parameters:
T- the type of message values
- All Superinterfaces:
AutoCloseable,Closeable
A consumer for streaming (ordered) consumption with broker-managed position tracking.
Messages are delivered in order (per-key if keyed). Acknowledgment is cumulative only: acknowledging a message ID means all messages up to and including that ID are acknowledged.
This interface provides synchronous (blocking) operations. For non-blocking
usage, obtain an AsyncStreamConsumer via async().
This maps to the Exclusive/Failover subscription model in the Pulsar v4 API.
-
Method Summary
Modifier and TypeMethodDescriptionvoidacknowledgeCumulative(MessageId messageId) Acknowledge all messages up to and including the given message ID.voidacknowledgeCumulative(MessageId messageId, Transaction txn) Acknowledge within a transaction.async()Return the asynchronous view of this consumer.voidclose()Close the consumer and release all resources.The consumer name (system-assigned or user-specified).receive()Receive a single message, blocking indefinitely.Receive a single message, blocking up to the given timeout.receiveMulti(int maxNumMessages, Duration timeout) Receive a batch of messages, blocking up to the given timeout.The subscription name.topic()The topic this consumer is subscribed to.
-
Method Details
-
topic
String topic()The topic this consumer is subscribed to.- Returns:
- the fully qualified topic name
-
subscription
String subscription()The subscription name.- Returns:
- the subscription name
-
consumerName
String consumerName()The consumer name (system-assigned or user-specified).- Returns:
- the consumer name, never
null
-
receive
Receive a single message, blocking indefinitely.- Returns:
- the received
Message - Throws:
PulsarClientException- if the consumer is closed or a connection error occurs
-
receive
Receive a single message, blocking up to the given timeout. Returnsnullif the timeout elapses without a message.- Parameters:
timeout- the maximum time to wait for a message- Returns:
- the received
Message, ornullif the timeout elapses - Throws:
PulsarClientException- if the consumer is closed or a connection error occurs
-
receiveMulti
Receive a batch of messages, blocking up to the given timeout.- Parameters:
maxNumMessages- the maximum number of messages to returntimeout- the maximum time to wait for messages- Returns:
- the received
Messagesbatch - Throws:
PulsarClientException- if the consumer is closed or a connection error occurs
-
acknowledgeCumulative
Acknowledge all messages up to and including the given message ID.- Parameters:
messageId- the ID of the message to acknowledge cumulatively
-
acknowledgeCumulative
Acknowledge within a transaction. The acknowledgment becomes effective when the transaction is committed.- Parameters:
messageId- the ID of the message to acknowledge cumulativelytxn- the transaction to associate the acknowledgment with
-
async
AsyncStreamConsumer<T> async()Return the asynchronous view of this consumer.- Returns:
- the
AsyncStreamConsumercounterpart of this consumer
-
close
Close the consumer and release all resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
PulsarClientException- if an error occurs while closing the consumer
-