java-quartz

Java Quartz

简介

Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs.

添加依赖

1
2
3
4
5
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>

每隔多长时间运行一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
try {
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();

Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever())
.build();

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(job, trigger);

scheduler.start();
} catch (SchedulerException se) {
se.printStackTrace();
}

Trigger does not reference given job

1
2
3
org.quartz.SchedulerException: Trigger does not reference given job!
at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:869)
at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:249)

一个 Job 可以有多个 Trigger,但是多个 Job 不能对应同一个 Trigger

org.quartz.ObjectAlreadyExistsException

1
2
3
4
5
org.quartz.ObjectAlreadyExistsException: Unable to store Trigger with name: 'QuartzQueryTaskTrigger' and group: 'QuartzQueryTaskGroup', because one already exists with this identification.
at org.quartz.simpl.RAMJobStore.storeTrigger(RAMJobStore.java:415)
at org.quartz.simpl.RAMJobStore.storeJobAndTrigger(RAMJobStore.java:252)
at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:886)
at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:249)

Job

Job 必须提供一个无参的公共构造函数

嵌套类

配置

推荐文章