Package org.apache.pulsar.client.api
Interface Consumer
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
ConsumerV1Impl
An interface that abstracts behavior of Pulsar's consumer.
-
Method Summary
Modifier and TypeMethodDescriptionvoidacknowledge(org.apache.pulsar.client.api.Message<?> message) Acknowledge the consumption of a single message.voidacknowledge(org.apache.pulsar.client.api.MessageId messageId) Acknowledge the consumption of a single message, identified by its MessageId.acknowledgeAsync(org.apache.pulsar.client.api.Message<?> message) Asynchronously acknowledge the consumption of a single message.acknowledgeAsync(org.apache.pulsar.client.api.MessageId messageId) Asynchronously acknowledge the consumption of a single message.voidacknowledgeCumulative(org.apache.pulsar.client.api.Message<?> message) Acknowledge the reception of all the messages in the stream up to (and including) the provided message.voidacknowledgeCumulative(org.apache.pulsar.client.api.MessageId messageId) Acknowledge the reception of all the messages in the stream up to (and including) the provided message.acknowledgeCumulativeAsync(org.apache.pulsar.client.api.Message<?> message) Asynchronously Acknowledge the reception of all the messages in the stream up to (and including) the provided message.acknowledgeCumulativeAsync(org.apache.pulsar.client.api.MessageId messageId) Asynchronously Acknowledge the reception of all the messages in the stream up to (and including) the provided message.voidclose()Close the consumer and stop the broker to push more messages.Asynchronously close the consumer and stop the broker to push more messages.Get the name of consumer.org.apache.pulsar.client.api.ConsumerStatsgetStats()Get statistics for the consumer.Get a subscription for the consumer.getTopic()Get a topic for the consumer.booleanReturn true if the topic was terminated and this consumer has already consumed all the messages in the topic.booleanvoidpause()Stop requesting new messages from the broker untilresume()is called.org.apache.pulsar.client.api.Message<byte[]>receive()Receives a single message.org.apache.pulsar.client.api.Message<byte[]>Receive a single messageCompletableFuture<org.apache.pulsar.client.api.Message<byte[]>>Receive a single messagevoidRedelivers all the unacknowledged messages.voidresume()Resume requesting messages from the broker.voidseek(long timestamp) Reset the subscription associated with this consumer to a specific message publish time.voidseek(org.apache.pulsar.client.api.MessageId messageId) Reset the subscription associated with this consumer to a specific message id.seekAsync(long timestamp) Reset the subscription associated with this consumer to a specific message publish time.seekAsync(org.apache.pulsar.client.api.MessageId messageId) Reset the subscription associated with this consumer to a specific message id.voidUnsubscribe the consumerAsynchronously unsubscribe the consumer.
-
Method Details
-
getTopic
String getTopic()Get a topic for the consumer.- Returns:
- topic for the consumer
-
getSubscription
String getSubscription()Get a subscription for the consumer.- Returns:
- subscription for the consumer
-
unsubscribe
void unsubscribe() throws org.apache.pulsar.client.api.PulsarClientExceptionUnsubscribe the consumerThis call blocks until the consumer is unsubscribed.
- Throws:
org.apache.pulsar.client.api.PulsarClientException
-
unsubscribeAsync
CompletableFuture<Void> unsubscribeAsync()Asynchronously unsubscribe the consumer.- Returns:
CompletableFuturefor this operation
-
receive
org.apache.pulsar.client.api.Message<byte[]> receive() throws org.apache.pulsar.client.api.PulsarClientExceptionReceives a single message.This calls blocks until a message is available.
- Returns:
- the received message
- Throws:
org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the consumer was already closedorg.apache.pulsar.client.api.PulsarClientException.InvalidConfigurationException- if a message listener was defined in the configurationorg.apache.pulsar.client.api.PulsarClientException
-
receiveAsync
CompletableFuture<org.apache.pulsar.client.api.Message<byte[]>> receiveAsync()Receive a single messageRetrieves a message when it will be available and completes
CompletableFuturewith received message.receiveAsync()should be called subsequently once returnedCompletableFuturegets complete with received message. Else it creates backlog of receive requests in the application.- Returns:
CompletableFuture<Message> will be completed when message is available
-
receive
org.apache.pulsar.client.api.Message<byte[]> receive(int timeout, TimeUnit unit) throws org.apache.pulsar.client.api.PulsarClientException Receive a single messageRetrieves a message, waiting up to the specified wait time if necessary.
- Parameters:
timeout- 0 or less means immediate rather than infiniteunit-- Returns:
- the received
Messageor null if no message available before timeout - Throws:
org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the consumer was already closedorg.apache.pulsar.client.api.PulsarClientException.InvalidConfigurationException- if a message listener was defined in the configurationorg.apache.pulsar.client.api.PulsarClientException
-
acknowledge
void acknowledge(org.apache.pulsar.client.api.Message<?> message) throws org.apache.pulsar.client.api.PulsarClientException Acknowledge the consumption of a single message.- Parameters:
message- TheMessageto be acknowledged- Throws:
org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the consumer was already closedorg.apache.pulsar.client.api.PulsarClientException
-
acknowledge
void acknowledge(org.apache.pulsar.client.api.MessageId messageId) throws org.apache.pulsar.client.api.PulsarClientException Acknowledge the consumption of a single message, identified by its MessageId.- Parameters:
messageId- TheMessageIdto be acknowledged- Throws:
org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the consumer was already closedorg.apache.pulsar.client.api.PulsarClientException
-
acknowledgeCumulative
void acknowledgeCumulative(org.apache.pulsar.client.api.Message<?> message) throws org.apache.pulsar.client.api.PulsarClientException Acknowledge the reception of all the messages in the stream up to (and including) the provided message. This method will block until the acknowledge has been sent to the broker. After that, the messages will not be re-delivered to this consumer. Cumulative acknowledge cannot be used when the consumer type is set to ConsumerShared. It's equivalent to calling asyncAcknowledgeCumulative(Message) and waiting for the callback to be triggered.- Parameters:
message- TheMessageto be cumulatively acknowledged- Throws:
org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the consumer was already closedorg.apache.pulsar.client.api.PulsarClientException
-
acknowledgeCumulative
void acknowledgeCumulative(org.apache.pulsar.client.api.MessageId messageId) throws org.apache.pulsar.client.api.PulsarClientException Acknowledge the reception of all the messages in the stream up to (and including) the provided message. This method will block until the acknowledge has been sent to the broker. After that, the messages will not be re-delivered to this consumer. Cumulative acknowledge cannot be used when the consumer type is set to ConsumerShared. It's equivalent to calling asyncAcknowledgeCumulative(MessageId) and waiting for the callback to be triggered.- Parameters:
messageId- TheMessageIdto be cumulatively acknowledged- Throws:
org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException- if the consumer was already closedorg.apache.pulsar.client.api.PulsarClientException
-
acknowledgeAsync
Asynchronously acknowledge the consumption of a single message.- Parameters:
message- TheMessageto be acknowledged- Returns:
- a future that can be used to track the completion of the operation
-
acknowledgeAsync
Asynchronously acknowledge the consumption of a single message.- Parameters:
messageId- TheMessageIdto be acknowledged- Returns:
- a future that can be used to track the completion of the operation
-
acknowledgeCumulativeAsync
Asynchronously Acknowledge the reception of all the messages in the stream up to (and including) the provided message. Cumulative acknowledge cannot be used when the consumer type is set to ConsumerShared.- Parameters:
message- TheMessageto be cumulatively acknowledged- Returns:
- a future that can be used to track the completion of the operation
-
acknowledgeCumulativeAsync
CompletableFuture<Void> acknowledgeCumulativeAsync(org.apache.pulsar.client.api.MessageId messageId) Asynchronously Acknowledge the reception of all the messages in the stream up to (and including) the provided message. Cumulative acknowledge cannot be used when the consumer type is set to ConsumerShared.- Parameters:
messageId- TheMessageIdto be cumulatively acknowledged- Returns:
- a future that can be used to track the completion of the operation
-
getStats
org.apache.pulsar.client.api.ConsumerStats getStats()Get statistics for the consumer.- numMsgsReceived : Number of messages received in the current interval
- numBytesReceived : Number of bytes received in the current interval
- numReceiveFailed : Number of messages failed to receive in the current interval
- numAcksSent : Number of acks sent in the current interval
- numAcksFailed : Number of acks failed to send in the current interval
- totalMsgsReceived : Total number of messages received
- totalBytesReceived : Total number of bytes received
- totalReceiveFailed : Total number of messages failed to receive
- totalAcksSent : Total number of acks sent
- totalAcksFailed : Total number of acks failed to sent
- Returns:
- statistic for the consumer
-
close
void close() throws org.apache.pulsar.client.api.PulsarClientExceptionClose the consumer and stop the broker to push more messages.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
org.apache.pulsar.client.api.PulsarClientException
-
closeAsync
CompletableFuture<Void> closeAsync()Asynchronously close the consumer and stop the broker to push more messages.- Returns:
- a future that can be used to track the completion of the operation
-
hasReachedEndOfTopic
boolean hasReachedEndOfTopic()Return true if the topic was terminated and this consumer has already consumed all the messages in the topic. Please note that this does not simply mean that the consumer is caught up with the last message published by producers, rather the topic needs to be explicitly "terminated". -
redeliverUnacknowledgedMessages
void redeliverUnacknowledgedMessages()Redelivers all the unacknowledged messages. In Failover mode, the request is ignored if the consumer is not active for the given topic. In Shared mode, the consumers messages to be redelivered are distributed across all the connected consumers. This is a non blocking call and doesn't throw an exception. In case the connection breaks, the messages are redelivered after reconnect. -
seek
void seek(org.apache.pulsar.client.api.MessageId messageId) throws org.apache.pulsar.client.api.PulsarClientException Reset the subscription associated with this consumer to a specific message id.The message id can either be a specific message or represent the first or last messages in the topic.
MessageId.earliest: Reset the subscription on the earliest message available in the topicMessageId.latest: Reset the subscription on the latest message in the topic
- Parameters:
messageId- the message id where to reposition the subscription- Throws:
org.apache.pulsar.client.api.PulsarClientException
-
seek
void seek(long timestamp) throws org.apache.pulsar.client.api.PulsarClientException Reset the subscription associated with this consumer to a specific message publish time. Note: this operation can only be done on non-partitioned topics. For these, one can rather perform the seek() on the individual partitions.- Parameters:
timestamp- the message publish time where to reposition the subscription- Throws:
org.apache.pulsar.client.api.PulsarClientException
-
seekAsync
Reset the subscription associated with this consumer to a specific message id.The message id can either be a specific message or represent the first or last messages in the topic.
MessageId.earliest: Reset the subscription on the earliest message available in the topicMessageId.latest: Reset the subscription on the latest message in the topic
- Parameters:
messageId- the message id where to reposition the subscription- Returns:
- a future to track the completion of the seek operation
-
seekAsync
Reset the subscription associated with this consumer to a specific message publish time. Note: this operation can only be done on non-partitioned topics. For these, one can rather perform the seek() on the individual partitions.- Parameters:
timestamp- the message publish time where to reposition the subscription- Returns:
- a future to track the completion of the seek operation
-
isConnected
boolean isConnected()- Returns:
- Whether the consumer is connected to the broker
-
getConsumerName
String getConsumerName()Get the name of consumer.- Returns:
- consumer name.
-
pause
void pause() -
resume
void resume()Resume requesting messages from the broker.
-