newur commented on pull request #10982:
URL: https://github.com/apache/pulsar/pull/10982#issuecomment-865903329
@BewareMyPower in my opinion this is a very different and more advanced use
case that supports your claim. I read the current doc in the spirit of 'Look,
here is a basic example how you can fetch messages one by one'.
But the example has the major drawback that it traps the app (main thread)
so it can never do anything else than receiving messages. This is a limitation
that goes far beyond what I would call reasonable. The user will most likely
want its app to be not trapped (like I would like my
SpringBoot/Quarkus/whatever app to finish the normal startup).
So the listener approach is similar complex to read/write to current one,
without the trap thingy.
```
final List<String> messages = new ArrayList<>();
Consumer<String> consumer = client.newConsumer(Schema.STRING)
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.subscriptionName("my-sub")
.topic("my-topic")
.subscribe();
for (int i = 0; i < 10; i++) {
// NOTE: If you're using receive() with timeout, you must
check null for timeout
messages.add(consumer.receive().getValue());
}
messages.forEach(System.out::println);
consumer.close();
```
If I read that right this `receive` call is blocking, so either I get a
timeout or the app is stuck till the message arrives. Again I would never
want/expect that behavior my app. Plus, this use case is on a totally different
level of complexity, compared to what is currently done.
Maybe you can outline a bit more when the current `while` loop would be
desirable. I totally fail to see any use case where I would prefer to have my
main thread blocked while consuming messages one by one.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]