Spring @Retryable无法正常工作

时间:2017-01-11 10:08:16

标签: java spring spring-boot

我已经阅读了很多关于这个主题的内容,但不知何故无法让我的代码工作。我有一个非常简单的项目,我正在尝试实现spring-retry过程。这是我的主要应用程序类:

@EnableRetry
@SpringBootApplication
@ComponentScan(basePackages = "com.support")
public class RetryApplication {

    public static void main(String[] args) {
        SpringApplication.run(RetryApplication.class, args);
    }
}

我的支持课程:

@Component
public class Support {

    @Retryable
    @PostConstruct
    public void mySupport() throws Exception {
        System.out.println("Attempt...");
        throw new IndexOutOfBoundsException();
    }

    @Recover
    public void myRecovery(){
        System.out.println("Recovering...");
    }

}

我的build.gradle文件的一部分:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.springframework.retry:spring-retry')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

当我执行我的项目时,@Retryable将无法管理异常,您可以通过以下输出看到:

2017-01-11 11:04:43.361  INFO 3029 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@aecb35a: startup date [Wed Jan 11 11:04:43 CET 2017]; root of context hierarchy
Attempt...
2017-01-11 11:04:44.028  WARN 3029 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'support': Invocation of init method failed; nested exception is java.lang.IndexOutOfBoundsException
2017-01-11 11:04:44.033  INFO 3029 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-01-11 11:04:44.039 ERROR 3029 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'support': Invocation of init method failed; nested exception is java.lang.IndexOutOfBoundsException
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1581) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at com.retry.RetryApplication.main(RetryApplication.java:15) [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: java.lang.IndexOutOfBoundsException: null
    at com.support.Support.mySupport(Support.java:21) ~[main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    ... 22 common frames omitted
Process finished with exit code 1

有人可以帮忙吗?我不明白我做错了什么......谢谢!

[编辑] 我添加了一个配置类并更改了一些我的代码,但结果是一样的。这是我的配置类:

@EnableRetry
@Configuration
public class MyConfiguration {

    @Bean
    public MyRetry myBean() throws Exception {
        new MyRetry().myMethod();
        return new MyRetry();
    }
}

和MyRetry类:

public class MyRetry {

    @Retryable
    public void myMethod() throws Exception {
        System.out.println("Attempt...");
        throw new Exception();
    }

    @Recover
    public void myRecover(){
        System.out.println("Recovering...");
    }
}

和主要的应用程序类:

@SpringBootApplication
@ComponentScan("com.retry")
public class RetryApplication {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(RetryApplication.class, args);
    }
}

1 个答案:

答案 0 :(得分:1)

我使用了不同的方法 - 春天map.getView().setCenter([53.44241609, 6.84913974]); - 并找到了一个有效的解决方案。这是我的主要应用程序类:

RetryTemplate

包含重试方法的类:

@SpringBootApplication
public class RetryApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(RetryApplication.class, args);
    }
}

配置类:

public class MyRetry {
    @Autowired
    private RetryTemplate retryTemplate;
    public void myMethod() throws Exception {
        retryTemplate.execute(
            new RetryCallback<Void, Exception>() {
                @Override
                public Void doWithRetry(RetryContext context) throws Exception {
                    System.out.println("Attempt...");
                    throw new Exception();
                }
            },
            new RecoveryCallback<Void>() {
                @Override
                public Void recover(RetryContext context){
                    System.out.println("Recovering...");
                    return null;
                }
            }
        );
    }
}

还有一个utils类:

@EnableRetry
@Configuration
public class MyConfiguration {

    @Bean
    public MyRetry myBean() throws Exception {
        return new MyRetry();
    }

    @Bean
    public RetryTemplate retryTemplate() {
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);

        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(1500); // 1.5 seconds

        RetryTemplate template = new RetryTemplate();
        template.setRetryPolicy(retryPolicy);
        template.setBackOffPolicy(backOffPolicy);

        return template;
    }
}

此代码尝试调用@Component public class MyUtility implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { MyRetry myRetry = applicationContext.getBean(MyRetry.class); try { myRetry.withTemplate(); } catch (Exception e) { e.printStackTrace(); } } } 5次,然后恢复。它真的是微不足道的,但有时琐碎的事情是最棘手的。一些来源:spring documentation on retrya useful tutorial