Spring Kafka producer not work asynchronous

删除回忆录丶 提交于 2020-02-29 06:03:07

问题


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.


回答1:


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()



来源:https://stackoverflow.com/questions/60043006/spring-kafka-producer-not-work-asynchronous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!