2016-07-05 119 views
4

以下代码不会重试。我错过了什么?Springboot @retryable not retrying

@EnableRetry 
@SpringBootApplication 
public class App implements CommandLineRunner 
{ 
    ......... 
    ......... 


    @Retryable() 
    ResponseEntity<String> authenticate(RestTemplate restTemplate, HttpEntity<MultiValueMap<String, String>> entity) throws Exception 
    { 
     System.out.println("try!"); 
     throw new Exception(); 
     //return restTemplate.exchange(auth_endpoint, HttpMethod.POST, entity, String.class); 
    } 

我已将以下内容添加到pom.xml中。

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

我也试着给@Retryable提供不同的参数组合。

@Retryable(maxAttempts=10,value=Exception.class,[email protected](delay = 2000,multiplier=2)) 

谢谢。

+0

我正面临同样的问题。 –

回答

2

@Retryable注释的方法去发现它需要从初始化上下文正确调用。该方法是从Spring上下文中的bean调用还是通过其他方式调用?

如果这是您的亚军使用SpringJunit4ClassRunner

+0

谢谢,我没有机会看这个,我会回来的。 – engg

+0

@engg是否有效? – UserF40

+0

对不起,我还没有检查,会做和更新。 – engg

6

我解决了它。我发现如果从你尝试重试的方法中返回一些东西,那么@Retryable()不起作用。在pom.xml中

行家依赖

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

春季启动Application.java

@SpringBootApplication 
@EnableTransactionManagement 
@EnableRetry 
public class Application { 

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

} 

在controller.java

@RestController 
public class JavaAllDataTypeController { 

@Autowired 
JavaAllDataTypeService JavaAllDataTypeService; 


@RequestMapping(
     value = "/springReTryTest", 
     method = RequestMethod.GET 
) 
public ResponseEntity<String> springReTryTest() { 

    System.out.println("springReTryTest controller"); 

    try { 
     JavaAllDataTypeService.springReTryTest(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return new ResponseEntity<String>("abcd", HttpStatus.OK); 
    } 

} 

在service.java

@Service 
@Transactional 
public class JavaAllDataTypeService { 

// try the method 9 times with 2 seconds delay. 
@Retryable(maxAttempts=9,value=Exception.class,[email protected](delay = 2000)) 
public void springReTryTest() throws Exception { 

    System.out.println("try!"); 
    throw new Exception(); 
    } 

} 

输出:它'尝试9次然后抛出异常。

enter image description here

+0

去除返回类型后仍不工作:( –