Comprehensive Guide to Quartz Scheduler in Java: Basics, Usage, and Advanced Topics
This article provides a detailed tutorial on the Quartz scheduling framework for Java, covering its core concepts, basic setup with SimpleTrigger and CronTrigger, advanced features like multiple triggers, bean injection, and persistent storage configuration, complete with code examples and resources.
The article introduces Quartz, a powerful Java scheduling framework ideal for tasks that require dynamic management without distributed needs.
It explains the three core components— Job , Trigger , and Scheduler —using a factory analogy to illustrate their relationships.
Basic usage is demonstrated: adding the spring-boot-starter-quartz dependency, implementing a SimpleJob , creating a Scheduler , and configuring SimpleTrigger for interval execution and CronTrigger for cron‑based schedules. Sample code snippets are provided.
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class SimpleJob implements Job {
@Override
public void execute(JobExecutionContext ctx) {
System.out.println(Thread.currentThread().getName() + "--" +
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
}
}Advanced usage covers:
Binding multiple triggers to a single JobDetail using storeDurably() and addJob() .
Injecting Spring beans into jobs via JobDataMap or a static SpringContextJobUtil utility.
Configuring Quartz persistence with JDBCJobStore , including Maven dependencies, a Spring @Configuration class, and the required SQL schema.
@Configuration
public class SchedulerConfig {
@Bean
public Properties quartzProperties() throws IOException {
// load quartz.properties
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
bean.setQuartzProperties(quartzProperties());
return bean;
}
}The article concludes with links to the GitHub repository, documentation, and additional resources for readers to explore further.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.