Spring Kafka KafkaTemplate.flush() required?

泄露秘密 提交于 2020-04-16 03:33:28

问题


I am using Spring kafka for the first time and I have created a Producer and Consumer by using spring kafka. I have my kafka server running on localhost and have created a topic called test. I was not able to send messages to the consumer by simply calling

        KafkaTemplate.send(topicName,Data);

I had to call flush() on kafkaTemplate after calling send on the same object and then the consumer was able to receive the data. Okay it works and it is fantastic. But could anyone explain to me what is happening behind the scenes? Why is the flush method required to be called.

From the official spring kafka documentation.

           public void flush()

Flush the producer. Note It only makes sense to invoke this method if the ProducerFactory serves up a singleton producer (such as the DefaultKafkaProducerFactory).

Thank you in advance.


回答1:


the implement of producer is async. Message is stored in an internal queue to wait to send by inner thread, which would improve efficiency with potential batching.

So, messages may stay in client's memory when program exit. In this case, Kafka server don't actually receive these messages.

message would be sent in timeout defined by queue.buffering.max.ms, or other size / number limit.

flush force all message in send queue to be delivered to server.



来源:https://stackoverflow.com/questions/48144997/spring-kafka-kafkatemplate-flush-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!