@Retryable在Spring引导应用程序中由集成测试触发时不进行重试

use*_*476 5 java spring spring-retry spring-boot

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

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();

}
Run Code Online (Sandbox Code Playgroud)

Bij*_*men 5

您的注释@EnableRetry位于错误的位置,而不是将其放在ActionService接口上,您应该将它放在基于Spring Java的@Configuration类中,在此实例中使用MyApp该类.通过此更改,重试逻辑应按预期工作.如果您对更多详细信息感兴趣,请阅读我写的博客文章 - http://biju-allandsundry.blogspot.com/2014/12/spring-retry-ways-to-integrate-with.html