2016-07-05 3 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 о методе, который должен быть обнаружен, его нужно правильно вызывать из инициализированного контекста. Вызывается ли метод из компонента из весеннего контекста или вызван другим способом?

Если вы испытываете это ваш бегун, используя SpringJunit4ClassRunner?

+0

Спасибо, у меня нет возможности взглянуть на это, я вернусь. – engg

+0

@engg это сработало? – UserF40

+0

К сожалению, я еще не зарегистрирован, сделаю и обновит. – engg

6

Я решил. Я понял, что если вы возвращаете что-то из метода, который вы пытаетесь повторить, то @Retryable() не работает.

Maven зависимостей в 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

еще не работает после удаления типа возврата :( –

Смежные вопросы