Jason
Jason

Reputation: 1431

Spring Retry Not working

I am trying to setup a very simple test to see if I can get Springs Retry API working but it doesn't seem to be working as expected. Below is my code / configuration. I am using Spring 3.0

Spring Retry Version defined in POM.xml

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

Interface for class under test

public interface RetryTest
{
     void test() throws Exception;
}

Interface Impl

@Component("retryTest")
@EnableRetry
public class RetryTestImpl implements RetryTest
{

   @Retryable(maxAttempts = 3)
   public void test() throws Exception
   {
      System.out.println("try!");
      throw new Exception();
   }
}

JUnit Class

public class TestIt
{

   @Before
   public void initSpringContext()
   {
      // Load the spring context 
      ApplicationContextUtil.initAppContext(MyConfig.class);
   }

   @Test
   public void test_retryLogic() throws Exception
   {

       RetryTest retryTest = (RetryTest)ApplicationContextUtil.getApplicationContext().getBean(
            "retryTest");
       retryTest.test();
   }

}

Console Output

Bean loaded: retryTest
try!

I am expecting the "try!" to be printed to the console 3 times. Instead an Exception is thrown and the JUnit fails right there. Shouldn't the retry run 3 times? What am I missing here?

Upvotes: 1

Views: 2184

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

You need to show your MyConfig class.

The @EnableRetry goes on one of your @Configuration classes, not the retryable target.

See the java doc for the annotation:

 * Global enabler for <code>@Retryable</code> annotations in Spring beans. If this is
 * declared on any <code>@Configuration</code> in the context then beans that have
 * retryable methods will be proxied and the retry handled according to the metadata in
 * the annotations.

Upvotes: 2

Related Questions