Skip to content

Add Quartz actuator endpoint #10364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[[quartz]]
= Quartz (`quartz`)

The `quartz` endpoint provides information about the scheduled jobs that are managed by
Quartz Scheduler.



[[quartz-report]]
== Retrieving the Quartz report

To retrieve the Quartz, make a `GET` request to `/application/quartz`,
as shown in the following curl-based example:

include::{snippets}quartz-report/curl-request.adoc[]

The resulting response is similar to the following:

include::{snippets}quartz-report/http-response.adoc[]



[[quartz-job]]
== Retrieving the Quartz job details

To retrieve the Quartz, make a `GET` request to `/application/quartz/{group}/{name}`,
as shown in the following curl-based example:

include::{snippets}quartz-job/curl-request.adoc[]

The preceding example retrieves the job with the `group` of `groupOne` and `name` of
`jobOne`. The resulting response is similar to the following:

include::{snippets}quartz-job/http-response.adoc[]



[[quartz-job-response-structure]]
=== Response Structure

The response contains details of the scheduled job. The following table describes the
structure of the response:

[cols="2,1,3"]
include::{snippets}quartz-job/response-fields.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ include::endpoints/logfile.adoc[leveloffset=+1]
include::endpoints/loggers.adoc[leveloffset=+1]
include::endpoints/metrics.adoc[leveloffset=+1]
include::endpoints/prometheus.adoc[leveloffset=+1]
include::endpoints/quartz.adoc[leveloffset=+1]
include::endpoints/scheduledtasks.adoc[leveloffset=+1]
include::endpoints/sessions.adoc[leveloffset=+1]
include::endpoints/shutdown.adoc[leveloffset=+1]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.quartz;

import org.quartz.Scheduler;

import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.quartz.QuartzEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link QuartzEndpoint}.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@Configuration
@ConditionalOnClass(Scheduler.class)
@AutoConfigureAfter(QuartzAutoConfiguration.class)
public class QuartzEndpointAutoConfiguration {

@Bean
@ConditionalOnBean(Scheduler.class)
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public QuartzEndpoint quartzEndpoint(Scheduler scheduler) {
return new QuartzEndpoint(scheduler);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Auto-configuration for actuator Quartz Scheduler concerns.
*/
package org.springframework.boot.actuate.autoconfigure.quartz;
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ org.springframework.boot.actuate.autoconfigure.management.ThreadDumpEndpointAuto
org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.mongo.MongoHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.neo4j.Neo4jHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.quartz.QuartzEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.redis.RedisHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.scheduling.ScheduledTasksEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.session.SessionsEndpointAutoConfiguration,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.endpoint.web.documentation;

import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.regex.Pattern;

import org.junit.Test;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.matchers.GroupMatcher;

import org.springframework.boot.actuate.quartz.QuartzEndpoint;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.replacePattern;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* Tests for generating documentation describing the {@link QuartzEndpoint}.
*
* @author Vedran Pavic
*/
public class QuartzEndpointDocumentationTests extends AbstractEndpointDocumentationTests {

private static final JobDetail jobOne = JobBuilder.newJob(Job.class)
.withIdentity("jobOne", "groupOne").withDescription("My first job").build();

private static final JobDetail jobTwo = JobBuilder.newJob(Job.class)
.withIdentity("jobTwo", "groupOne").build();

private static final JobDetail jobThree = JobBuilder.newJob(Job.class)
.withIdentity("jobThree", "groupTwo").build();

private static final Trigger triggerOne = TriggerBuilder.newTrigger().forJob(jobOne)
.withIdentity("triggerOne").withDescription("My first trigger")
.modifiedByCalendar("myCalendar")
.startAt(Date.from(Instant.parse("2017-12-01T12:00:00Z")))
.endAt(Date.from(Instant.parse("2017-12-01T12:30:00Z")))
.withSchedule(SimpleScheduleBuilder.repeatMinutelyForever()).build();

private static final Trigger triggerTwo = TriggerBuilder.newTrigger().forJob(jobOne)
.withIdentity("triggerTwo").withDescription("My second trigger")
.modifiedByCalendar("myCalendar")
.startAt(Date.from(Instant.parse("2017-12-01T00:00:00Z")))
.endAt(Date.from(Instant.parse("2017-12-10T00:00:00Z")))
.withSchedule(SimpleScheduleBuilder.repeatHourlyForever()).build();

@MockBean
private Scheduler scheduler;

@Test
public void quartzReport() throws Exception {
String groupOne = jobOne.getKey().getGroup();
String groupTwo = jobThree.getKey().getGroup();
given(this.scheduler.getJobGroupNames())
.willReturn(Arrays.asList(groupOne, groupTwo));
given(this.scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupOne)))
.willReturn(
new HashSet<>(Arrays.asList(jobOne.getKey(), jobTwo.getKey())));
given(this.scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupTwo)))
.willReturn(Collections.singleton(jobThree.getKey()));
this.mockMvc.perform(get("/application/quartz")).andExpect(status().isOk())
.andDo(document("quartz/report"));
}

