Configurable Java Scheduled Tasks with Quartz
This guide explains how to build a configurable Java scheduling microservice using the Quartz framework, covering task entity CRUD integration, a utility class for creating, updating, pausing, and deleting jobs, and concrete code examples that demonstrate building JobDetails, CronTriggers, and invoking the scheduler.
Scheduled tasks appear in every system; this article introduces a configurable and controllable approach using the Quartz job‑scheduling framework written in Java.
We treat scheduled tasks as a dedicated microservice module whose responsibility is to run jobs. Quartz provides the core scheduling capabilities, and a concrete business class implements org.quartz.Job by overriding the execute method to contain the task logic.
The implementation process includes CRUD operations on task entities, which add the corresponding job class to the scheduler. A utility class encapsulates methods for creating, updating, triggering immediately, pausing, resuming, and deleting jobs.
Creating a new scheduled job involves building a JobDetail, configuring a CronScheduleBuilder with the desired cron expression, constructing a CronTrigger, and registering both with the scheduler. If the job’s status indicates pause, the utility pauses it.
public static void createScheduleJob(Scheduler scheduler, ScheduleJob scheduleJob) throws BaseException {
try {
JobDetail jobDetail = JobBuilder.newJob(ScheduleJobBean.class)
.withIdentity(getJobKey(scheduleJob.getJobId()))
.build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression())
.withMisfireHandlingInstructionDoNothing();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(getTriggerKey(scheduleJob.getJobId()))
.withSchedule(scheduleBuilder)
.build();
jobDetail.getJobDataMap().put(ScheduleJob.JOB_PARAM_KEY, scheduleJob);
scheduler.scheduleJob(jobDetail, trigger);
if (scheduleJob.getStatus() == ScheduleStatus.PAUSE.getValue()) {
pauseJob(scheduler, scheduleJob.getJobId());
}
} catch (SchedulerException e) {
logger.error("Create scheduled task exception", e);
throw new BaseException("Failed to create scheduled task");
}
}After encapsulating the utility, the calling code sets the job status, inserts the task record into the database, and invokes the utility to register the job with the scheduler.
scheduleJob.setStatus(ScheduleStatus.NORMAL.getValue());
if (scheduleJobMapper.insertSelective(scheduleJob) > 0) {
ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
return scheduleJob.getJobId();
}The addition part of the scheduled‑task module is now complete; subsequent operations such as modification follow a similar pattern.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
