kafka producer - call callback.onFailure in case of producer.send exception (like InterruptedException when buffer.memory and max.block.ms reached)

This commit is contained in:
Sergey Matvienko 2021-07-26 14:01:58 +03:00 committed by Andrew Shvayka
parent 2d4831af39
commit 2a4a9187d1

View File

@ -77,6 +77,7 @@ public class TbKafkaProducerTemplate<T extends TbQueueMsg> implements TbQueuePro
@Override @Override
public void send(TopicPartitionInfo tpi, T msg, TbQueueCallback callback) { public void send(TopicPartitionInfo tpi, T msg, TbQueueCallback callback) {
try {
createTopicIfNotExist(tpi); createTopicIfNotExist(tpi);
String key = msg.getKey().toString(); String key = msg.getKey().toString();
byte[] data = msg.getData(); byte[] data = msg.getData();
@ -96,6 +97,14 @@ public class TbKafkaProducerTemplate<T extends TbQueueMsg> implements TbQueuePro
} }
} }
}); });
} catch (Exception e) {
if (callback != null) {
callback.onFailure(e);
} else {
log.warn("Producer template failure (send method wrapper): {}", e.getMessage(), e);
}
throw e;
}
} }
private void createTopicIfNotExist(TopicPartitionInfo tpi) { private void createTopicIfNotExist(TopicPartitionInfo tpi) {