[英]Spring @Retryable not working
我已經閱讀了很多有關該主題的文章,但是以某種方式無法使我的代碼正常工作。 我有一個非常簡單的項目,我正在嘗試實施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);
}
}
我使用了另一種方法RetryTemplate
,找到了一個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;
}
}
);
}
}
配置類:
@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;
}
}
還有一個utils類:
@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();
}
}
}
此代碼嘗試調用myMethod()
5次,然后恢復。 這確實很瑣碎,但有時瑣碎的事情是最棘手的。 一些資源: 有關重試的spring文檔和有用的教程 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.