DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project

Trending

  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • A Modern Stack for Building Scalable Systems
  • Performance Optimization Techniques for Snowflake on AWS
  • Contextual AI Integration for Agile Product Teams
  1. DZone
  2. Coding
  3. Frameworks
  4. Managing Quartz Using Spring Boot Actuator

Managing Quartz Using Spring Boot Actuator

Learn more about managing Quartz using the Spring Boot Actuator module.

By 
Sathyabodh Mudhol user avatar
Sathyabodh Mudhol
·
Aug. 05, 19 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
36.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we will explore writing custom endpoints for managing Quartz entities like jobs and triggers using the Spring Boot Actuator framework.

What Is Missing?

The Spring Boot Actuator module provides various production-ready endpoints for managing and monitoring application. But strangely enough, it does not have out-of-the-box support for managing Quartz. Quarz is a very popular scheduler and is, for many, the de-facto standard for microservices that require scheduling abilities.

Why Is It Important?

In my experience, I have seen that when products get deployed in production, there might a situation where you want to pause/resume jobs/triggers. It could be because some job is bringing down the whole application or causing high resource usage, which in turn degrades overall performance. We could have tackled this situation by deploy new code but this might take more time or the organization might have a freeze period where no new deployments can happen.

This brings us to a point of requiring a way to manage Quartz jobs/triggers at run-time. This can be done by having application-specific code or leveraging the Spring Boot Actuator framework. Using the Spring Boot Actuator framework makes it easy to integrate with an application using Spring and can have a reusable module to be used across different applications

Quartz Job Endpoint

We will look at how to write a custom endpoint for supporting various operation on Quartz jobs. The below endpoints will be useful:

GET actuator/quartz-jobs?group=&name=
GET actuator/quartz-jobs/{jobgroup}/{jobname}
POST actuator/quartz-jobs/{jobgroup}/{jobname}/resume
POST actuator/quartz-jobs/{jobgroup}/{jobname}/pause


Let's look at the Java code to achieve this goal:

import java.util.Set;
import java.util.stream.Collectors;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.lang.Nullable;

import org.sathyabodh.actuator.quartz.exception.UnsupportStateChangeException;
import org.sathyabodh.actuator.quartz.model.GroupModel;
import org.sathyabodh.actuator.quartz.model.JobDetailModel;
import org.sathyabodh.actuator.quartz.model.JobModel;
import org.sathyabodh.actuator.quartz.service.TriggerModelBuilder;

@Endpoint(id = "quartz-jobs")
public class QuartzJobEndPoint {

private Scheduler scheduler;

private TriggerModelBuilder triggerModelBuilder = new TriggerModelBuilder();

public QuartzJobEndPoint(Scheduler scheduler) {
this.scheduler = scheduler;
}

@ReadOperation
public GroupModel<JobModel> listJobs(@Nullable String group, @Nullable String name) throws SchedulerException {
try {
if (name != null && group != null) {
JobModel model = createJobModel(new JobKey(name, group));
if (model == null) {
return null;
}
GroupModel<JobModel> jobGroupModel = new GroupModel<>();
jobGroupModel.add(group, model);
return jobGroupModel;
}
GroupMatcher<JobKey> jobGroupMatcher = group == null ? GroupMatcher.anyJobGroup()
: GroupMatcher.jobGroupEquals(group);

Set<JobKey> jobKeys = scheduler.getJobKeys(jobGroupMatcher);
if (name != null) {
jobKeys = jobKeys.stream().filter(key -> name.equals(key.getName())).collect(Collectors.toSet());
}
if (jobKeys == null || jobKeys.isEmpty()) {
return null;
}
GroupModel<JobModel> jobGroupModel = new GroupModel<>();
jobKeys.forEach(key -> {
JobModel model = createJobModel(key);
jobGroupModel.add(key.getGroup(), model);
});
return jobGroupModel;
} catch (SchedulerException e) {
throw e;
}
}

@ReadOperation
public JobDetailModel getJobDetail(@Selector String group, @Selector String name) throws SchedulerException {
JobDetail jobDetail = scheduler.getJobDetail(new JobKey(name, group));
JobDetailModel model = new JobDetailModel();
copyJobDetailModel(jobDetail, model);
model.setGroup(jobDetail.getKey().getGroup());
model.setTriggers(triggerModelBuilder.buildTriggerDetailModel(scheduler, jobDetail.getKey()));
return model;
}

private JobModel createJobModel(JobKey key) {
try {
JobDetail jobDetail = scheduler.getJobDetail(key);
if (jobDetail == null) {
return null;
}
JobModel model = new JobModel();
copyJobDetailModel(jobDetail, model);
return model;

} catch (SchedulerException e) {
throw new RuntimeException(e);
}

}

private void copyJobDetailModel(JobDetail jobDetail, JobModel model) {
model.setName(jobDetail.getKey().getName());
model.setDurable(jobDetail.isDurable());
model.setConcurrentDisallowed(jobDetail.isConcurrentExectionDisallowed());
model.setJobClass(jobDetail.getJobClass().getName());
}

@WriteOperation
public boolean modifyJobStatus(@Selector String group, @Selector String name, @Selector String state)
throws SchedulerException {
JobKey jobKey = new JobKey(name, group);
JobDetail detail = scheduler.getJobDetail(jobKey);
if (detail == null)
return false;
else if (QuartzState.PAUSE.equals(state)) {
scheduler.pauseJob(jobKey);
} else if (QuartzState.RESUME.equals(state)) {
scheduler.resumeJob(jobKey);
} else {
throw new UnsupportStateChangeException(String.format("unsupported state change. state:[%s]", state));
}
return true;
}
}
-


