2016-07-05 115 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

去除返回類型後仍不工作:( –