com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy Maven / Gradle / Ivy
package com.github.danielwegener.logback.kafka.delivery;
import org.apache.kafka.clients.producer.BufferExhaustedException;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.errors.TimeoutException;
/**
* @since 0.0.1
*/
public class AsynchronousDeliveryStrategy implements DeliveryStrategy {
@Override
public boolean send(Producer producer, ProducerRecord record, final E event,
final FailedDeliveryCallback failedDeliveryCallback) {
try {
producer.send(record, new Callback() {
@Override
public void onCompletion(RecordMetadata metadata, Exception exception) {
if (exception != null) {
failedDeliveryCallback.onFailedDelivery(event, exception);
}
}
});
return true;
} catch (BufferExhaustedException | TimeoutException e) {
failedDeliveryCallback.onFailedDelivery(event, e);
return false;
}
}
}