gpt4 book ai didi

java - Spring @Retryable 不工作

转载 作者:行者123 更新时间:2023-11-30 02:41:22 31 4
gpt4 key购买 nike

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

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

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

还有我的支持类(class):

@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);
}
}

最佳答案

我使用了不同的方法 - spring 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 documentation on retrya useful tutorial .

关于java - Spring @Retryable 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41587909/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com