Spring Kafka监听器无限循环出错

时间:2017-06-28 07:48:26

标签: spring apache-kafka

我将Spring Kafka监听器初始化为

UILabel

并用作

@Bean
public Map<String, Object> consumerConfig() {
    final HashMap<String, Object> result = new HashMap<>();
    result.put(BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    result.put(GROUP_ID_CONFIG, groupId);
    result.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    result.put(VALUE_DESERIALIZER_CLASS_CONFIG, MyKafkaJacksonRulesExecutionResultsDeserializer.class);
    return result;
}

@Bean
public ConsumerFactory<Long, MessageResult> consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(consumerConfig());
}

@Bean
public ConcurrentKafkaListenerContainerFactory<Long, MessageResult> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<Long, MessageResult> containerFactory = new ConcurrentKafkaListenerContainerFactory<>();
    containerFactory.setConsumerFactory(consumerFactory());
    containerFactory.setConcurrency(KAFKA_LISTENER_THREADS_COUNT);
    containerFactory.getContainerProperties().setPollTimeout(KAFKA_LISTENER_POLL_TIMEOUT);
    containerFactory.getContainerProperties().setAckOnError(true);
    containerFactory.getContainerProperties().setAckMode(RECORD);
    return containerFactory;
}
反序列化器在反序列化期间抛出异常会导致无限循环,因为侦听器无法获取messege。

如何让kafka监听器在出错时提交?

2 个答案:

答案 0 :(得分:2)

我为反序列化器创建了一个抛出异常的子类。然后我在我的配置中使用它作为反序列化器。然后,您的处理器必须处理空对象。

public class MyErrorHandlingDeserializer extends ExceptionThrowingDeserializer {

    @Override
    public Object deserialize(String topic, byte[] data) {
        try {
            return super.deserialize(topic, data);
        } catch (Exception e) {
            log.error("Problem deserializing data " + new String(data) + " on topic " + topic, e);
            return null;
        }
    }
}

答案 1 :(得分:0)

您可以将org.springframework.kafka.support.serializer.ErrorHandlingDeserializer2用作使用者配置中的值反序列化器。这使您有机会优雅地处理反序列化错误,并允许您提交使用者记录。使用情况和更多详细信息,请访问https://docs.spring.io/spring-kafka/reference/html/#error-handling-deserializer