javaspringspring-bootspring-scheduledspring-retry

How to retry @Scheduled task in spring


I have the following code sample:

@Service
public class ScheduledTask {

    // Running each hour at 0 min. and 0 seconds
    @Scheduled(cron = "0 0 * * * *")
    @Retryable(value = Throwable.class, maxAttempts = 2, backoff = @Backoff(delay = 1000))
    public void execute() {
        log.info("Executing scheduled task!");
        throw new HttpTimeoutException("Let's see whether retry will rerun");
    }
}

I can see the task being executed once. But I do not see any retries. Is there any incompatibility between @Scheduled and @Retryable or am I missing something?

Or is there a better way to retry an @Scheduled task?


Solution

  • Make sure you have @EnableRetry annotation on your configuration class to enable the @Retryable annotation processing. Same for scheduling with @EnableScheduling.