问题
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
- Either reduce the max.block.ms but don't reduce it too much.
- 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