Spring Kafka KafkaTemplate.flush() 需要吗? [英] Spring Kafka KafkaTemplate.flush() required?

查看:213
本文介绍了Spring Kafka KafkaTemplate.flush() 需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用 Spring kafka,我使用 spring kafka 创建了生产者和消费者.我的 kafka 服务器在本地主机上运行,​​并创建了一个名为 test 的主题.我无法通过简单地调用

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);

在同一对象上调用 send 后,我不得不在 kafkaTemplate 上调用 flush() ,然后消费者才能接收数据.好的,它可以工作,而且很棒.但是有人可以向我解释幕后发生的事情吗?为什么需要调用flush方法.

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.

来自官方 spring kafka 文档.

From the official spring kafka documentation.

           public void flush()

刷新生产者.注意 如果 ProducerFactory 提供单例生产者(例如 DefaultKafkaProducerFactory),则调用此方法才有意义.

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

提前致谢.

推荐答案

producer 的实现是异步的.消息存储在内部队列中等待内部线程发送,这将通过潜在的批处理提高效率.

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.

因此,当程序退出时,消息可能会留在客户端的内存中.在这种情况下,Kafka 服务器实际上不会收到这些消息.

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

消息将在 queue.buffering.max.ms 或其他大小/数量限制定义的超时时间内发送.

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

flush 强制将发送队列中的所有消息传递到服务器.

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

这篇关于Spring Kafka KafkaTemplate.flush() 需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