|
| 1 | +If you are using [Quartz](http://www.quartz-scheduler.org/), please see <PlatformLink to="/configuration/integrations/quartz/">our Quartz integration</PlatformLink>. If you are using Spring Boot with `@Scheduled` tasks, see [our Spring Boot integration](/platforms/java/guides/spring-boot/crons/). |
| 2 | + |
| 3 | +## Check-Ins (Recommended) |
| 4 | + |
| 5 | +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). |
| 6 | + |
| 7 | +```java |
| 8 | +import io.sentry.CheckIn; |
| 9 | +import io.sentry.CheckInStatus; |
| 10 | +import io.sentry.Sentry; |
| 11 | +import io.sentry.protocol.SentryId; |
| 12 | + |
| 13 | +// 🟡 Notify Sentry your job is running: |
| 14 | +SentryId checkInId = Sentry.captureCheckIn( |
| 15 | + new CheckIn( |
| 16 | + "<monitor-slug>", |
| 17 | + CheckInStatus.IN_PROGRESS |
| 18 | + ) |
| 19 | +); |
| 20 | + |
| 21 | +// Execute your scheduled task here... |
| 22 | + |
| 23 | +// 🟢 Notify Sentry your job has completed successfully: |
| 24 | +Sentry.captureCheckIn( |
| 25 | + new CheckIn( |
| 26 | + checkInId, |
| 27 | + "<monitor-slug>", |
| 28 | + CheckInStatus.OK |
| 29 | + ) |
| 30 | +); |
| 31 | +``` |
| 32 | + |
| 33 | +If your job execution fails, you can notify Sentry about the failure: |
| 34 | + |
| 35 | +```java |
| 36 | +// 🔴 Notify Sentry your job has failed: |
| 37 | +Sentry.captureCheckIn( |
| 38 | + new CheckIn( |
| 39 | + checkInId, |
| 40 | + "<monitor-slug>", |
| 41 | + CheckInStatus.ERROR |
| 42 | + ) |
| 43 | +); |
| 44 | +``` |
| 45 | + |
| 46 | +## Heartbeat |
| 47 | + |
| 48 | +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. |
| 49 | + |
| 50 | +```java |
| 51 | +import io.sentry.CheckIn; |
| 52 | +import io.sentry.CheckInStatus; |
| 53 | +import io.sentry.Sentry; |
| 54 | +import io.sentry.protocol.SentryId; |
| 55 | + |
| 56 | +// Execute your scheduled task... |
| 57 | + |
| 58 | +// 🟢 Notify Sentry your job completed successfully: |
| 59 | +CheckIn checkIn = new CheckIn( |
| 60 | + "<monitor-slug>", |
| 61 | + CheckInStatus.OK |
| 62 | +); |
| 63 | +checkIn.setDuration(10.0); |
| 64 | +Sentry.captureCheckIn(checkIn); |
| 65 | +``` |
| 66 | + |
| 67 | +If your job execution fails, you can: |
| 68 | + |
| 69 | +```java |
| 70 | +import io.sentry.CheckIn; |
| 71 | +import io.sentry.CheckInStatus; |
| 72 | +import io.sentry.Sentry; |
| 73 | +import io.sentry.protocol.SentryId; |
| 74 | + |
| 75 | +// 🔴 Notify Sentry your job has failed: |
| 76 | +CheckIn checkIn = new CheckIn( |
| 77 | + "<monitor-slug>", |
| 78 | + CheckInStatus.ERROR |
| 79 | +); |
| 80 | +checkIn.setDuration(10.0); |
| 81 | +Sentry.captureCheckIn(checkIn); |
| 82 | +``` |
| 83 | + |
| 84 | +## Upserting Cron Monitors |
| 85 | + |
| 86 | +You can create and update your Monitors programmatically with code |
| 87 | +rather than [creating and configuring them in Sentry.io](https://sentry.io/crons/create/). |
| 88 | + |
| 89 | +```java |
| 90 | +import io.sentry.MonitorSchedule; |
| 91 | +import io.sentry.MonitorScheduleUnit; |
| 92 | + |
| 93 | +// Create a crontab schedule object (every 10 minutes) |
| 94 | +MonitorSchedule monitorSchedule = MonitorSchedule.crontab("*/10 * * * *"); |
| 95 | + |
| 96 | +// Or create an interval schedule object (every 10 minutes) |
| 97 | +MonitorSchedule monitorSchedule = MonitorSchedule.interval(10, MonitorScheduleUnit.MINUTE); |
| 98 | +``` |
| 99 | + |
| 100 | +Supported units are: |
| 101 | + |
| 102 | +- `MonitorScheduleUnit.MINUTE` |
| 103 | +- `MonitorScheduleUnit.HOUR` |
| 104 | +- `MonitorScheduleUnit.DAY` |
| 105 | +- `MonitorScheduleUnit.WEEK` |
| 106 | +- `MonitorScheduleUnit.MONTH` |
| 107 | +- `MonitorScheduleUnit.YEAR` |
| 108 | + |
| 109 | +```java |
| 110 | +import io.sentry.MonitorConfig; |
| 111 | + |
| 112 | +// Create a config object |
| 113 | +MonitorConfig monitorConfig = new MonitorConfig(monitorSchedule); |
| 114 | +monitorConfig.setTimezone("Europe/Vienna"); // Optional timezone |
| 115 | +monitorConfig.setCheckinMargin(5L); // Optional check-in margin in minutes |
| 116 | +monitorConfig.setMaxRuntime(15L); // Optional max runtime in minutes |
| 117 | + |
| 118 | +// 🟡 Notify Sentry your job is running: |
| 119 | +CheckIn checkIn = new CheckIn( |
| 120 | + "<monitor-slug>", |
| 121 | + CheckInStatus.IN_PROGRESS |
| 122 | +); |
| 123 | +checkIn.setMonitorConfig(monitorConfig); |
| 124 | +SentryId checkInId = Sentry.captureCheckIn(checkIn); |
| 125 | + |
| 126 | +// Execute your scheduled task here... |
| 127 | + |
| 128 | +// 🟢 Notify Sentry your job has completed successfully: |
| 129 | +Sentry.captureCheckIn( |
| 130 | + new CheckIn( |
| 131 | + checkInId, |
| 132 | + "<monitor-slug>", |
| 133 | + CheckInStatus.OK |
| 134 | + ) |
| 135 | +); |
| 136 | +``` |
0 commit comments