首页 > 解决方案 > 整数类型的值在 DeadLetterPublishingRecoverer 上出现错误

问题描述

我试过这段代码,在这里:

@Bean
    public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
            ConcurrentKafkaListenerContainerFactoryConfigurer configure,
            ConsumerFactory<Object, Object> kafkaConsumerFactory,
            KafkaTemplate<Object, Object> template) {
        ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
        configure.configure(factory, kafkaConsumerFactory);
        factory.setErrorHandler(new SeekToCurrentErrorHandler(new DeadLetterPublishingRecoverer(template),3));
        return factory;
    }

这部分的 int

factory.setErrorHandler(new SeekToCurrentErrorHandler(new DeadLetterPublishingRecoverer(template),3));

不允许使用数字 3,因为它需要 BackOff 而不是 int 类型。

标签: javaspring-bootapache-kafkaspring-kafka

解决方案


该构造函数在 2.3.x 中已弃用,并在 2.5.x 中删除。

请参阅文档:https ://docs.spring.io/spring-kafka/docs/current/reference/html/#seek-to-current

以前,配置是“maxFailures”(包括第一次交付尝试)。使用 FixedBackOff 时,它的 maxAttempts 属性表示传递重试次数(比旧的 maxFailures 属性少一)。此外,maxFailures=-1这意味着使用旧配置无限期重试,BackOff您可以将 maxAttempts 设置为 Long.MAX_VALUE 对于 aFixedBackOff并将 maxElapsedTime 保留为 a 中的默认值ExponentialBackOff

现在的等价物是

factory.setErrorHandler(new SeekToCurrentErrorHandler(new DeadLetterPublishingRecoverer(template), new FixedBackOff(0L, 2));

(不延迟 3 次交付尝试)。


推荐阅读