@Test
public void quartzJob() throws Exception {
JobKey jobKey = jobOne.getKey();
given(this.scheduler.getJobDetail(jobKey)).willReturn(jobOne);
given(this.scheduler.getTriggersOfJob(jobKey))
.willAnswer(invocation -> Arrays.asList(triggerOne, triggerTwo));
this.mockMvc.perform(get("/application/quartz/groupOne/jobOne"))
.andExpect(status().isOk())
.andDo(document("quartz/job",
preprocessResponse(replacePattern(
Pattern.compile("org.quartz.Job"), "com.example.MyJob")),
responseFields(
fieldWithPath("jobGroup").description("Job group."),
fieldWithPath("jobName").description("Job name."),
fieldWithPath("description")
.description("Job description, if any."),
fieldWithPath("className").description("Job class."),
fieldWithPath("triggers.[].triggerGroup")
.description("Trigger group."),
fieldWithPath("triggers.[].triggerName")
.description("Trigger name."),
fieldWithPath("triggers.[].description")
.description("Trigger description, if any."),
fieldWithPath("triggers.[].calendarName")
.description("Trigger's calendar name, if any."),
fieldWithPath("triggers.[].startTime")
.description("Trigger's start time."),
fieldWithPath("triggers.[].endTime")
.description("Trigger's end time."),
fieldWithPath("triggers.[].nextFireTime")
.description("Trigger's next fire time."),
fieldWithPath("triggers.[].previousFireTime").description(
"Trigger's previous fire time, if any."),
fieldWithPath("triggers.[].finalFireTime").description(
"Trigger's final fire time, if any."))));
}

@Configuration
@Import(BaseDocumentationConfiguration.class)
static class TestConfiguration {

@Bean
public QuartzEndpoint endpoint(Scheduler scheduler) {
return new QuartzEndpoint(scheduler);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.quartz;

import org.junit.Test;
import org.quartz.Scheduler;

import org.springframework.boot.actuate.quartz.QuartzEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link QuartzEndpointAutoConfiguration}.
*
* @author Vedran Pavic
*/
public class QuartzEndpointAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(QuartzEndpointAutoConfiguration.class))
.withUserConfiguration(QuartzConfiguration.class);

@Test
public void runShouldHaveEndpointBean() {
this.contextRunner.run(
(context) -> assertThat(context).hasSingleBean(QuartzEndpoint.class));
}

@Test
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean()
throws Exception {
this.contextRunner.withPropertyValues("management.endpoint.quartz.enabled:false")
.run((context) -> assertThat(context)
.doesNotHaveBean(QuartzEndpoint.class));
}

@Configuration
static class QuartzConfiguration {

@Bean
public Scheduler scheduler() {
return mock(Scheduler.class);
}

}

}
4 changes: 4 additions & 0 deletions spring-boot-project/spring-boot-actuator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
<artifactId>liquibase-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
Expand Down
Loading