Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

don't crash the app on nonfatal failure #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ public class AsynchronousDeliveryStrategy implements DeliveryStrategy {
public <K, V, E> boolean send(Producer<K, V> producer, ProducerRecord<K, V> record, final E event,
final FailedDeliveryCallback<E> failedDeliveryCallback) {
try {
producer.send(record, new Callback() {
@Override
public void onCompletion(RecordMetadata metadata, Exception exception) {
if (exception != null) {
failedDeliveryCallback.onFailedDelivery(event, exception);
}
producer.send(record, (metadata, exception) -> {
if (exception != null) {
failedDeliveryCallback.onFailedDelivery(event, exception);
}
});
return true;
} catch (BufferExhaustedException | TimeoutException e) {
} catch (Exception e) {
cleverchuk marked this conversation as resolved.
Show resolved Hide resolved
failedDeliveryCallback.onFailedDelivery(event, e);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.KafkaException;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.TimeoutException;
import org.junit.Test;
Expand Down Expand Up @@ -74,4 +75,16 @@ public void testCallbackWillTriggerOnFailedDeliveryOnProducerSendTimeout() {
verify(failedDeliveryCallback).onFailedDelivery(eq("msg"), same(exception));
}

@Test
public void testCallbackWillTriggerOnFailedDeliveryOnAnyError() {
final Exception exception = new KafkaException("miau");
final ProducerRecord<String,String> record = new ProducerRecord<String,String>("topic", 0, null, "msg");

when(producer.send(same(record), any(Callback.class))).thenThrow(exception);

unit.send(producer, record, "msg", failedDeliveryCallback);

verify(failedDeliveryCallback).onFailedDelivery(eq("msg"), same(exception));
}

}