Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

Retrieving all past messages from a apache pulsar topic

I think a simple example would describe my question better.

For example, let's say there is a topic named "A" and I have produced 100 messages(message1...message100). I have already consumed and acknowledged up to message 50 using subscription "A_1" with type exclusive. For some reason, my application shuts down, so when restarting the application, I need to read from message 1 again. Can this be achieved? I was thinking it would be possible to create a new subscription("A_2") and start reading messages again but i was unsure whether "A_2" would start reading from message1 or message51.... any directions or hints would be great!

Thanks in advance

question from:https://stackoverflow.com/questions/65921561/retrieving-all-past-messages-from-a-apache-pulsar-topic

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes this can be achieved, all that is required is to create a new subscription, e.g. "A_2", and use the subscriptionInitialPosition parameter to specify that you want to start consuming messages from the earliest available message as shown:

return getClient().newConsumer()
        .topic(topic)
        .subscriptionName("A_2") 
        .subscriptionType(SubscriptionType.Exclusive)
        .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
        .subscribe();

This assumes that the messages haven't been deleted due to message retention policies.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...