【问题标题】:Spring Retry @Recover passing parametersSpring Retry @Recover 传参
【发布时间】:2018-03-25 15:35:41
【问题描述】:

我找不到任何有关我需要采取的行动的信息。我将@Retryable 注释与@Recover 处理程序方法一起使用。像这样的:

    @Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 10000))
    public void update(Integer id)
    {
        execute(id);
    }

    @Recover
    public void recover(Exception ex)
    {
        logger.error("Error when updating object with id {}", id);
    }

问题是我不知道如何将我的参数“id”传递给recover()方法。有任何想法吗?提前致谢。

【问题讨论】:

    标签: java spring recover spring-retry


    【解决方案1】:

    根据Spring Retry documentation,只需对齐@Retryable@Recover方法之间的参数即可:

    恢复方法的参数可以选择包括 抛出的异常,以及可选的参数传递给 原始可重试方法(或它们的部分列表,只要 没有省略)。示例:

    @Service
    class Service {
        @Retryable(RemoteAccessException.class)
        public void service(String str1, String str2) {
            // ... do something
        }
        @Recover
        public void recover(RemoteAccessException e, String str1, String str2) {
           // ... error handling making use of original args if required
        }
    }
    

    所以你可以写:

    @Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 10000))
    public void update(Integer id) {
        execute(id);
    }
    
    @Recover
    public void recover(Exception ex, Integer id){
        logger.error("Error when updating object with id {}", id);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 2018-12-29
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2019-03-27
      相关资源
      最近更新 更多