Browse docs

Monitor cron jobs in Spring Boot

Integrate WatchCat with Spring's @Scheduled tasks to get alerted when scheduled methods miss a run or fail silently.

Spring's @Scheduled annotation runs methods on a fixed schedule, but there's no built-in alerting when a method throws an exception or when the scheduler thread stalls. Adding a WatchCat ping at the end of each task closes that gap.

Step 1 — create a cron monitor

In WatchCat, go to Cron monitors → New monitor. Set the schedule to match your method's frequency and add a grace period of a few minutes. Copy the ping URL.

Step 2 — enable scheduling

Add @EnableScheduling to your main application class or any @Configuration class if you haven't already:

@SpringBootApplication
@EnableScheduling
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

Step 3 — ping after the task runs

Inject a RestClient (Spring 6.1+) or RestTemplate and call the ping URL at the end of your scheduled method.

Spring 6.1+ (RestClient)

@Component
public class ReportScheduler {

    private static final String PING_URL =
        "https://watchcat.io/p/cron/YOUR_TOKEN";

    private final RestClient restClient = RestClient.create();

    @Scheduled(cron = "0 0 3 * * *")
    public void generateDailyReport() {
        // ... your task logic ...

        restClient.get().uri(PING_URL).retrieve().toBodilessEntity();
    }
}

Spring 5 / Spring Boot 2 (RestTemplate)

@Component
public class ReportScheduler {

    private static final String PING_URL =
        "https://watchcat.io/p/cron/YOUR_TOKEN";

    private final RestTemplate restTemplate = new RestTemplate();

    @Scheduled(cron = "0 0 3 * * *")
    public void generateDailyReport() {
        // ... your task logic ...

        restTemplate.getForEntity(PING_URL, Void.class);
    }
}
Note: If the task throws an exception before the ping is reached, no ping is sent — which is exactly the behaviour you want. WatchCat will treat the missing ping as a failure after the grace period expires.

Start and end tracking

For long-running tasks, use a try/finally block to ping start unconditionally and end only on success. This lets WatchCat detect tasks that began but stalled.

@Scheduled(cron = "0 0 2 * * *")
public void runNightlySync() {
    restClient.get()
        .uri("https://watchcat.io/p/cron/YOUR_TOKEN/start")
        .retrieve().toBodilessEntity();

    try {
        // ... your task logic ...

        restClient.get()
            .uri("https://watchcat.io/p/cron/YOUR_TOKEN/end")
            .retrieve().toBodilessEntity();
    } catch (Exception e) {
        restClient.get()
            .uri("https://watchcat.io/p/cron/YOUR_TOKEN/fail")
            .retrieve().toBodilessEntity();
        throw e;
    }
}

Cron expression format

Spring uses a six-field cron expression: second minute hour day-of-month month day-of-week. This differs from the standard five-field Unix cron format.

0 0 3 * * * Every day at 03:00:00
0 */30 * * * * Every 30 minutes
0 0 9 * * MON-FRI Weekdays at 09:00:00

In WatchCat, set the expected interval to match your method's run frequency — for example, 0 0 3 * * * runs daily at 3am (interval: 1 day), and 0 */30 * * * * runs every 30 minutes (interval: 30 minutes).

Recommended settings

Schedule Match your @Scheduled frequency
Grace period 2–5 min for short tasks; longer for heavy processing
Alert after 1 missed run for critical tasks; 2 for routine ones

Start monitoring in minutes

Free plan available. No credit card required.