Cronwize
Guide · Spring

Monitor a Spring @Scheduled job

Paste the cron expression from the annotation unchanged, then ping Cronwize at the start, on success and on failure.

1. Create the monitor from the annotation

Spring cron has six fields with seconds first. Cronwize reads it as written, so 0 0 2 * * * means 02:00 every day. Pick the same zone as the annotation's zone attribute.

Faster than once a minute is rejectedCronwize checks once a minute, so an expression like 0/30 * * * * * is refused with an explanation.

2. Send start, success and failure pings

@Component
class NightlyBackup {
  private static final String PING = "https://ping.cronwize.com/YOUR-KEY";
  private final HttpClient http = HttpClient.newHttpClient();

  @Scheduled(cron = "0 0 2 * * *", zone = "Europe/Belgrade")
  void run() {
    ping("/start", "");
    try {
      backup();
      ping("", "");
    } catch (RuntimeException e) {
      ping("/fail", String.valueOf(e.getMessage()));
      throw e;
    }
  }

  // Fire and forget: a slow or failed ping never blocks the job.
  private void ping(String signal, String body) {
    HttpRequest req = HttpRequest.newBuilder(URI.create(PING + signal))
        .timeout(Duration.ofSeconds(10))
        .POST(HttpRequest.BodyPublishers.ofString(body))
        .build();
    http.sendAsync(req, HttpResponse.BodyHandlers.discarding());
  }
}

The body of a /fail ping, up to 10 KB, is kept in the ping log and the first 500 characters go into the alert. Do not send secrets.

3. Check the first ping

Run the job once. The monitor turns from new to up and the run strip shows the first tick with its duration.

Up3 min 41 s