0

I have used spring @Retryable to implement retry function call if there is any problems occurs when calling another service using RestTemplate.

The function is given below, the problem is that I have given maxAttempts to 4 in case of any exception happens it should try for 4 times. But even without any exception the function is executing 4 times and four entries of employee is created in the DB.

createEmployee function which calls another service for creating the employee in DB

@Retryable(value = { Exception.class }, maxAttempts = 4, backoff = @Backoff(delay = 1000))
public Response createEmployee(EmployeeRequest employeeRequest) 
{
  log.info(“creating employee”)
  :
  // calls another micro service using RestTemplate which creates employee into the DB
  :
}

@EnableRetry in AppConfig

@Configuration
@EnableRetry
public class AppConfig {
}

Can anyone please help me on this

Alex Man
  • 4,746
  • 17
  • 93
  • 178

3 Answers3

0

You should review your "calls another microservice" implementation.

It could be throwing the exception by another service that is being called inside that logic. I suggest creating a custom exception and use it on your Retry definition. Then you could check if another kind of unexpected exception is the one that was forcing the 4 attempts in the retry.

@Retryable(value = { YourCustomException.class }, maxAttempts = 4, backoff = @Backoff(delay = 1000))
public Response createEmployee(EmployeeRequest employeeRequest) 
{
  log.info(“creating employee”)
  :
  // calls another micro service using RestTemplate which creates employee into the DB
  :
}
Charles. Hi
  • 111
  • 1
  • 8
  • Thanks for the reply, but I have cross check that and the external service is working fine without having any exception. Also I don't have any `YourCustomException`, If any exception happens the root class Exception can simple catch it right – Alex Man Jul 04 '19 at 19:58
  • Yes, it will catch it. But if you isolate the expected error instead of catching a generic exception you can start tracking down if another exception is happening (that you are not expecting). Your implementation looks correct to me, that's why I'm thinking that it may be related with some exception that you are not expecting to happen. Did you try commenting out the code inside your method and run it just with the log.info? – Charles. Hi Jul 04 '19 at 20:25
0

Check what is the maximum time your service takes before it fails set back off time according to that. Check what exception the resttemplate can throw in case of failure mark them only as your retry.

niteshbisht
  • 179
  • 8
0

Ok check what exception the resttemplate can throw in case of failure mark them only as your retry.

niteshbisht
  • 179
  • 8