SimpleRetryPolicy used as MaxAttemptsRetryPolicy Example (using xml configuration)

In this post under Spring Retry, I will show with example how to use SimpleRetryPolicy (another implementation of RetryPolicy interface) in place of MaxAttemptsRetryPolicy.

SimpleRetryPolicy retries a failed operation for fixed number of times for a set of named exceptions (and subclasses).

In this post I will configure SimpleRetryPolicy to retry failed operation for fixed number of times for any exception. Configuring the SimpleRetryPolicy to work like this is similar to MaxAttemptsRetryPolicy purpose.

Below is the xml configuration

XML Code

1  <?xml version="1.0" encoding="UTF-8"?>
2  <beans xmlns=""
3  xmlns:xsi=""
4  xsi:schemaLocation="">
5      <bean id="retryPolicy" class="org.springframework.retry.policy.SimpleRetryPolicy">
6          <constructor-arg name="maxAttempts" value="4"/>
7      </bean>
9      <bean id="retryTemplate" class="">
10         <property name="retryPolicy" ref="retryPolicy"/>
11     </bean>
13     <bean id="service1" class="Service1"/>
14 </beans>

In the above xml code, at line 5 I have created a bean named “retryPolicy” for SimpleRetryPolicy class and set its “maxAttempts” property to 4.

At line 10, we are referring to the bean “retryPolicy” when setting the property “retryPolicy” of class RetryTemplate.

In this way we are telling SimpleRetryPolicy to retry a failed operation 4 times for any exception. This is similar to MaxAttemptsRetryPolicy.

Below is the Service and Main class for your reference

Service class

public class Service1 {
    private int i = 0;
    private int j = 0;

    public void executeWithException() {
        i = i + 1;
        System.out.println("Executing method 'executeWithException' : " + i);
        throw new NullPointerException();

    public void executeWithoutException() {
        j = j + 1;
        System.out.println("Executing method 'executeWithoutException' : " + j);

    public String recovery() {
        return "Recovered";

Main Class

import org.springframework.context.ApplicationContext;

public class Example7 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Example7.xml");
        Service1 service1 = (Service1)context.getBean("service1");
        RetryTemplate retryTemplate =  (RetryTemplate)context.getBean("retryTemplate");

        try {
            retryTemplate.execute(retryContext -> { service1.executeWithException(); return null; });
        } catch(NullPointerException excep) {

Below is the output


[INFO] ClassPathXmlApplicationContext – Refreshing startup date [Sun Jan 23 09:26:48 IST 2022]; root of context hierarchy
[INFO] XmlBeanDefinitionReader – Loading XML bean definitions from class path resource [Example7.xml]
Executing method ‘executeWithException’ : 1
Executing method ‘executeWithException’ : 2
Executing method ‘executeWithException’ : 3
Executing method ‘executeWithException’ : 4
at Service1.executeWithException(
at Example7.lambda$0(
at Example7.main(


Press like button if you like the post. Also share your opinion regarding posts through comments. Also mention what other framework you want me to cover through comments.

Leave a Reply