Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Spring Kafka to use DeadLetterPublishingRecoverer

Tags:

spring-kafka

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));
        });
    }
like image 240
Swordfish Avatar asked Oct 17 '22 06:10

Swordfish


1 Answers

The DeadLetterPublishingRecoverer simply publishes the incoming ConsumerRecord contents.

When the ErrorHandlingDeserializer2 detects a deserialization exception, there is no value() field in the ConsumerRecord (because it couldn't be deserialized).

Instead, the failure is put into one of two headers: ErrorHandlingDeserializer2.VALUE_DESERIALIZER_EXCEPTION_HEADER or ErrorHandlingDeserializer2.KEY_DESERIALIZER_EXCEPTION_HEADER.

You can obtain the details with

Header header = record.headers().lastHeader(headerName);
DeserializationException ex = (DeserializationException) new ObjectInputStream(
    new ByteArrayInputStream(header.value())).readObject();

with the original payload in ex.getData().

We should probably enhance the recoverer to do this when it detects such a header is present and the value() is null.

I opened a new feature issue.

like image 157
Gary Russell Avatar answered Oct 21 '22 09:10

Gary Russell