Set Up Crons for Spring Boot

Sentry Crons allows you to monitor the uptime and performance of any scheduled, recurring job in your application using the Sentry Java SDK.

Once implemented, it'll allow you to get alerts and metrics to help you solve errors, detect timeouts, and prevent disruptions to your service.

  • Use our getting started guide to install and configure the Sentry Java SDK (min 6.30.0) for your recurring job.
  • Ensure that org.aspectj.aspectjweaver is present as a dependency of your project.
  • From Sentry Java SDK version 8.64.0, @SentryCheckIn(upsertMonitorConfig = true) sends a monitor configuration based on the method's @Scheduled annotation, and Sentry creates the monitor on the first check-in. Without it, on older SDK versions, or for schedules that can't be converted, create the monitor in Sentry first, because Sentry drops check-ins for a slug that has no monitor.

If you are using Quartz, please see our Quartz integration. You may also send check-ins manually.

Check-in monitoring allows you to track a job's progress by completing two check-ins: one at the start of your job and another at the end of your job. This two-step process allows Sentry to notify you if your job didn't start when expected (missed) or if it exceeded its maximum runtime (failed). To start sending check-ins simply add the @SentryCheckIn("<monitor-slug>") annotation to the method you want to send check-ins for.

Copied
import io.sentry.spring.jakarta.checkin.SentryCheckIn;

@Component
public class CustomJob {

  @Scheduled(fixedRate = 3 * 60 * 1000L)
  @SentryCheckIn("<monitor-slug>") // 👈
  void execute() throws InterruptedException {
    // your task code
  }
}

From Sentry Java SDK version 8.64.0, set upsertMonitorConfig = true and @SentryCheckIn reads the method's @Scheduled annotation and sends a monitor configuration with the in-progress check-in, so Sentry creates the monitor, or updates its schedule, from your code:

Copied
@Scheduled(cron = "0 0 * * * *")
@SentryCheckIn(value = "<monitor-slug>", upsertMonitorConfig = true) // 👈
void execute() {
  // your task code
}

The schedule is converted like this:

  • cron: a Spring cron expression with a fixed seconds field (for example 0 0 * * * *) is sent as a crontab schedule without the seconds. If the seconds field isn't 0, a check-in margin of 2 minutes is sent too, since the job checks in up to 59 seconds after the minute Sentry expects. This replaces a margin set in Sentry; set a larger options.cron default check-in margin to keep it. Spring macros such as @hourly and @daily are supported.
  • fixedRate (including fixedRateString, such as "5m" or "PT5M", and timeUnit): sent as an interval schedule when the period is a whole number of minutes. fixedDelay jobs send no monitor config.
  • zone: sent as the monitor's timezone for cron schedules. Without zone, the JVM's default time zone is sent, since that's where Spring runs the job. It must be an IANA time zone ID such as Europe/Vienna; whole-hour offsets such as GMT+2 are sent as Etc/GMT-2.

Placeholders such as ${app.cleanup.cron} are resolved. No configuration is sent for cron expressions with variable seconds (for example */30 * * * * *), cron expressions that set both day-of-month and day-of-week (on Spring 5.3 and later, a day-of-month step such as 0 0 9 */2 * MON is converted; on older Spring versions, none are), #5 days of week on Spring 5.3 and later, cron syntax Sentry doesn't support (such as 15W, L-3, or wrap-around ranges like 22-2), other time zones, periods that aren't whole minutes, methods with more than one @Scheduled, or heartbeat check-ins. For those, for methods without upsertMonitorConfig = true, and for older SDK versions, create the monitor in Sentry first.

Sentry only updates the fields that were sent. Margins, max runtime, and thresholds configured in Sentry are kept unless you set defaults for them in options.cron, which are sent only with check-ins that carry a monitor configuration.

When you turn on upsertMonitorConfig for a monitor that already exists, it gets the schedule and timezone from @Scheduled (the JVM's default time zone if zone isn't set), overwriting the ones set in Sentry.

Heartbeat monitoring notifies Sentry of a job's status through one check-in. This setup will only notify you if your job didn't start when expected (missed). If you need to track a job to see if it exceeded its maximum runtime (failed), use check-ins instead. To start sending heartbeats simply add the @SentryCheckIn(monitorSlug = "<monitor-slug>", heartbeat = true) annotation to the method you want to send heartbeats for.

Copied
import io.sentry.spring.jakarta.checkin.SentryCheckIn;

@Component
public class CustomJob {

  @Scheduled(fixedRate = 3 * 60 * 1000L)
  @SentryCheckIn(monitorSlug = "<monitor-slug>", heartbeat = true) // 👈
  void execute() throws InterruptedException {
    // your task code
  }
}

When your recurring job fails to check in (missed), runs beyond its configured maximum runtime (failed), or manually reports a failure, Sentry will create an error event with a tag to your monitor.

To receive alerts about these events:

  1. Navigate to Alerts in the sidebar.
  2. Create a new alert and select "Issues" under "Errors" as the alert type.
  3. Configure your alert and define a filter match to use: The event's tags match {key} {match} {value}.

Example: The event's tags match monitor.slug equals my-monitor-slug-here

Learn more in Alerts.

To prevent abuse and resource overuse, Crons limits check-ins to 6 per minute for each monitor environment.

For example, if you have a monitor called "database-backup" with two environments:

  • database-backup in environment production can send up to 6 check-ins per minute
  • database-backup in environment staging can also send up to 6 check-ins per minute
  • Combined, they can send up to 12 check-ins per minute

You can verify if any check-ins are being dropped by visiting the Usage Stats page. To avoid dropped check-ins, ensure your monitors don't exceed the rate limit.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").