Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring retry XML equivalent to @EnableRetry

I have to work with a project where I cannot use Java-Config for Spring, but I have to use XML-Config. Now I am looking for an XML-Config equivalent to @EnableRetry from Java-Config.

I want this line to work.

@Retryable(SQLException.class)
public void saveOrUpdate(Entity entity) 
like image 774
Tarken Avatar asked Aug 10 '15 15:08

Tarken


1 Answers

Yes, @EnableRetry on an empty @Configuration will work (comment by OP).

Just for completeness, here's the equivalent in XML.

<context:annotation-config />

<aop:aspectj-autoproxy />

<bean class="org.springframework.retry.annotation.RetryConfiguration" />

EDIT

Complete example using XML to enable retry:

@SpringBootApplication
@ImportResource("context.xml")
public class So31923175Application {

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

    @Bean
    public ApplicationRunner runner(Foo app) {
        return args -> {
            try {
                app.retry("foo");
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        };
    }

    @Component
    public static class Foo {

        @Retryable
        public void retry(String param) {
            System.out.println(param);
            throw new RuntimeException("test retry");
        }

    }

}

and

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config />

    <aop:aspectj-autoproxy />

    <bean class="org.springframework.retry.annotation.RetryConfiguration" />

</beans>

and

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.gprussell.filemailer</groupId>
    <artifactId>so31923175</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>so31923175</name>
    <description>Mail non-empty files passed on the command line</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

and

foo
foo
foo
java.lang.RuntimeException: test retry
like image 189
Gary Russell Avatar answered Oct 14 '22 14:10

Gary Russell