KafkaProducer.close()和KafkaProducer.flush()之间的区别

时间:2019-05-08 14:32:29

标签: apache-kafka kafka-producer-api

看文档,我不确定我是否了解使用close()flush()之间的区别。

这是flush()的文档

 * Invoking this method makes all buffered records immediately available to send (even if <code>linger.ms</code> is
 * greater than 0) and blocks on the completion of the requests associated with these records. The post-condition
 * of <code>flush()</code> is that any previously sent record will have completed (e.g. <code>Future.isDone() == true</code>).
 * A request is considered completed when it is successfully acknowledged
 * according to the <code>acks</code> configuration you have specified or else it results in an error.

还有close()的文档:

 * This method waits up to <code>timeout</code> for the producer to complete the sending of all incomplete requests.
 * If the producer is unable to complete all requests before the timeout expires, this method will fail
 * any unsent and unacknowledged records immediately.

这是否意味着:

  1. 如果我使用close()并且内存中的缓冲区中有一些记录正在等待处理,它们甚至都不会被尝试(相比于刷新,它将尝试发送它们)?
  2. 如果我使用flush(),如果重试次数很大,它可能会“永远”阻塞?在使用close()时,我有一个上限要花多长时间?

我想如果我对1.正确,那么acks = 0的制作人将获得一条记录的确认,如果记录“不幸”到足以放置在in-中,甚至可能不会尝试发布该记录。内存队列,并在调用close()之后立即进行。

1 个答案:

答案 0 :(得分:0)

如果要继续使用生产者并等待发送消息,则可以使用flush else close。使用超时值关闭将等待按照配置发送和确认消息,直到超时值,然后关闭生产者