Combining Blocking and Non-Blocking Retries

从 2.8.4 开始,您可以配置该框架以同时使用阻塞和非阻塞重试。例如,您可能有一组异常,这些异常也可能在下一条记录中触发错误,例如 DatabaseAccessException,因此您可以在将同一记录发送到重试主题之前或直接发送到 DLT 之前重试该记录几次。

Starting in 2.8.4 you can configure the framework to use both blocking and non-blocking retries in conjunction. For example, you can have a set of exceptions that would likely trigger errors on the next records as well, such as DatabaseAccessException, so you can retry the same record a few times before sending it to the retry topic, or straight to the DLT.

若要配置阻止重试,请在扩展 RetryTopicConfigurationSupport@Configuration 类中覆盖 configureBlockingRetries 方法,并添加要重试的异常以及要使用的 BackOff。默认 BackOff 是不延迟、尝试 9 次的 FixedBackOff。有关更多信息,请参阅 Configuring Global Settings and Features

To configure blocking retries, override the configureBlockingRetries method in a @Configuration class that extends RetryTopicConfigurationSupport and add the exceptions you want to retry, along with the BackOff to be used. The default BackOff is a FixedBackOff with no delay and 9 attempts. See Configuring Global Settings and Features for more information.

@Override
protected void configureBlockingRetries(BlockingRetriesConfigurer blockingRetries) {
    blockingRetries
            .retryOn(MyBlockingRetryException.class, MyOtherBlockingRetryException.class)
            .backOff(new FixedBackOff(3_000, 5));
}

结合全局可重试主题的致命异常分类,可以将框架配置为你想要的任何行为,例如让某些异常触发阻止和非阻止重试、仅触发一种或另一种,或在不进行任何类型的重试的情况下直接转到 DLT。

In combination with the global retryable topic’s fatal exceptions classification, you can configure the framework for any behavior you’d like, such as having some exceptions trigger both blocking and non-blocking retries, trigger only one kind or the other, or go straight to the DLT without retries of any kind.

下面是一个同时使用两种配置的示例:

Here’s an example with both configurations working together:

@Override
protected void configureBlockingRetries(BlockingRetriesConfigurer blockingRetries) {
    blockingRetries
            .retryOn(ShouldRetryOnlyBlockingException.class, ShouldRetryViaBothException.class)
            .backOff(new FixedBackOff(50, 3));
}

@Override
protected void manageNonBlockingFatalExceptions(List<Class<? extends Throwable>> nonBlockingFatalExceptions) {
    nonBlockingFatalExceptions.add(ShouldSkipBothRetriesException.class);
}

在此示例中:

In this example:

  • ShouldRetryOnlyBlockingException.class would retry only via blocking and, if all retries fail, would go straight to the DLT.

  • ShouldRetryViaBothException.class would retry via blocking, and if all blocking retries fail would be forwarded to the next retry topic for another set of attempts.

  • ShouldSkipBothRetriesException.class would never be retried in any way and would go straight to the DLT if the first processing attempt failed.

请注意,阻止重试行为是允许列表——你以这种方式添加你确实想要重试的异常;而非阻止重试分类面向的是致命异常,因此是拒绝列表——你添加你不希望进行非阻止重试、而是希望直接发送到 DLT 的异常。

Note that the blocking retries behavior is allowlist - you add the exceptions you do want to retry that way; while the non-blocking retries classification is geared towards FATAL exceptions and as such is denylist - you add the exceptions you don’t want to do non-blocking retries, but to send directly to the DLT instead.

非阻止异常分类行为还取决于特定主题的配置。

The non-blocking exception classification behavior also depends on the specific topic’s configuration.