2015-07-22 64 views
4

我在SpringBoot应用程序的服务中有一个简单的方法。我使用@Retryable为该方法设置了重试机制。
我正在尝试服务中的方法的集成测试,并且当方法抛出异常时不会发生重试。该方法只执行一次。@Retryable在集成测试触发时未进行重试在Spring Boot应用程序中

public interface ActionService { 

@Retryable(maxAttempts = 3, backoff = @Backoff(delay = 2000)) 
public void perform() throws Exception; 

} 



@Service 
public class ActionServiceImpl implements ActionService { 

@Override 
public void perform() throws Exception() { 

    throw new Exception(); 
    } 
} 



@SpringBootApplication 
@Import(RetryConfig.class) 
public class MyApp { 

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



@Configuration 
@EnableRetry 
public class RetryConfig { 

@Bean 
public ActionService actionService() { return new ActionServiceImpl(); } 

} 



@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes= {MyApp.class}) 
@IntegrationTest({"server.port:0", "management.port:0"}) 
public class MyAppIntegrationTest { 

@Autowired 
private ActionService actionService; 

public void testAction() { 

    actionService.perform(); 

} 
+0

我不认为'@ Retryable'被继承。尝试将它移动到bean服务方法而不是接口方法 –

回答

4

你的注释@EnableRetry是在错误的地方,而不是把它ActionService界面上,你应该用一个基于的Spring Java @Configuration类放置,在这种情况下与MyApp类。通过这种更改,重试逻辑应该按预期工作。这里是我写过的博客文章,如果你对更多细节感兴趣 - http://biju-allandsundry.blogspot.com/2014/12/spring-retry-ways-to-integrate-with.html

+0

这是一个SpringBoot应用程序,除了接口本身之外,我应该将Service作为配置中的Bean来使用。 我已更新与我的更改的帖子。它仍然不起作用。 – user3451476

+0

我测试了你的类,它对我很好,'actionService'被调用了三次,然后返回一个异常,尝试在你的执行方法中打印一些东西,看它多次调用。 –

0

Biju感谢你为此提供了一个链接,它帮助我解决了很多问题。我唯一需要分开的是我仍然必须在基于spring xml的方法中将'retryAdvice'添加为bean,还必须确保启用了上下文注释,并且可以在classpath中使用aspectj。在我在下面添加这些之后,我可以开始工作。

<bean id="retryAdvice" 
    class="org.springframework.retry.interceptor.RetryOperationsInterceptor"> 
</bean> 

<context:annotation-config /> 
<aop:aspectj-autoproxy /> 
+0

'@ Configuration'与''相同,'@ EnableAspectJAutoProxy'与''相同,'@ EnableRetry'添加'@ EnableAspectJAutoProxy'。添加'spring-boot-starter-aop'作为依赖提供'aspectjweaver'。 – WhiteKnight

相关问题