[improve][client] Close consumer when topic terminates.#13960
[improve][client] Close consumer when topic terminates.#13960Technoboy- wants to merge 2 commits intoapache:masterfrom
Conversation
f78d896 to
25e1b09
Compare
25e1b09
|
The pr had no activity for 30 days, mark with Stale label. |
BewareMyPower
left a comment
There was a problem hiding this comment.
I think whether it should be treated as an improvement needs a discussion, so I requested changes here.
The REACHED_END_OF_TOPIC could be received not only by topic termination. When a managed cursor doesn't have more entries, maybe temporarily, this request could also be sent by broker. Calling closeAsync here at this case might lead to frequent reconnections.
To correct my comment, But I still have some concerns about this behavior change. It means, when a topic is terminated, the subscribed consumers should all call the public default void reachedEndOfTopic(Consumer<T> consumer) {
consumer.closeAsync();
}which changes the original behavior. In addition, for programmers new to Pulsar, when they read the @RobertIndie @mattisonchao @Demogorgon314 @liudezhi2098 What's your opinions since I see you approved this PR? |
|
In addition, this also makes consumer not able to seek to an older position after topic terminates. try (PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build()) {
CountDownLatch latch = new CountDownLatch(1);
Consumer<byte[]> consumer = client.newConsumer()
.topic("my-topic")
.subscriptionName("sub")
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.messageListener(new MessageListener<byte[]>() {
@Override
public void received(Consumer<byte[]> consumer, Message<byte[]> msg) {
System.out.println("Received " + new String(msg.getValue()));
consumer.acknowledgeAsync(msg);
}
@Override
public void reachedEndOfTopic(Consumer<byte[]> consumer) {
System.out.println("consumer closed");
latch.countDown();
}
})
.subscribe();
latch.await();
try {
consumer.seek(MessageId.earliest);
System.out.println("seek done");
} catch (PulsarClientException e) {
e.printStackTrace();
}Run the application above, the run following commands. ./bin/pulsar-client produce -m hello my-topic
./bin/pulsar-admin topics terminate my-topic Before this patch, the output will be: After this patch, the output will be: It's only a case for |
yes, right. this is a case. even if the topic is terminated, the subscription can be created. |

Motivation
When the topic is terminated, and the consumer received 'end-of-topic' command, we should close the consumer.
Because entries in backlog is 0, and no more messages to delivery .
Documentation
no-need-doc