spring-retry Retryble注释与retryTemplate

时间:2019-08-04 11:50:38

标签: java spring spring-boot spring-batch spring-retry

我试图理解spring-retry,我看到了两种方法,第一种是简单的

    @Retryable(value = {SomeException1.class,SomeException2.class}, maxAttempts = Constants.RETRY_VAL, backoff = @Backoff(value = Constants.RETRY_BACK))
    public String getData(){...}

和第二种RetryTemplate(通过RetryCallback / RecoveryCallback执行)。

我的理解是第二个可以提供更细粒度的控制。我们什么时候会采用第二种方法?会在我们拥有自己的Backoff和retryPolicies实现时吗?

此外,我找不到使用@Retryble注释设置自定义策略的示例或示例。

2 个答案:

答案 0 :(得分:0)

我认为,第二种方法RetryTemplate是首选。因为这种方法有助于我们在一个地方编写配置并在许多地方使用。

您还可以创建一个或多个RetryTemplate bean。每个$submissions = Submission::with('comments')->get()->find( $submission ); bean都用于一个异常映射。

答案 1 :(得分:0)

无论哪种方式,您都拥有平等的控制权。

要通过RetryTemplate使用自定义的Retryable,只需将重试拦截器作为Bean(带有自定义模板)连接起来,并在interceptor属性中提供它。

/**
 * Retry interceptor bean name to be applied for retryable method. Is mutually
 * exclusive with other attributes.
 * @return the retry interceptor bean name
 */
String interceptor() default "";

走哪条路只是一个偏好问题。