java - 带有注释的 Spring Retry 框架不起作用

标签 java spring-retry

我使用 SimpleRetryPolicy/RetryTemplate 尝试了带有简单 java 类的 spring 重试框架。它运作良好。所以我想对 Annotations 做同样的事情。注释对我来说从来没有用过。也没有在网上找到太多帮助。以下代码就像正常的 Java 程序一样工作,在第一次尝试时抛出异常,这不是预期的行为。在抛出异常或从异常中恢复之前,它应该至少尝试了 5 次。不知道我哪里出错了。我需要做任何 XML/spring AOP 配置才能工作吗?会怎样

import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;

@EnableRetry
@Configuration
public class App {  
    public static void main(String[] args) throws Exception {
        Parentservice p = new  SpringRetryWithHystrixService();
        String status = p.getStatus();
        System.out.println(status);
    }
}

import org.springframework.retry.annotation.Retryable;
public interface Parentservice {
    @Retryable(maxAttempts=5)
    String getStatus() throws Exception;
}


import org.springframework.retry.annotation.Recover;
public class SpringRetryWithHystrixService implements Parentservice{

    public static int i=0;

    public String getStatus() throws Exception{
        System.out.println("Attempt:"+i++);
        throw new NullPointerException();
    }

        @Recover
        public static String getRecoveryStatus(){
            return "Recovered from NullPointer Exception";
        }
    }

我在接口(interface)和实现的顶部尝试了 @Retryable(maxAttempts=5),但没有任何区别。

最佳答案

您的应用程序必须由 Spring 管理,您不能只使用 new ...

Parentservice p = new  SpringRetryWithHystrixService();

这是一个 Spring Boot 应用...

@SpringBootApplication
@EnableRetry
public class So40308025Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So40308025Application.class, args);
        System.out.println(context.getBean(SpringRetryWithHystrixService.class).getStatus());
        context.close();
    }

}


@Component
public class SpringRetryWithHystrixService {

    public static int i = 0;

    @Retryable(maxAttempts = 5)
    public String getStatus() throws Exception {
        System.out.println("Attempt:" + i++);
        throw new NullPointerException();
    }

    @Recover
    public static String getRecoveryStatus() {
        return "Recovered from NullPointer Exception";
    }

}

结果:

Attempt:0
Attempt:1
Attempt:2
Attempt:3
Attempt:4
Recovered from NullPointer Exception

如果出于某种原因不想使用 Spring Boot,请使用

@Configuration
@EnableRetry
public class So40308025Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(So40308025Application.class);
        System.out.println(context.getBean(SpringRetryWithHystrixService.class).getStatus());
        context.close();
    }

    @Bean
    public SpringRetryWithHystrixService service() {
        return new SpringRetryWithHystrixService();
    }

}

关于java - 带有注释的 Spring Retry 框架不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308025/

相关文章:

Java native 库 System.loadLibrary 因 UnsatisfiedLinkError 而失败

java - 在 Boggle.java 游戏中部署启发式修剪搜索空间

java - 验证具有不同格式的日期列表

java - 无法禁用 ORMLites 日志记录

spring-retry - @Retryable 是否可以与 JDK 动态代理一起使用?

java - 通过 SQL Developer 将 Java 类添加到 Oracle 数据库 - 编译错误

java - 使用重试模板执行自定义重试策略

java - Spring 重试异常 block 的问题

java - boolean 标志上的 Spring 重试策略