@InterfaceAudience.Public @InterfaceStability.Stable public interface Consumer<T> extends Closeable
All the operations on the consumer instance are thread safe.
| Modifier and Type | Method and Description |
|---|---|
void |
acknowledge(List<MessageId> messageIdList)
Acknowledge the consumption of a list of message.
|
void |
acknowledge(Message<?> message)
Acknowledge the consumption of a single message.
|
void |
acknowledge(MessageId messageId)
Acknowledge the consumption of a single message, identified by its
MessageId. |
void |
acknowledge(Messages<?> messages)
Acknowledge the consumption of
Messages. |
CompletableFuture<Void> |
acknowledgeAsync(List<MessageId> messageIdList)
Asynchronously acknowledge the consumption of a list of message.
|
CompletableFuture<Void> |
acknowledgeAsync(Message<?> message)
Asynchronously acknowledge the consumption of a single message.
|
CompletableFuture<Void> |
acknowledgeAsync(MessageId messageId)
Asynchronously acknowledge the consumption of a single message.
|
CompletableFuture<Void> |
acknowledgeAsync(MessageId messageId,
Transaction txn)
Asynchronously acknowledge the consumption of a single message, it will store in pending ack.
|
CompletableFuture<Void> |
acknowledgeAsync(Messages<?> messages)
Asynchronously acknowledge the consumption of
Messages. |
void |
acknowledgeCumulative(Message<?> message)
Acknowledge the reception of all the messages in the stream up to (and including) the provided message.
|
void |
acknowledgeCumulative(MessageId messageId)
Acknowledge the reception of all the messages in the stream up to (and including) the provided message.
|
CompletableFuture<Void> |
acknowledgeCumulativeAsync(Message<?> message)
Asynchronously Acknowledge the reception of all the messages in the stream up to (and including) the provided
message.
|
CompletableFuture<Void> |
acknowledgeCumulativeAsync(MessageId messageId)
Asynchronously Acknowledge the reception of all the messages in the stream up to (and including) the provided
message.
|
CompletableFuture<Void> |
acknowledgeCumulativeAsync(MessageId messageId,
Transaction txn)
Acknowledge the reception of all the messages in the stream up to (and including) the provided message with this
transaction, it will store in transaction pending ack.
|
Messages<T> |
batchReceive()
Batch receiving messages.
|
CompletableFuture<Messages<T>> |
batchReceiveAsync()
Batch receiving messages.
|
void |
close()
Close the consumer and stop the broker to push more messages.
|
CompletableFuture<Void> |
closeAsync()
Asynchronously close the consumer and stop the broker to push more messages.
|
String |
getConsumerName()
Get the name of consumer.
|
long |
getLastDisconnectedTimestamp() |
MessageId |
getLastMessageId()
Get the last message id available for consume.
|
CompletableFuture<MessageId> |
getLastMessageIdAsync()
Get the last message id available for consume.
|
ConsumerStats |
getStats()
Get statistics for the consumer.
|
String |
getSubscription()
Get a subscription for the consumer.
|
String |
getTopic()
Get a topic for the consumer.
|
boolean |
hasReachedEndOfTopic()
Return true if the topic was terminated and this consumer has already consumed all the messages in the topic.
|
boolean |
isConnected() |
void |
negativeAcknowledge(Message<?> message)
Acknowledge the failure to process a single message.
|
void |
negativeAcknowledge(MessageId messageId)
Acknowledge the failure to process a single message.
|
void |
negativeAcknowledge(Messages<?> messages)
Acknowledge the failure to process
Messages. |
void |
pause()
Stop requesting new messages from the broker until
resume() is called. |
Message<T> |
receive()
Receives a single message.
|
Message<T> |
receive(int timeout,
TimeUnit unit)
Receive a single message.
|
CompletableFuture<Message<T>> |
receiveAsync()
Receive a single message
|
void |
reconsumeLater(Message<?> message,
long delayTime,
TimeUnit unit)
reconsumeLater the consumption of
Messages. |
void |
reconsumeLater(Messages<?> messages,
long delayTime,
TimeUnit unit)
reconsumeLater the consumption of
Messages. |
CompletableFuture<Void> |
reconsumeLaterAsync(Message<?> message,
long delayTime,
TimeUnit unit)
Asynchronously reconsumeLater the consumption of a single message.
|
CompletableFuture<Void> |
reconsumeLaterAsync(Messages<?> messages,
long delayTime,
TimeUnit unit)
Asynchronously reconsumeLater the consumption of
Messages. |
void |
reconsumeLaterCumulative(Message<?> message,
long delayTime,
TimeUnit unit)
reconsumeLater the reception of all the messages in the stream up to (and including) the provided message.
|
CompletableFuture<Void> |
reconsumeLaterCumulativeAsync(Message<?> message,
long delayTime,
TimeUnit unit)
Asynchronously ReconsumeLater the reception of all the messages in the stream up to (and including) the provided
message.
|
void |
redeliverUnacknowledgedMessages()
Redelivers all the unacknowledged messages.
|
void |
resume()
Resume requesting messages from the broker.
|
void |
seek(Function<String,Object> function)
Reset the subscription associated with this consumer to a specific message ID or message publish time.
|
void |
seek(long timestamp)
Reset the subscription associated with this consumer to a specific message publish time.
|
void |
seek(MessageId messageId)
Reset the subscription associated with this consumer to a specific message id.
|
CompletableFuture<Void> |
seekAsync(Function<String,Object> function)
Reset the subscription associated with this consumer to a specific message ID
or message publish time asynchronously.
|
CompletableFuture<Void> |
seekAsync(long timestamp)
Reset the subscription associated with this consumer to a specific message publish time.
|
CompletableFuture<Void> |
seekAsync(MessageId messageId)
Reset the subscription associated with this consumer to a specific message id.
|
void |
unsubscribe()
Unsubscribe the consumer.
|
CompletableFuture<Void> |
unsubscribeAsync()
Asynchronously unsubscribe the consumer.
|
String getTopic()
String getSubscription()
void unsubscribe()
throws PulsarClientException
This call blocks until the consumer is unsubscribed.
Unsubscribing will the subscription to be deleted and all the data retained can potentially be deleted as well.
The operation will fail when performed on a shared subscription where multiple consumers are currently connected.
PulsarClientException - if the operation failsCompletableFuture<Void> unsubscribeAsync()
CompletableFuture to track the operationunsubscribe()Message<T> receive() throws PulsarClientException
This calls blocks until a message is available.
PulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientException.InvalidConfigurationException - if a message listener was defined in the configurationPulsarClientExceptionCompletableFuture<Message<T>> receiveAsync()
Retrieves a message when it will be available and completes CompletableFuture with received message.
receiveAsync() should be called subsequently once returned CompletableFuture gets complete
with received message. Else it creates backlog of receive requests in the application.
The returned future can be cancelled before completion by calling .cancel(false)
(CompletableFuture.cancel(boolean)) to remove it from the the backlog of receive requests. Another
choice for ensuring a proper clean up of the returned future is to use the CompletableFuture.orTimeout method
which is available on JDK9+. That would remove it from the backlog of receive requests if receiving exceeds
the timeout.
CompletableFuture<Message> will be completed when message is availableMessage<T> receive(int timeout, TimeUnit unit) throws PulsarClientException
Retrieves a message, waiting up to the specified wait time if necessary.
timeout - 0 or less means immediate rather than infiniteunit - Message or null if no message available before timeoutPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientException.InvalidConfigurationException - if a message listener was defined in the configurationPulsarClientExceptionMessages<T> batchReceive() throws PulsarClientException
This calls blocks until has enough messages or wait timeout, more details to see BatchReceivePolicy.
PulsarClientExceptionCompletableFuture<Messages<T>> batchReceiveAsync()
Retrieves messages when has enough messages or wait timeout and
completes CompletableFuture with received messages.
batchReceiveAsync() should be called subsequently once returned CompletableFuture gets complete
with received messages. Else it creates backlog of receive requests in the application.
The returned future can be cancelled before completion by calling .cancel(false)
(CompletableFuture.cancel(boolean)) to remove it from the the backlog of receive requests. Another
choice for ensuring a proper clean up of the returned future is to use the CompletableFuture.orTimeout method
which is available on JDK9+. That would remove it from the backlog of receive requests if receiving exceeds
the timeout.
PulsarClientExceptionvoid acknowledge(Message<?> message) throws PulsarClientException
message - The Message to be acknowledgedPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionvoid acknowledge(MessageId messageId) throws PulsarClientException
MessageId.messageId - The MessageId to be acknowledgedPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionvoid acknowledge(Messages<?> messages) throws PulsarClientException
Messages.messages - messagesPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionvoid acknowledge(List<MessageId> messageIdList) throws PulsarClientException
messageIdList - PulsarClientExceptionvoid negativeAcknowledge(Message<?> message)
When a message is "negatively acked" it will be marked for redelivery after
some fixed delay. The delay is configurable when constructing the consumer
with ConsumerBuilder.negativeAckRedeliveryDelay(long, TimeUnit).
This call is not blocking.
Example of usage:
while (true) {
Message<String> msg = consumer.receive();
try {
// Process message...
consumer.acknowledge(msg);
} catch (Throwable t) {
log.warn("Failed to process message");
consumer.negativeAcknowledge(msg);
}
}
message - The Message to be acknowledgedvoid negativeAcknowledge(MessageId messageId)
When a message is "negatively acked" it will be marked for redelivery after
some fixed delay. The delay is configurable when constructing the consumer
with ConsumerBuilder.negativeAckRedeliveryDelay(long, TimeUnit).
This call is not blocking.
This variation allows to pass a MessageId rather than a Message
object, in order to avoid keeping the payload in memory for extended amount
of time
messageId - The MessageId to be acknowledgednegativeAcknowledge(Message)void negativeAcknowledge(Messages<?> messages)
Messages.
When messages is "negatively acked" it will be marked for redelivery after
some fixed delay. The delay is configurable when constructing the consumer
with ConsumerBuilder.negativeAckRedeliveryDelay(long, TimeUnit).
This call is not blocking.
Example of usage:
while (true) {
Messages<String> msgs = consumer.batchReceive();
try {
// Process message...
consumer.acknowledge(msgs);
} catch (Throwable t) {
log.warn("Failed to process message");
consumer.negativeAcknowledge(msgs);
}
}
messages - The Message to be acknowledgedvoid reconsumeLater(Message<?> message, long delayTime, TimeUnit unit) throws PulsarClientException
Messages.
When a message is "reconsumeLater" it will be marked for redelivery after some custom delay.
Example of usage:
while (true) {
Message<String> msg = consumer.receive();
try {
// Process message...
consumer.acknowledge(msg);
} catch (Throwable t) {
log.warn("Failed to process message");
consumer.reconsumeLater(msg, 1000 , TimeUnit.MILLISECONDS);
}
}
message - the Message to be reconsumeLaterdelayTime - the amount of delay before the message will be deliveredunit - the time unit for the delayPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionvoid reconsumeLater(Messages<?> messages, long delayTime, TimeUnit unit) throws PulsarClientException
Messages.messages - the messages to be reconsumeLaterdelayTime - the amount of delay before the message will be deliveredunit - the time unit for the delayPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionvoid acknowledgeCumulative(Message<?> message) throws PulsarClientException
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.
message - The Message to be cumulatively acknowledgedPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionvoid acknowledgeCumulative(MessageId messageId) throws PulsarClientException
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.
messageId - The MessageId to be cumulatively acknowledgedPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionCompletableFuture<Void> acknowledgeCumulativeAsync(MessageId messageId, Transaction txn)
After the transaction commit, the end of previous transaction acked message until this transaction acked message will actually ack.
After the transaction abort, the end of previous transaction acked message until this transaction acked message will be redelivered to this consumer.
Cumulative acknowledge with transaction only support cumulative ack and now have not support individual and cumulative ack sharing.
If cumulative ack with a transaction success, we can cumulative ack messageId with the same transaction more than previous messageId.
It will not be allowed to cumulative ack with a transaction different from the previous one when the previous transaction haven't commit or abort.
Cumulative acknowledge cannot be used when the consumer type is set to ConsumerShared.
messageId - The MessageId to be cumulatively acknowledgedtxn - Transaction the transaction to cumulative ackCompletableFuture the future of the ack resultPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientException.TransactionConflictException - if the ack with messageId is less than the messageId in pending ack state or ack with transaction is
different from the transaction in pending ack.PulsarClientException.NotAllowedException - broker don't support transactionvoid reconsumeLaterCumulative(Message<?> message, long delayTime, TimeUnit unit) throws PulsarClientException
message - The message to be cumulatively reconsumeLaterdelayTime - the amount of delay before the message will be deliveredunit - the time unit for the delayPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionCompletableFuture<Void> acknowledgeAsync(Message<?> message)
message - The Message to be acknowledgedCompletableFuture<Void> acknowledgeAsync(MessageId messageId)
messageId - The MessageId to be acknowledgedCompletableFuture<Void> acknowledgeAsync(MessageId messageId, Transaction txn)
After the transaction commit, the message will actually ack.
After the transaction abort, the message will be redelivered.
messageId - MessageId to be individual acknowledgedtxn - Transaction the transaction to cumulative ackCompletableFuture the future of the ack resultPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientException.TransactionConflictException - if the ack with messageId has been acked by another transactionPulsarClientException.NotAllowedException - broker don't support transaction
don't find batch size in consumer pending ackCompletableFuture<Void> acknowledgeAsync(Messages<?> messages)
Messages.messages - The Messages to be acknowledgedCompletableFuture<Void> acknowledgeAsync(List<MessageId> messageIdList)
messageIdList - CompletableFuture<Void> reconsumeLaterAsync(Message<?> message, long delayTime, TimeUnit unit)
message - The Message to be reconsumeLaterdelayTime - the amount of delay before the message will be deliveredunit - the time unit for the delayCompletableFuture<Void> reconsumeLaterAsync(Messages<?> messages, long delayTime, TimeUnit unit)
Messages.messages - The Messages to be reconsumeLaterdelayTime - the amount of delay before the message will be deliveredunit - the time unit for the delayCompletableFuture<Void> acknowledgeCumulativeAsync(Message<?> message)
Cumulative acknowledge cannot be used when the consumer type is set to ConsumerShared.
message - The Message to be cumulatively acknowledgedCompletableFuture<Void> acknowledgeCumulativeAsync(MessageId messageId)
Cumulative acknowledge cannot be used when the consumer type is set to ConsumerShared.
messageId - The MessageId to be cumulatively acknowledgedCompletableFuture<Void> reconsumeLaterCumulativeAsync(Message<?> message, long delayTime, TimeUnit unit)
Cumulative reconsumeLater cannot be used when the consumer type is set to ConsumerShared.
message - The message to be cumulatively reconsumeLaterdelayTime - the amount of delay before the message will be deliveredunit - the time unit for the delayConsumerStats getStats()
void close()
throws PulsarClientException
close in interface AutoCloseableclose in interface CloseablePulsarClientExceptionCompletableFuture<Void> closeAsync()
boolean hasReachedEndOfTopic()
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".
void redeliverUnacknowledgedMessages()
void seek(MessageId messageId) throws PulsarClientException
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 topic
MessageId.latest : Reset the subscription on the latest message in the topic
Note: this operation can only be done on non-partitioned topics. For these, one can rather perform the seek() on the individual partitions.
messageId - the message id where to reposition the subscriptionPulsarClientExceptionvoid seek(long timestamp) throws PulsarClientException
timestamp - the message publish time where to reposition the subscriptionPulsarClientExceptionvoid seek(Function<String,Object> function) throws PulsarClientException
The Function input is topic+partition. It returns only timestamp or MessageId.
The return value is the seek position/timestamp of the current partition. Exception is thrown if other object types are returned.
If returns null, the current partition will not do any processing. Exception in a partition may affect other partitions.
function - PulsarClientExceptionCompletableFuture<Void> seekAsync(Function<String,Object> function)
The Function input is topic+partition. It returns only timestamp or MessageId.
The return value is the seek position/timestamp of the current partition. Exception is thrown if other object types are returned.
If returns null, the current partition will not do any processing. Exception in a partition may affect other partitions.
function - CompletableFuture<Void> seekAsync(MessageId messageId)
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 topic
MessageId.latest : Reset the subscription on the latest message in the topic
Note: this operation can only be done on non-partitioned topics. For these, one can rather perform the seek() on the individual partitions.
messageId - the message id where to reposition the subscriptionCompletableFuture<Void> seekAsync(long timestamp)
timestamp - the message publish time where to reposition the subscriptionMessageId getLastMessageId() throws PulsarClientException
PulsarClientExceptionCompletableFuture<MessageId> getLastMessageIdAsync()
boolean isConnected()
String getConsumerName()
void pause()
void resume()
long getLastDisconnectedTimestamp()
Copyright © 2017–2021 Apache Software Foundation. All rights reserved.