esseara
esseara

Reputation: 880

Spring @Retryable not working

I have read a lot on the subject but somehow can't manage to make my code work. I have a very simple project and I'm trying to implement the spring-retry process. Here's my main application class:

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

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

And my Support 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...");
    }

}

And part of my build.gradle file:

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

When I execute my project, the @Retryable won't manage the exception, as you can see by the following output:

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

Can anyone help? I don't understand what I'm doing wrong... Thanks!

[EDIT] I have added a configuration class and changed a little my code, but the result is the same. Here's my configuration class:

@EnableRetry
@Configuration
public class MyConfiguration {

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

And the MyRetry class:

public class MyRetry {

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

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

and the main application class:

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

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

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

Upvotes: 1

Views: 11181

Answers (1)

esseara
esseara

Reputation: 880

I used a different approach - the spring RetryTemplate - and found a working solution. Here's my main application class:

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

The class that contains the method to retry:

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

The configuration class:

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

And a utils class:

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

    }
}

This code tries to call myMethod() 5 times, then recovers. It's really trivial but sometimes trivial things are the trickiest. Some sources: spring documentation on retry and a useful tutorial.

Upvotes: 4

Related Questions