The following code is not retrying. What am I missing?
@EnableRetry
@SpringBootApplication
public class App implements CommandLineRunner
{
.........
....
I solved it. I figured out that if return something from the method that you trying to retry, then @Retryable() is not working.
maven dependency in pom.xml
org.springframework.retry
spring-retry
1.1.5.RELEASE
Spring boot Application.java
@SpringBootApplication
@EnableTransactionManagement
@EnableRetry
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
in controller.java
@RestController
public class JavaAllDataTypeController {
@Autowired
JavaAllDataTypeService JavaAllDataTypeService;
@RequestMapping(
value = "/springReTryTest",
method = RequestMethod.GET
)
public ResponseEntity springReTryTest() {
System.out.println("springReTryTest controller");
try {
JavaAllDataTypeService.springReTryTest();
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity("abcd", HttpStatus.OK);
}
}
in service.java
@Service
@Transactional
public class JavaAllDataTypeService {
// try the method 9 times with 2 seconds delay.
@Retryable(maxAttempts=9,value=Exception.class,backoff=@Backoff(delay = 2000))
public void springReTryTest() throws Exception {
System.out.println("try!");
throw new Exception();
}
}
output: It' trying 9 times then throwing exception.