As Thilo suggested, you can call Future#get
to block until the sending has completed. However you might have some performance issue, since the producer starts sending when the producer queue has batch.size
elements, when the buffer of size buffer.memory
is full or after max.block.ms
milliseconds.
If you have a limited number of threads pushing to kafka, you will have to wait max.block.ms
each time for your message to be sent. So in some cases, you will prefer using :
// send message to producer queue
Future<RecordMetadata> future = producer.send(new ProducerRecord<>(topic, key, message));
// flush producer queue to spare queuing time
producer.flush();
// throw error when kafka is unreachable
future.get(10, TimeUnit.SECONDS);