在kafka中发送同步消息? [英] Sending Synchronous messages in 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屋!