0

I am using retry template as below for an exception that might occur. The retryTemplate.execute block never gets called. I have defined the retrytemplate bean as below. Not sure if that's the correct way. I am new to java and spring. Any help will be appreciated.

    public class checkUser{ 
    
    RetryTemplate retryTemplate;
        

      public checkUser(){}

      public checkUser(RetryTemplate retryTemplate){

       this.retryTemplate = retryTemplate;}
    

     private RetryTemplate retryConfig()  {
    
            RetryTemplate retryTemplate = new RetryTemplate();
            FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
            fixedBackOffPolicy.setBackOffPeriod(2000l);
            retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
    
    
            SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
            retryPolicy.setMaxAttempts(2);
            retryTemplate.setRetryPolicy(retryPolicy);
    
            return retryTemplate;
    
        }
    
    private void checkGroup() throws Exception {
            retryTemplate = retryConfig();
           
            try {
    
                retryTemplate.execute(new RetryCallback<Object>() {
                    @Override
                    public Object doWithRetry(RetryContext retryContext) throws Exception {
                        if (retryContext.getRetryCount() < 2) { // unexpected disconnection
                            throw new RuntimeException("retry exception");
    
                        }
    
                      //do something
    
    
                        }
    
                        return null;
                    }
                });
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

defined bean as below:

<bean id="checkUser"
          class="com.checkUser">

        <constructor-arg index="1" value="${retryTemplate}" />
        
    </bean>

    <bean id = "retryTemplate" class="com.checkUser">

    </bean>
Suhana
  • 21
  • 2

1 Answers1

0

Spring beans are completely passive; the execute won't be called until something calls checkGroup().

You should also not call retryConfig() for each call, that will simply overwrite the one passed into the constructor.

Your XML configuration makes no sense at all.

You need <constructor-arg ref="templateBean"/> where templateBean is a bean of type RetryTemplate.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179