Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change way of working on DlqDeserializationExceptionHandler. #75

Merged
Changes from all commits
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 @@ -8,6 +8,7 @@
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.streams.errors.DeserializationExceptionHandler;
import org.apache.kafka.streams.processor.ProcessorContext;
import org.apache.kafka.common.KafkaException;

import java.util.Map;

Expand Down Expand Up @@ -41,18 +42,25 @@ public DeserializationHandlerResponse handle(ProcessorContext processorContext,
.setPartition(consumerRecord.partition())
.setTopic(consumerRecord.topic());

producer.send(new ProducerRecord<>(KafkaStreamsExecutionContext.getDlqTopicName(), consumerRecord.key(), builder.build())).get();
boolean isCausedByKafka = consumptionException.getCause() instanceof KafkaException;
//If the cause of this exception is a KafkaException and if getCause == sourceException (see Throwable.getCause - including SerializationException)
//use to handle poison pill => sent message into dlq and continue our life.
if(isCausedByKafka || consumptionException.getCause() == null) {
producer.send(new ProducerRecord<>(KafkaStreamsExecutionContext.getDlqTopicName(), consumerRecord.key(), builder.build())).get();
return DeserializationHandlerResponse.CONTINUE;
}
} catch (InterruptedException ie) {
log.error("Interruption while sending the deserialization exception {} for key {}, value {} and topic {} to DLQ topic {}", consumptionException,
consumerRecord.key(), consumerRecord.value(), consumerRecord.topic(), KafkaStreamsExecutionContext.getDlqTopicName(), ie);
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("Cannot send the deserialization exception {} for key {}, value {} and topic {} to DLQ topic {}", consumptionException,
consumerRecord.key(), consumerRecord.value(), consumerRecord.topic(), KafkaStreamsExecutionContext.getDlqTopicName(), e);
return DeserializationHandlerResponse.FAIL;
}

return DeserializationHandlerResponse.CONTINUE;
// here we only have exception like UnknownHostException for example or TimeoutException ...
// situation example: we cannot ask schema registry because the url is unavailable
return DeserializationHandlerResponse.FAIL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coudl the whole method be optimized for readability, i.e with a finally statement?

}

/**
Expand Down