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
.
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).
The producer API returns a Future
from send
. You can call Future#get
to block until the sending has completed.
See this example from the Javadocs:
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();