2015-12-17 2 views
0

Я пытаюсь настроить очень простой тест, чтобы увидеть, могу ли я заставить работать Springs Retry API работать, но он работает не так, как ожидалось. Ниже приведен мой код/​​конфигурация. Я использую Spring 3,0Spring Retry Не работает

Spring Retry Версия определена в pom.xml

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

интерфейс для класса испытываемого

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

Интерфейс 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 Класс

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! 

Я ожидаю "попробовать!" для печати на консоль 3 раза. Вместо этого возникает исключение, и JUnit не работает прямо там. Не следует ли повторить попытку 3 раза? Что мне здесь не хватает?

ответ

1

Вы должны показать свой класс MyConfig.

@EnableRetry идет на один из ваших классов @Configuration, а не на повторную цель.

См Java документ для аннотирования:

* 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. 
Смежные вопросы