Starting from Spring Boot 2.0, writing custom endpoints have changed a lot. They have made the Actuator endpoint technology-independent, i.e. JMX, WebMVC, SpringFlux. Hence, the annotation names are very generic. Please refer to the Spring Actuator documentation for annotation details

To add some specific code when used as an HTTP endpoint, we can write an Extension. This gives us the required hook for our chosen technology. The code below shows a web-specific extension.

import org.quartz.SchedulerException;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;

import org.sathyabodh.actuator.quartz.exception.UnsupportStateChangeException;

@EndpointWebExtension(endpoint = QuartzJobEndPoint.class)
public class QuartzJobEndPointWebExtension {

private QuartzJobEndPoint qurtzJobEndPoint;

public QuartzJobEndPointWebExtension(QuartzJobEndPoint qurtzJobEndPoint) {
this.qurtzJobEndPoint = qurtzJobEndPoint;
}

@WriteOperation
public WebEndpointResponse<?> modifyJobStatus(@Selector String group, @Selector String name, @Selector String state)
throws SchedulerException {
try {
boolean isSucess = qurtzJobEndPoint.modifyJobStatus(group, name, state);
int status = isSucess ? WebEndpointResponse.STATUS_OK : WebEndpointResponse.STATUS_NOT_FOUND;
return new WebEndpointResponse<>(status);
} catch (UnsupportStateChangeException e) {
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
}
}
}


Quartz Trigger Endpoint

This endpoint is very similar to the Quartz job endpoint but works on trigger objects. The below endpoints will be useful:

GET /actuator/quartz-triggers?group=&name=
POST /actuator/quartz-triggers/{group}/{name}/pause
POST /actuator/quartz-triggers/{group}/{name}/resume


Let's see the code:

import java.util.Set;
import java.util.stream.Collectors;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.lang.Nullable;

import org.sathyabodh.actuator.quartz.exception.UnsupportStateChangeException;
import org.sathyabodh.actuator.quartz.model.GroupModel;
import org.sathyabodh.actuator.quartz.model.TriggerDetailModel;
import org.sathyabodh.actuator.quartz.service.TriggerModelBuilder;

