简体   繁体   English

Spring Kafka 生产者不能异步工作

[英]Spring Kafka producer not work asynchronous

I'm using a non Blocking (Async) sending message to Kafka using this :我正在使用非阻塞(异步)向 Kafka 发送消息:

    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.但是当出现连接问题(例如服务器宕机)时,结果变为非异步并且该方法保持阻塞状态,直到 max.block.ms 的持续时间结束。

This is natural in Async KAfka producer.这在 Async KAfka 生产者中很自然。 You have two options你有两个选择

  1. Either reduce the max.block.ms but don't reduce it too much.要么减少 max.block.ms 但不要减少太多。
  2. You can wait for acks你可以等待确认

You can also create a callback function for onCompletion()您还可以为 onCompletion() 创建回调函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM