Spring framework has a retry project that is very useful when you have a business logic failure that you want to be able to retry to complete.
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
@Configuration
@EnableRetry
public class GlobalConfig {
}
@Service
@Slf4j
public class UserServiceImp implements UserService {
@Retryable(value = {RuntimeException.class}, maxAttempts = 3, backoff = @Backoff(delay = 2000))
@Override
public boolean sendEmail(User user) throws RuntimeException {
log.info("send email");
if (true) {
throw new RuntimeException("error");
}
return false;
}
@Recover
@Override
public boolean recover(RuntimeException e, User user) {
log.info("recover");
return true;
}
}