@Endpoint(id = "quartz-triggers")
public class QuartzTriggerEndPoint {
private Scheduler scheduler;
private TriggerModelBuilder triggerModelBuilder = new TriggerModelBuilder();

public QuartzTriggerEndPoint(Scheduler scheduler) {
this.scheduler = scheduler;
}

@ReadOperation
public GroupModel<TriggerDetailModel> listTriggers(@Nullable String group, @Nullable String name)
throws SchedulerException {
try {
if (name != null && group != null) {
TriggerDetailModel model = triggerModelBuilder.buildTriggerDetailModel(scheduler,
new TriggerKey(name, group));
if (model == null) {
return null;
}
GroupModel<TriggerDetailModel> groupModel = new GroupModel<>();
groupModel.add(group, model);
return groupModel;
}
GroupMatcher<TriggerKey> triggerGroupMatcher = group == null ? GroupMatcher.anyTriggerGroup()
: GroupMatcher.triggerGroupEquals(group);
Set<TriggerKey> triggerKeys = scheduler.getTriggerKeys(triggerGroupMatcher);
if (name != null) {
triggerKeys = triggerKeys.stream().filter(key -> name.equals(key.getName()))
.collect(Collectors.toSet());
}
if (triggerKeys == null || triggerKeys.isEmpty()) {
return null;
}
GroupModel<TriggerDetailModel> groupModel = new GroupModel<>();
triggerKeys.forEach(key -> addTriggerDetailModel(groupModel, key));
return groupModel;
} catch (SchedulerException e) {
throw e;
}
}

private void addTriggerDetailModel(GroupModel<TriggerDetailModel> groupModel, TriggerKey key) {
TriggerDetailModel model;
try {
model = triggerModelBuilder.buildTriggerDetailModel(scheduler, key);
groupModel.add(key.getGroup(), model);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}

@WriteOperation
public boolean modifyTriggerStatus(@Selector String group, @Selector String name, @Selector String state)
throws SchedulerException {
TriggerKey triggerKey = new TriggerKey(name, group);
Trigger trigger = scheduler.getTrigger(triggerKey);
if (trigger == null)
return false;
else if (QuartzState.PAUSE.equals(state)) {
scheduler.pauseTrigger(triggerKey);
} else if (QuartzState.RESUME.equals(state)) {
scheduler.resumeTrigger(triggerKey);
} else {
throw new UnsupportStateChangeException(String.format("unsupported state change. state:[%s]", state));
}
return true;
}

@WriteOperation
public boolean modifyTriggersStatus(@Selector String group, @Selector String state) throws SchedulerException {
GroupMatcher<TriggerKey> triggerGroupMatcher = GroupMatcher.triggerGroupEquals(group);
Set<TriggerKey> triggerKeys = scheduler.getTriggerKeys(triggerGroupMatcher);
if (triggerKeys == null || triggerKeys.isEmpty()) {
return false;
} else if (QuartzState.PAUSE.equals(state)) {
scheduler.pauseTriggers(triggerGroupMatcher);
} else if (QuartzState.RESUME.equals(state)) {
scheduler.resumeTriggers((triggerGroupMatcher));
} else {
throw new UnsupportStateChangeException(String.format("unsupported state change. state:[%s]", state));
}
return true;
}
}
-


And its web extension:

import org.quartz.SchedulerException;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;

import org.sathyabodh.actuator.quartz.exception.UnsupportStateChangeException;

@EndpointWebExtension(endpoint = QuartzTriggerEndPoint.class)
public class QuartzTriggerEndPointWebExtension {

private QuartzTriggerEndPoint qurtzTriggerEndPoint;

public QuartzTriggerEndPointWebExtension(QuartzTriggerEndPoint qurtzTriggerEndPoint) {
this.qurtzTriggerEndPoint = qurtzTriggerEndPoint;
}

@WriteOperation
public WebEndpointResponse<?> modifyTriggerStatus(@Selector String group, @Selector String name,
@Selector String state) throws SchedulerException {
try {
boolean isSucess = qurtzTriggerEndPoint.modifyTriggerStatus(group, name, state);
int status = isSucess ? WebEndpointResponse.STATUS_OK : WebEndpointResponse.STATUS_NOT_FOUND;
return new WebEndpointResponse<>(status);
} catch (UnsupportStateChangeException e) {
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
}
}
}
-


Enabling Quartz Endpoints

These new endpoints can be enabled similar to out-of-the-box endpoints provided by the Spring Boot Actuator:

management.endpoints.web.exposure.include=quartz-jobs,quartz-triggers


These endpoint names are taken from id value specified in @EndPoint annotation.

Bonus: Spring Boot Autoconfigure Module

Now, we have looked at how to implement Quartz endpoint for the actuator. If we put the @Component annotation on top of each class, Spring will scan and register them. Now, Quartz can be managed just like any other entities supported by the out-of-the-box Spring Actuator. But we can do better. We can package all of these as an independent module and add autoconfigure feature. This makes it like any first-class Spring Boot autoconfigure module. When any application includes this jar as a dependency, and the Quartz scheduler dependency is also present, it will auto-register these endpoints.

Below is code for the autoconfigure feature:

import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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;

import org.sathyabodh.actuator.quartz.QuartzTriggerEndPoint;
import org.sathyabodh.actuator.quartz.QuartzTriggerEndPointWebExtension;

@Configuration
@ConditionalOnClass({ Scheduler.class, SchedulerFactory.class })
@AutoConfigureAfter(QuartzAutoConfiguration.class)
public class QuartzEndPointAutoConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public QuartzTriggerEndPoint quartzTriggerEndPoint(Scheduler scheduler) {
return new QuartzTriggerEndPoint(scheduler);
}

@Bean
@ConditionalOnBean(QuartzTriggerEndPoint.class)
public QuartzTriggerEndPointWebExtension quartzTriggerEndPointWebExtension(
QuartzTriggerEndPoint quartzTriggerEndPoint) {
return new QuartzTriggerEndPointWebExtension(quartzTriggerEndPoint);
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public QuartzTriggerEndPoint quartzTriggerEndPoint(Scheduler scheduler) {
return new QuartzTriggerEndPoint(scheduler);
}

@Bean
@ConditionalOnBean(QuartzTriggerEndPoint.class)
public QuartzTriggerEndPointWebExtension quartzTriggerEndPointWebExtension(
QuartzTriggerEndPoint quartzTriggerEndPoint) {
return new QuartzTriggerEndPointWebExtension(quartzTriggerEndPoint);
}
}
-


The last thing left now is to configure the class name in src/main/resources/META-INF/spring.factories file.

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.sathyabodh.actuator.autoconfigure.quartz.QurtzEndPointAutoConfiguration


Summary

Now, we have seen how to implement Quartz endpoints using Spring Actuator and also make the Spring Boot autoconfigure module for ease of use.

Spring Framework Spring Boot Quartz (scheduler)

Opinions expressed by DZone contributors are their own.

Related

  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!