[英]Spring Kafka KafkaTemplate.flush() required?
I am using Spring kafka for the first time and I have created a Producer and Consumer by using spring kafka. 我是第一次使用Spring kafka,并且已经通过使用spring kafka创建了生产者和消费者。 I have my kafka server running on localhost and have created a topic called test.
我在本地主机上运行我的kafka服务器,并创建了一个名为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. 在同一个对象上调用send之后,我必须在kafkaTemplate上调用flush(),然后使用者能够接收数据。 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.
为什么需要调用flush方法。
From the official spring kafka documentation. 摘自Spring Kafka官方文档。
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).
注意仅当ProducerFactory提供单个生产者(例如DefaultKafkaProducerFactory)时,才调用此方法才有意义。
Thank you in advance. 先感谢您。
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.
在这种情况下,Kafka服务器实际上不会收到这些消息。
message would be sent in timeout defined by queue.buffering.max.ms
, or other size / number limit. 消息将在
queue.buffering.max.ms
或其他大小/数量限制所定义的超时时间内发送。
flush
force all message in send queue to be delivered to server. flush
强制将发送队列中的所有邮件传递到服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.