Search code examples
javaspring-bootasynchronousapache-kafkakafka-producer-api

Spring Kafka producer not work asynchronous


I'm using a non Blocking (Async) sending message to Kafka using this :

    ListenableFuture<SendResult<Integer, String>> future = template.send(record);
    future.addCallback(new ListenableFutureCallback<SendResult<Integer, String>>() {

        @Override
        public void onSuccess(SendResult<Integer, String> result) {
            handleSuccess(data);
        }

        @Override
        public void onFailure(Throwable ex) {
            handleFailure(data, record, ex);
        }

    });

This work perfectly when the send action does its work.

But when there is a connection problem (server down for example), the result become non asynchronous and the method remains blocked until the end of the duration of max.block.ms.


Solution

  • This is natural in Async KAfka producer. You have two options

    1. Either reduce the max.block.ms but don't reduce it too much.
    2. You can wait for acks

    You can also create a callback function for onCompletion()