I'm using Spring Boot 2.1.3 and am trying to configure Spring SeekToCurrentErrorHandler
with a DeadLetterPublishingRecoverer
to send error records to a different topic. The new DLT queue is created and a record is inserted but the message body is empty. I was expecting the message body to be populated with the original JSON body for future analysis.
Here is the configuration I have so far. Any idea where I'm going wrong? Not sure if its to do with using kafkaTemplate<Object, Object>
where as the message producer uses kafkaTemplate<String, Message>
.
@Configuration
@EnableKafka
public class ListenerConfig {
@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;
@Autowired
private KafkaTemplate<Object, Object> kafkaTemplate;
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer2.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer2.class);
props.put(ErrorHandlingDeserializer2.KEY_DESERIALIZER_CLASS, JsonDeserializer.class);
props.put(ErrorHandlingDeserializer2.VALUE_DESERIALIZER_CLASS, JsonDeserializer.class.getName());
props.put(JsonDeserializer.KEY_DEFAULT_TYPE, "java.lang.String");
props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, "com.test.kafka.Message");
props.put(JsonDeserializer.TRUSTED_PACKAGES, "com.test.kafka");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "json");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return props;
}
@Bean
public ConsumerFactory<String, Message> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(
consumerConfigs());
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Message> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Message> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setErrorHandler(new SeekToCurrentErrorHandler(new DeadLetterPublishingRecoverer(kafkaTemplate), 3));
return factory;
}
@KafkaListener(topics = "test")
public void receive(@Payload Message data,
@Headers MessageHeaders headers) {
LOG.info("received data='{}'", data);
System.out.println(data.getMessage());
headers.keySet().forEach(key -> {
LOG.info("{}: {}", key, headers.get(key));
});
}