BewareMyPower edited a comment on pull request #10982:
URL: https://github.com/apache/pulsar/pull/10982#issuecomment-865920717


   IMO, in short, the `MessageListener` should be the advanced API that's used 
for efficiency if users prefer asynchronous API. But the `receive` method 
should be the easiest API that's suitable for basic example.
   
   > 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. 
   
   Not completely right. If there're already messages that are cached in the 
consumer, the `receive` method will return immediately. It looks like you 
strongly dislike the synchronous `receive` API that may block the current 
thread. However, let's look at Kafka's API design: 
https://kafka.apache.org/28/javadoc/org/apache/kafka/clients/consumer/Consumer.html#poll(java.time.Duration)
   
   It provides the synchronous API (`poll()` method) directly. What's different 
from Pulsar's `receive()` API is that Kafka's `poll()` returns a container of 
messages while Pulsar's `receive()` returns a single message. However, Pulsar's 
API design just avoids collecting the messages into a container. If there're 
some messages that are cached, the client will only fetch a single message from 
the cache and pass it to user side. Therefore, in essential they're similar.
   
   - Kafka: fetch N messages (block main thread for some time), collect to a 
list and return the list to the user.
   - Pulsar: fetch N messages (block main thread for some time), users peek 
messages one by one.
   
   > I totally fail to see any use case where I would prefer to have my main 
thread blocked while consuming messages one by one.
   
   Maybe it's right to you. But lots of users uses Kafka's synchronous `poll()` 
API at the same time. You can also see the example in Kafka's official website: 
https://kafka.apache.org/28/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html
   
   ```java
        while (true) { // infinite loop
            ConsumerRecords<String, String> records = 
consumer.poll(Duration.ofMillis(100)); // block main thread for some time
            for (ConsumerRecord<String, String> record : records) // peek 
messages one by one
                System.out.printf("offset = %d, key = %s, value = %s%n", 
record.offset(), record.key(), record.value());
        }
   ```
   
   Maybe some Kafka client's wrapper like SpringBoot will provide a listener 
like API that has some overhead. In this case, Pulsar provides the 
`MessageListener` that can avoid users from wrapping a listener in another 
thread again.
   
   
   


-- 
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]


Reply via email to