public interface Reader<T> extends Closeable
限定符和类型 | 方法和说明 |
---|---|
CompletableFuture<Void> |
closeAsync()
Asynchronously close the reader and stop the broker to push more messages
|
String |
getTopic() |
boolean |
hasMessageAvailable()
Check if there is any message available to read from the current position.
|
CompletableFuture<Boolean> |
hasMessageAvailableAsync()
Asynchronously check if there is any message available to read from the current position.
|
boolean |
hasReachedEndOfTopic()
Return true if the topic was terminated and this reader has reached the end of the topic.
|
boolean |
isConnected() |
Message<T> |
readNext()
Read the next message in the topic.
|
Message<T> |
readNext(int timeout,
TimeUnit unit)
Read the next message in the topic waiting for a maximum time.
|
CompletableFuture<Message<T>> |
readNextAsync()
Read asynchronously the next message in the topic.
|
void |
seek(long timestamp)
Reset the subscription associated with this reader to a specific message publish time.
|
void |
seek(MessageId messageId)
Reset the subscription associated with this reader to a specific message id.
|
CompletableFuture<Void> |
seekAsync(long timestamp)
Reset the subscription associated with this reader to a specific message publish time.
|
CompletableFuture<Void> |
seekAsync(MessageId messageId)
Reset the subscription associated with this reader to a specific message id.
|
String getTopic()
Message<T> readNext() throws PulsarClientException
This method will block until a message is available.
PulsarClientException
Message<T> readNext(int timeout, TimeUnit unit) throws PulsarClientException
Returns null if no message is received before the timeout.
PulsarClientException
CompletableFuture<Message<T>> readNextAsync()
PulsarClientException
if the reader
is already closed.CompletableFuture<Void> closeAsync()
boolean hasReachedEndOfTopic()
Note that this only applies to a "terminated" topic (where the topic is "sealed" and no
more messages can be published) and not just that the reader is simply caught up with
the publishers. Use hasMessageAvailable()
to check for for that.
boolean hasMessageAvailable() throws PulsarClientException
This check can be used by an application to scan through a topic and stop when the reader reaches the current last published message. For example:
while (reader.hasMessageAvailable()) { Message<String> msg = reader.readNext(); // Do something } // Done readingNote that this call might be blocking (see #hasMessageAvailableAsync() for async version) and that even if this call returns true, that will not guarantee that a subsequent call to {@link #readNext()} will not block.
PulsarClientException
- if there was any error in the operationCompletableFuture<Boolean> hasMessageAvailableAsync()
This check can be used by an application to scan through a topic and stop when the reader reaches the current last published message.
PulsarClientException
if there was any error in the operationboolean isConnected()
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 reader on the earliest message available in the topic
MessageId.latest
: Reset the reader on the latest message in the topic
messageId
- the message id where to reposition the readerPulsarClientException
void seek(long timestamp) throws PulsarClientException
timestamp
- the message publish time where to reposition the readerPulsarClientException
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 reader on the earliest message available in the topic
MessageId.latest
: Reset the reader on the latest message in the topic
messageId
- the message id where to position the readerCompletableFuture<Void> seekAsync(long timestamp)
timestamp
- the message publish time where to position the readerCopyright © 2017–2019 Apache Software Foundation. All rights reserved.