在 kafka 中发送同步消息? [英] Sending Synchronous messages in kafka?

查看:36
本文介绍了在 kafka 中发送同步消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在kafka中发送同步消息?
实现它的一种方法是设置属性参数
max.in.flight.requests.per.connection = 1.

How to send Synchronous messages in kafka?
One way of achieving it could be by setting the properties parameter
max.in.flight.requests.per.connection = 1.

但我想知道在 kafka 中是否有一种甚至直接或替代的方式发送同步消息.(类似于 producer.syncSend(...) 等).

But I want to know if there is an even direct or alternate way of sending Synchronous messages in kafka. (something like producer.syncSend(...) etc).

推荐答案

生产者 API 从 send 返回一个 Future.您可以调用 Future#get 进行阻塞,直到发送完成.

The producer API returns a Future from send. You can call Future#get to block until the sending has completed.

见这个示例来自Javadocs:

如果你想模拟一个简单的阻塞调用,你可以立即调用 get() 方法:

If you want to simulate a simple blocking call you can call the get() method immediately:

 byte[] key = "key".getBytes();
 byte[] value = "value".getBytes();
 ProducerRecord<byte[],byte[]> record = 
     new ProducerRecord<byte[],byte[]>("my-topic", key, value)
 producer.send(record).get();

这篇关于在 kafka 中发送同步消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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