Background: when consume message failed and publish to DLQ, I would like to add the custom header , the header key is exception and value is the exception
this is my configuration
spring:
cloud:
function:
definition: numberConsumer
stream:
bindings:
numberProducer-out-0:
destination: first-topic
numberConsumer-in-0:
group: group
destination: first-topic
kafka:
bindings:
numberConsumer-in-0:
consumer:
standard-headers:
enableDlq: true
dlqName: dlq
dlq-partitions: 1
this is my consumer
@Bean
public Consumer<String> numberConsumer() {
return message -> log.info("receive message : {}", message);
}
I try to use this way but not work,not sure what's wrong with my code?
@Configuration
@Slf4j
public class KafkaConfiguration {
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<String, String>> customizer(DefaultErrorHandler errorHandler) {
return (container, dest, group) -> {
container.setCommonErrorHandler(errorHandler);
};
}
@Bean
public DefaultErrorHandler errorHandler(DeadLetterPublishingRecoverer deadLetterPublishingRecoverer) {
return new DefaultErrorHandler(deadLetterPublishingRecoverer);
}
@Bean
public DeadLetterPublishingRecoverer publisher(KafkaOperations template) {
DeadLetterPublishingRecoverer recover = new DeadLetterPublishingRecoverer(template);
recover.setExceptionHeadersCreator((kafkaHeaders, exception, isKey, headerNames) -> {
var exceptionType = getRootCauseExceptionType(exception);
kafkaHeaders.add("exception-type", exceptionType.getBytes());
});
return recover;
}
I use the spring-cloud-stream-kafka version 4.0.1