【问题标题】:@Recover is not working for custom exception@Recover 不适用于自定义异常
【发布时间】:2021-11-10 16:09:04
【问题描述】:

以下重试工作正常。在 3 次尝试失败后调用恢复方法

@Retryable(value = MyException.class)
public String getSomething(String performanceId) {
    String s = "test";
     ...
     throw new MyException("test", new RuntimeException());
     ...
    return s;
 }

@Recover
public String recover(MyException exception, String performanceId) {
     log.info("In recover method!!!!!!!!!!!!!!!!!!");
     return "test";
}

但是当我更改返回类型时,将不会调用以下恢复方法。你能告诉我为什么以及如何解决这个问题吗?非常感谢!

@Retryable(value = MyException.class)
public MyOwnType getSomething(String performanceId) {
    MyOwnType s = new MyOwnType();
     ...
     throw new MyException("test", new RuntimeException());
     ...
    return s;
 }

@Recover
public MyOwnType recover(MyException exception, String performanceId) {
     log.info("In recover method!!!!!!!!!!!!!!!!!!");
     return new MyOwnType();
}

public class MyOwnType implements Serializable {
...
}

编辑: 我忘了提到我也在抛出一个自定义异常

public class MyException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public MyException(String message, Throwable cause) {
        super(message, cause);
    }

}

【问题讨论】:

    标签: java spring spring-retry


    【解决方案1】:

    这对我来说很好;你确定你有@EnableRetry

    @SpringBootApplication
    @EnableRetry
    public class So69916559Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So69916559Application.class, args);
        }
    
    
        @Bean
        ApplicationRunner runner(Test test) {
            return args -> {
                System.out.println(test.getSomething("foo"));
            };
        }
    
    }
    
    @Component
    class Test {
    
        private static final Logger log = LoggerFactory.getLogger(Test.class);
    
        @Retryable(value = RuntimeException.class)
        public MyOwnType getSomething(String performanceId) {
            MyOwnType s = new MyOwnType();
            log.info("here");
            throw new RuntimeException();
        }
    
        @Recover
        public MyOwnType recover(RuntimeException exception, String performanceId) {
            log.info("In recover method!!!!!!!!!!!!!!!!!!");
            return new MyOwnType();
        }
    
    }
    
    class MyOwnType implements Serializable {
    
    }
    

    结果

    here
    here
    here
    In recover method!!!!!!!!!!!!!!!!!!
    com.example.demo.MyOwnType@3f725306
    

    【讨论】:

    • 感谢您的回复!是的,我有所有必要的注释。否则,第一种情况是行不通的。我忘了提到我正在抛出可能包含不同异常类型的自定义异常。我怀疑这可能是原因?
    【解决方案2】:

    通过将恢复方法更改为解决问题

    @Recover
    public MyOwnType recover(RuntimeException exception, String performanceId) {
         log.info("In recover method!!!!!!!!!!!!!!!!!!");
         return new MyOwnType();
    }
    

    即使 MyException 扩展了 RuntimeException。如果期待 MyException,recover 方法将不会被调用。不知道为什么

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      相关资源
      最近更新 更多