简体   繁体   English

卡夫卡-制片人-处理“发送失败”

[英]Kafka - producer - handle “failed to send”

I'm running a 0.8 Kafka, and build a producer using the provided Java API. 我正在运行0.8 Kafka,并使用提供的Java API构建生产者。
The API functions of sending a message (or messages) return void. 发送消息(或多个消息)的API函数返回void。

Is there a way to get the status of the sent message? 有没有办法获取已发送消息的状态? If it sent or failed? 如果发送或失败?

This is extremely important to us since we are reading the messages from a file and we want to delete the file after all messages were sent. 这对我们来说非常重要,因为我们正在从文件中读取消息,并且在发送完所有消息后希望删除该文件。 But if there were errors and some messages weren't sent and I delete the file it will cause a loss of a very important data. 但是,如果出现错误并且未发送某些消息,而我删除了文件,则将导致非常重要的数据丢失。

You can configure your producer to wait until it gets n acks from the Kafka cluster (request.required.acks) so that you have some kind of guarantee that the data has been committed properly before deleting your source file. 您可以将生产者配置为等到它从Kafka群集(request.required.acks)收到通知后,才能保证在删除源文件之前已正确提交了数据。

If really you need to be sure that the message sent succeeded, you might want to consider the alternative of making the producer to be synchronous (producer.type=sync). 如果确实需要确保成功发送了消息,则可能需要考虑使生产者成为同步的替代方法(producer.type = sync)。 This way, you would be able to catch any exception thrown by the blocking invocation and act accordingly. 这样,您将能够捕获阻塞调用引发的任何异常并采取相应的措施。 The exception thrown by send() is kafka.common.FailedToSendMessageException. send()抛出的异常是kafka.common.FailedToSendMessageException。

Kafka's Java API is not ideal, hope this helps you. Kafka的Java API并不理想,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM