简体   繁体   English

如何确保邮件到达kafka经纪人?

[英]How to ensure messages reach kafka broker?

I have have a message producer on my local machine and a broker on remote host (aws). 我的本地计算机上有一个消息生成器,远程主机(aws)上有一个代理。

After sending a message from the producer, I wait and call the console consumer on the remote host and see excessive logs. 从生产者发送消息后,我等待并调用远程主机上的控制台使用者,并看到过多的日志。 Without the value from producer. 没有生产者的价值。

The producer flushes the data after calling the send method. 生产者在调用send方法后刷新数据。 Everything is configured correctly. 一切都配置正确。

How can I check to see that the broker received the message from the producer and to see if the producer received the answer? 如何检查代理是否收到了生产者的消息以及生产者是否收到了答复?

The Send method asynchronously sends the message to the topic and returns a Future of RecordMetadata . Send方法将消息异步发送到主题,并返回Future of RecordMetadata

java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<K,V> record)

Asynchronously sends a record to a topic 异步发送记录到主题

After the flush call, check to see that the Future has completed by calling the isDone method. flush调用后,通过调用isDone方法来检查Future是否已完成。 (for example, Future.isDone() == true ) (例如, Future.isDone() == true

Invoking this method makes all buffered records immediately available to send (even if linger.ms is greater than 0) and blocks on the completion of the requests associated with these records. 调用此方法将使所有缓冲的记录立即可用于发送(即使linger.ms大于0),并在与这些记录关联的请求完成时阻塞。 The post-condition of flush() is that any previously sent record will have completed (eg Future.isDone() == true). flush()的后置条件是任何先前发送的记录都将完成(例如Future.isDone()== true)。 A request is considered completed when it is successfully acknowledged according to the acks configuration you have specified or else it results in an error. 根据您指定的acks配置成功确认请求后,该请求即被视为完成,否则会导致错误。

The RecordMetadata contains the offset and the partition RecordMetadata包含offsetpartition

public int partition() public int partition()

The partition the record was sent to 记录发送到的分区

public long offset() 公共long offset()

the offset of the record, or -1 if {hasOffset()} returns false. 记录的偏移量;如果{hasOffset()}返回false,则返回-1。

Or you can also use Callback function to ensure messages was sent to topic or not 或者,您也可以使用回调功能来确保消息是否发送到主题

Fully non-blocking usage can make use of the Callback parameter to provide a callback that will be invoked when the request is complete. 完全无阻塞的用法可以利用Callback参数来提供一个回调,该回调将在请求完成时被调用。

here is clear example in docs 这是文档中的清晰示例

 ProducerRecord<byte[],byte[]> record = new ProducerRecord<byte[],byte[]>("the-topic", key, value);
 producer.send(myRecord,
           new Callback() {
               public void onCompletion(RecordMetadata metadata, Exception e) {
                   if(e != null) {
                      e.printStackTrace();
                   } else {
                      System.out.println("The offset of the record we just sent is: " + metadata.offset());
                   }
               }
           });

You can try get() API of send , which will return the Future of RecordMetadata 您可以尝试send的get()API,它将返回RecordMetadata的Future。

ProducerRecord<String, String> record = 
new ProducerRecord<>("SampleTopic", "SampleKey", "SampleValue");

try {
    producer.send(record).get(); 
} catch (Exception e) {
    e.printStackTrace(); 
}

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

相关问题 当经纪人破产时,卡夫卡制片人正在丢失消息 - Kafka producer is losing messages when broker is down 当我重新启动我的kafka经纪人时,为什么或如何丢失一些消息? - why or how we lose few messages when i restart my kafka broker? 当代理不可用时,如何忽略 Log4j2 Kafka appender 警告消息? - How can I ignore Log4j2 Kafka appender warning messages when broker is not available? 将消息写入代理后,Spark kafka 连接器抛出空指针异常 - Spark kafka connector throwing nullpointerexception after writing the messages into the broker 没有消费者连接时,Kafka经纪人可以保留消息吗? - Can a Kafka broker retain messages while there are no consumers connected? 当代理不可用时,消息不会出现在Spring Integration(Kafka)ErrorChannel中 - Messages DO NOT appear in the Spring Integration (Kafka) ErrorChannel when Broker is unavailable Java Spring Kafka 模板生产者在代理重启时丢失消息 - Java Spring Kafka Template producer lost messages on broker restart 如何以编程方式创建Kafka经纪人实例? - How to programmatically create Kafka broker instance? 如何构建高效的Kafka broker健康检查? - How to build efficient Kafka broker healthcheck? 如何在 kafka 代理上设置代理层? - How to setup proxy layer on kafka broker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM