Search code examples
spring-kafka

Infinite retries in Spring Kafka with 2.8.* version


@Bean
fun commonErrorHandler(
    kafkaListenerEndpointRegistry: KafkaListenerEndpointRegistry
) = DefaultErrorHandler(
    ExponentialBackOff(2000, 1.1).apply {
        maxInterval = 60 * 1000 // 1 minutes
    } // 2000, 2000 * 1.1, 2000 * 1.1 * 1.1 ...
).apply {
    listOf(
        DeserializationException::class.java,
        MessageConversionException::class.java,
        ConversionException::class.java,
        MethodArgumentResolutionException::class.java,
        NoSuchMethodException::class.java,
        ClassCastException::class.java,
    ).forEach {
        removeNotRetryableException(it)
    }

    setBackOffFunction { t, e ->
        logger.error(e) { "Fail processed topic: ${t.topic()}, record_key: ${t.key()}" }

        return@setBackOffFunction null
    }
}

Now I have made such a decision after my question Backoff none exhausted for "topic"

I want to achieve the following goals: infinitive retries, add a logger.

Is this a good solution?


Solution

  • You don't need to remove each default not retryable exceptions; that code is brittle because the framework might add more in the future, just call defaultfalse() instead.

    /**
     * By default, unmatched types classify as true. Call this method to make the default
     * false, and remove types explicitly classified as false. This should be called before
     * calling any of the classification modification methods.
     * @since 2.8.4
     */
    public void defaultFalse() {