简体   繁体   English

需要Spring Kafka KafkaTemplate.flush()吗?

[英]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.

相关问题 在 spring 引导 Kafka 中创建通用 KafkaTemplate - Creating generic KafkaTemplate in spring boot Kafka SpringBoot/spring-kafka 应用程序中的 Autowired KafkaTemplate 抛出空指针 - Autowired KafkaTemplate in SpringBoot/spring-kafka application throws null pointer spring kafka 所需的配置 - Required configs for spring kafka 春天kafkatemplate相关ID - spring kafkatemplate correlation id Spring Kafka-将KafkaTemplate与Producer Listener配置并使用Listenable Future注册回调之间的区别 - Spring Kafka-Difference between configuring KafkaTemplate with Producer Listener and registering a callback with Listenable Future 如何在 Java Spring 中使用没有 @KafkaListener 的 KafkaTemplate 使用来自 Kafka 的消息? RabbitMQ 模拟 - How I can consume message form Kafka using KafkaTemplate without @KafkaListener in Java Spring? RabbitMQ analogue Spring 用于 Apache Kafka:KafkaTemplate 行为与异步请求、批处理和最大 In-flight 为 1 - Spring for Apache Kafka: KafkaTemplate Behavior with Async Requests, Batching, and Max In-flight of 1 Spring Kafka:kafkaTemplate executeInTransaction 方法如何发挥 Consumer 的 read_committed 隔离级别 - Spring Kafka: How does kafkaTemplate executeInTransaction method play with Consumer's read_committed isolation level KafkaReplyTemplate 抛出:需要 KafkaTemplate 来支持回复? - KafkaReplyTemplate throws: a KafkaTemplate is required to support replies? spring boot中创建KafkaTemplate的正确方法 - The correct way for creation of KafkaTemplate in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM