public interface Consumer<T> extends Closeable
All the operations on the consumer instance are thread safe.
| 限定符和类型 | 方法和说明 |
|---|---|
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. |
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
|
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.
|
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.
|
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 |
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
Retrieves a message, waiting up to the specified wait time if necessary.
|
CompletableFuture<Message<T>> |
receiveAsync()
Receive a single message
Retrieves a message when it will be available and completes
CompletableFuture with received message. |
void |
redeliverUnacknowledgedMessages()
Redelivers all the unacknowledged messages.
|
void |
resume()
Resume requesting messages from the broker.
|
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(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
This call blocks until the consumer is unsubscribed.
|
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.
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 configurationPulsarClientExceptionvoid 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 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 acknowledgeCumulative(Message<?> message) throws PulsarClientException
message - The Message to be cumulatively acknowledgedPulsarClientException.AlreadyClosedException - if the consumer was already closedPulsarClientExceptionvoid acknowledgeCumulative(MessageId messageId) throws PulsarClientException
messageId - The MessageId to be cumulatively acknowledgedPulsarClientException.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> acknowledgeCumulativeAsync(Message<?> message)
message - The Message to be cumulatively acknowledgedCompletableFuture<Void> acknowledgeCumulativeAsync(MessageId messageId)
messageId - The MessageId to be cumulatively acknowledgedConsumerStats getStats()
void close()
throws PulsarClientException
close 在接口中 AutoCloseableclose 在接口中 CloseablePulsarClientExceptionCompletableFuture<Void> closeAsync()
boolean hasReachedEndOfTopic()
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
messageId - the message id where to reposition the subscriptionPulsarClientExceptionvoid seek(long timestamp) throws PulsarClientException
timestamp - the message publish time where to reposition the subscriptionPulsarClientExceptionCompletableFuture<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
messageId - the message id where to reposition the subscriptionCompletableFuture<Void> seekAsync(long timestamp)
timestamp - the message publish time where to reposition the subscriptionboolean isConnected()
String getConsumerName()
void pause()
void resume()
Copyright © 2017–2019 Apache Software Foundation. All rights reserved.