Skip to content

Commit a650509

Browse files
adinauershanamatthewsgetsantry[bot]
authored
Docs for Java SDK Check-ins (#7963)
Co-authored-by: Shana Matthews <[email protected]> Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
1 parent c443f7d commit a650509

File tree

9 files changed

+279
-6
lines changed

9 files changed

+279
-6
lines changed

src/docs/product/crons/getting-started/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ To set up Sentry Crons, use the links below for supported SDKs or the Sentry CLI
1919
- [SvelteKit](/platforms/javascript/guides/sveltekit/crons/)
2020
- [Remix](/platforms/javascript/guides/remix/crons/)
2121
- [Go](/platforms/go/crons/)
22+
- [Java](/platforms/java/crons/)
23+
- [Spring Boot](/platforms/java/guides/spring-boot/crons/)
2224

2325
<Alert level="note" title="Don't see your platform?">
2426

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Use our <PlatformLink to="/">getting started</PlatformLink> guide to install and configure the Sentry Java SDK (min 6.30.0) for your recurring job.
2+
- [Create and configure](https://sentry.io/crons/create/) your first Monitor.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
If you are using [Quartz](http://www.quartz-scheduler.org/), please see <PlatformLink to="/configuration/integrations/quartz/">our Quartz integration</PlatformLink>. You may also [send check-ins manually](/platforms/java/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). To start sending check-ins simply add the `@SentryCheckIn("<monitor-slug>")` annotation to the method you want to send check-ins for.
6+
7+
```java {tabTitle:Java (Spring Boot 3)}
8+
import io.sentry.spring.jakarta.checkin.SentryCheckIn;
9+
10+
@Component
11+
public class CustomJob {
12+
13+
@Scheduled(fixedRate = 3 * 60 * 1000L)
14+
@SentryCheckIn("<monitor-slug>") // 👈
15+
void execute() throws InterruptedException {
16+
// your task code
17+
}
18+
}
19+
```
20+
21+
```java {tabTitle:Java (Spring Boot 2)}
22+
import io.sentry.spring.checkin.SentryCheckIn;
23+
24+
@Component
25+
public class CustomJob {
26+
27+
@Scheduled(fixedRate = 3 * 60 * 1000L)
28+
@SentryCheckIn("<monitor-slug>") // 👈
29+
void execute() throws InterruptedException {
30+
// your task code
31+
}
32+
}
33+
```
34+
35+
## Heartbeat
36+
37+
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.
38+
39+
```java {tabTitle:Java (Spring Boot 3)}
40+
import io.sentry.spring.jakarta.checkin.SentryCheckIn;
41+
42+
@Component
43+
public class CustomJob {
44+
45+
@Scheduled(fixedRate = 3 * 60 * 1000L)
46+
@SentryCheckIn(monitorSlug = "<monitor-slug>", heartbeat = true) // 👈
47+
void execute() throws InterruptedException {
48+
// your task code
49+
}
50+
}
51+
```
52+
53+
```java {tabTitle:Java (Spring Boot 2)}
54+
import io.sentry.spring.checkin.SentryCheckIn;
55+
56+
@Component
57+
public class CustomJob {
58+
59+
@Scheduled(fixedRate = 3 * 60 * 1000L)
60+
@SentryCheckIn(monitorSlug = "<monitor-slug>", heartbeat = true) // 👈
61+
void execute() throws InterruptedException {
62+
// your task code
63+
}
64+
}
65+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### How Do I Send an Attachment With a Check-in (Such as a Log Output)?
2+
3+
Attachments aren't supported by our Java SDK yet. For now, you can use the [check-in attachments API](/product/crons/getting-started/http/#check-in-attachment-optional).

src/platforms/common/crons/index.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ supported:
55
- python
66
- php
77
- node
8+
- java
89
- javascript.nextjs
910
- javascript.sveltekit
1011
- javascript.remix
@@ -16,7 +17,7 @@ description: "Learn how to enable Cron Monitoring in your app"
1617

1718
Sentry Crons allows you to monitor the uptime and performance of any scheduled, recurring job. Once implemented, it'll allow you to get alerts and metrics to help you solve errors, detect timeouts, and prevent disruptions to your service.
1819

19-
<PlatformSection supported={["python", "php", "node", "javascript.nextjs", "javascript.sveltekit", "javascript.remix", "go"]}>
20+
<PlatformSection supported={["python", "php", "node", "javascript.nextjs", "javascript.sveltekit", "javascript.remix", "go", "java"]}>
2021

2122
## Requirements
2223

@@ -36,7 +37,7 @@ To link any exceptions captured during your job's lifecycle, use <PlatformLink t
3637

3738
</PlatformSection>
3839

39-
<PlatformSection notSupported={["python", "php", "node", "javascript.nextjs", "javascript.sveltekit", "javascript.remix", "go"]}>
40+
<PlatformSection notSupported={["python", "php", "node", "javascript.nextjs", "javascript.sveltekit", "javascript.remix", "go", "java"]}>
4041

4142
## Requirements
4243

src/platforms/common/crons/troubleshooting.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ supported:
55
- python
66
- php
77
- node
8+
- java
89
- javascript.nextjs
910
- javascript.sveltekit
1011
- javascript.remix
@@ -16,6 +17,7 @@ description: "Learn how to troubleshoot your Cron Monitoring setup."
1617
"python",
1718
"php",
1819
"node",
20+
"java",
1921
"javascript.nextjs",
2022
"javascript.sveltekit",
2123
"javascript.remix"

src/platforms/java/common/configuration/integrations/index.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
title: Integrations
33
sidebar_order: 200
44
description: "Learn more about how integrations extend the functionality of our SDK to cover common libraries and environments automatically."
5-
notSupported:
6-
- java.logback
7-
- java.log4j2
8-
- java.jul
95
---
106

117
<PageGrid />
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Quartz Integration
3+
sidebar_order: 10
4+
description: "Learn how to send check-ins for your Quartz jobs."
5+
---
6+
7+
Sentry's [Quartz](http://www.quartz-scheduler.org/) integration is provided through `SenryJobListener` which you have to add to your scheduler instance.
8+
9+
The Quartz integration will be configured automatically if you're using `spring-boot-starter-quartz` with either the `sentry-spring-boot-starter` or the `sentry-spring-boot-jakarta-starter` integration. However, you still have to specify the monitor slug as shown below.
10+
11+
Check-ins may also be [sent manually](/platforms/java/crons/) or if you're using Sentry's Spring Boot integration you can send send check-ins for your `@Scheduled` tasks by using our [`@SentryCheckIn` annotation](/platforms/java/guides/spring-boot/crons/).
12+
13+
## Install
14+
15+
To install use:
16+
17+
```groovy {tabTitle:Gradle Plugin}
18+
plugins {
19+
id "io.sentry.jvm.gradle" version "{{@inject packages.version('sentry.java.android.gradle-plugin', '3.13.0') }}"
20+
}
21+
```
22+
23+
```groovy {tabTitle:Gradle}
24+
implementation 'io.sentry:sentry-quartz:{{@inject packages.version('sentry.java.quartz', '6.30.0') }}'
25+
```
26+
27+
```xml {tabTitle:Maven}
28+
<dependency>
29+
<groupId>io.sentry</groupId>
30+
<artifactId>sentry-quartz</artifactId>
31+
<version>{{@inject packages.version('sentry.java.quartz', '6.30.0') }}</version>
32+
</dependency>
33+
```
34+
35+
```scala {tabTitle: SBT}
36+
libraryDependencies += "io.sentry" % "sentry-quartz" % "{{@inject packages.version('sentry.java.quartz', '6.30.0') }}"
37+
```
38+
39+
For other dependency managers, check out the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-quartz).
40+
41+
## Set Up
42+
43+
You have to provide the monitor slug either
44+
45+
- when building a Quartz `JobDetail` instance
46+
- or when building a Quartz `Trigger` instance
47+
48+
To do so, you have to add an entry to the jobs data map:
49+
50+
```java
51+
import io.sentry.quartz.SentryJobListener;
52+
53+
// you can set the monitor slug on the job detail
54+
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
55+
jobDetailFactory.setJobDataAsMap(Collections.singletonMap(SentryJobListener.SENTRY_SLUG_KEY, "<monitor-slug>"));
56+
57+
// you can also set the monitor slug on the trigger
58+
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
59+
trigger.setJobDataAsMap(Collections.singletonMap(SENTRY_SLUG_KEY, "monitor_slug_simple_trigger"));
60+
```
61+
62+
<Note>
63+
64+
Setting the monitor slug on the `Trigger` will override what is set on the `JobDetail`. This also means you can set up a separate monitor per `Trigger`.
65+
66+
</Note>

0 commit comments

Comments
 (0)