Skip to content

Commit 4f58f40

Browse files
committed
quartz
1 parent f516120 commit 4f58f40

File tree

5 files changed

+193
-19
lines changed

5 files changed

+193
-19
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.ylc.note.quartz.boot;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.quartz.CronTrigger;
5+
import org.quartz.SchedulerException;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.CommandLineRunner;
8+
import org.springframework.core.annotation.Order;
9+
import org.springframework.stereotype.Component;
10+
import org.ylc.note.quartz.entity.ScheduleJob;
11+
import org.ylc.note.quartz.service.QuartzService;
12+
import org.ylc.note.quartz.service.ScheduleJobService;
13+
14+
import java.util.List;
15+
16+
/**
17+
* 代码全万行,注释第一行
18+
* 注释不规范,同事泪两行
19+
* <p>
20+
* 项目启动后初始化定时任务
21+
*
22+
* @author YuLc
23+
* @version 1.0.0
24+
* @date 2020-09-13
25+
*/
26+
@Slf4j
27+
@Component
28+
@Order(2)
29+
public class ScheduleJobStartupBoot implements CommandLineRunner {
30+
31+
@Autowired
32+
private QuartzService quartzService;
33+
34+
@Autowired
35+
private ScheduleJobService scheduleJobService;
36+
37+
@Override
38+
public void run(String... args) {
39+
List<ScheduleJob> jobs = scheduleJobService.list();
40+
41+
for (ScheduleJob job : jobs) {
42+
43+
CronTrigger cronTrigger;
44+
try {
45+
cronTrigger = quartzService.getCronTrigger(job.getId());
46+
} catch (SchedulerException e) {
47+
log.error("获取触发器失败,JOB:【{}】,{}", job.getId(), e.getMessage());
48+
continue;
49+
}
50+
// 如果不存在,则创建
51+
if (cronTrigger == null) {
52+
quartzService.createJob(job);
53+
} else {
54+
quartzService.modifyJob(job);
55+
}
56+
57+
}
58+
}
59+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.ylc.note.quartz.entity;
2+
3+
import org.quartz.JobExecutionContext;
4+
import org.quartz.JobExecutionException;
5+
import org.springframework.scheduling.quartz.QuartzJobBean;
6+
7+
/**
8+
* 代码全万行,注释第一行
9+
* 注释不规范,同事泪两行
10+
* <p>
11+
* 执行定时任务,记录日志
12+
*
13+
* @author YuLc
14+
* @version 1.0.0
15+
* @date 2020-09-13
16+
*/
17+
public class QuartzScheduleJob extends QuartzJobBean {
18+
19+
@Override
20+
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
21+
ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("JOB_PARAM_KEY");
22+
}
23+
}

quartz/src/main/java/org/ylc/note/quartz/entity/ScheduleJob.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.Getter;
44
import lombok.Setter;
5+
import lombok.ToString;
56

67
import javax.persistence.*;
78
import java.time.LocalDateTime;
@@ -18,6 +19,7 @@
1819
*/
1920
@Getter
2021
@Setter
22+
@ToString
2123
@Entity
2224
@Table(name = "schedule_job")
2325
public class ScheduleJob {
@@ -26,15 +28,15 @@ public class ScheduleJob {
2628
@GeneratedValue(strategy = GenerationType.IDENTITY)
2729
private Long id;
2830

29-
@Column(name = "class_name", nullable = false)
31+
@Column(name = "class_name")
3032
private String className;
3133

32-
@Column(name = "method_name", nullable = false)
34+
@Column(name = "method_name")
3335
private String methodName;
3436

3537
private String parameter;
3638

37-
@Column(name = "cron_expression", nullable = false)
39+
@Column(name = "cron_expression")
3840
private String cronExpression;
3941

4042
private String status;
@@ -44,6 +46,6 @@ public class ScheduleJob {
4446

4547
private String remark;
4648

47-
@Column(name = "create_time", nullable = false)
49+
@Column(name = "create_time")
4850
private LocalDateTime createTime;
4951
}
Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.ylc.note.quartz.service;
22

3-
import org.quartz.Scheduler;
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.quartz.*;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.stereotype.Component;
7+
import org.ylc.note.quartz.constant.EnumConstant;
8+
import org.ylc.note.quartz.entity.QuartzScheduleJob;
9+
import org.ylc.note.quartz.entity.ScheduleJob;
610

711
/**
812
* 代码全万行,注释第一行
@@ -12,30 +16,115 @@
1216
* @version 1.0.0
1317
* @date 2020-09-11
1418
*/
19+
@Slf4j
1520
@Component
1621
public class QuartzService {
1722

1823
@Autowired
1924
private Scheduler scheduler;
2025

21-
public void createJob() {
26+
private final static String JOB_NAME = "TASK_";
2227

28+
/**
29+
* 获取jobKey
30+
*/
31+
public static JobKey getJobKey(Long jobId) {
32+
return JobKey.jobKey(JOB_NAME + jobId);
2333
}
2434

25-
public void pauseJob() {
35+
/**
36+
* 获取触发器key
37+
*/
38+
public static TriggerKey getTriggerKey(Long jobId) {
39+
return TriggerKey.triggerKey(JOB_NAME + jobId);
40+
}
41+
42+
/**
43+
* 获取触发器
44+
*/
45+
public CronTrigger getCronTrigger(Long jobId) throws SchedulerException {
46+
return (CronTrigger) scheduler.getTrigger(getTriggerKey(jobId));
47+
}
48+
49+
public void createJob(ScheduleJob job) {
50+
try {
51+
//构建job信息
52+
JobDetail jobDetail = JobBuilder.newJob(QuartzScheduleJob.class).withIdentity(getJobKey(job.getId())).build();
53+
54+
//表达式调度构建器
55+
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression())
56+
.withMisfireHandlingInstructionDoNothing();
57+
58+
//按新的cronExpression表达式构建一个新的trigger
59+
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(job.getId())).withSchedule(scheduleBuilder).build();
60+
61+
//放入参数,运行时的方法可以获取
62+
jobDetail.getJobDataMap().put("JOB_PARAM_KEY", job);
63+
// 生成定时任务
64+
scheduler.scheduleJob(jobDetail, trigger);
2665

66+
// 暂停任务
67+
if (job.getStatus().equals(EnumConstant.JobStatus.PAUSED.getValue())) {
68+
pauseJob(job.getId());
69+
}
70+
} catch (SchedulerException e) {
71+
log.error("创建定时任务失败:{}", e.getMessage());
72+
}
2773
}
2874

29-
public void restoreJob() {
75+
public void pauseJob(long jobId) {
76+
try {
77+
scheduler.pauseJob(getJobKey(jobId));
78+
} catch (SchedulerException e) {
79+
log.error("暂停任务失败:{}", e.getMessage());
80+
}
81+
}
3082

83+
public void resumeJob(long jobId) {
84+
try {
85+
scheduler.resumeJob(getJobKey(jobId));
86+
} catch (SchedulerException e) {
87+
log.error("恢复任务失败:{}", e.getMessage());
88+
}
3189
}
3290

33-
public void modifyJob() {
91+
public void modifyJob(ScheduleJob job) {
92+
try {
93+
TriggerKey triggerKey = getTriggerKey(job.getId());
94+
95+
//表达式调度构建器
96+
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression())
97+
.withMisfireHandlingInstructionDoNothing();
98+
99+
CronTrigger trigger = getCronTrigger(job.getId());
100+
101+
//按新的cronExpression表达式重新构建trigger
102+
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
103+
//参数
104+
trigger.getJobDataMap().put("JOB_PARAM_KEY", job);
105+
// 重置定时任务
106+
scheduler.rescheduleJob(triggerKey, trigger);
34107

108+
// 暂停任务
109+
if (job.getStatus().equals(EnumConstant.JobStatus.PAUSED.getValue())) {
110+
pauseJob(job.getId());
111+
}
112+
} catch (SchedulerException e) {
113+
log.error("更新定时任务失败:{}", e.getMessage());
114+
}
35115
}
36116

37-
public void removeJob() {
117+
public void removeJob(long jobId) {
118+
try {
119+
scheduler.deleteJob(getJobKey(jobId));
120+
} catch (SchedulerException e) {
121+
log.error("删除任务失败:{}", e.getMessage());
122+
}
123+
}
124+
125+
public void run(ScheduleJob job) {
38126

39127
}
40128

129+
41130
}

quartz/src/main/java/org/ylc/note/quartz/service/ScheduleJobService.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,23 @@ public void createJob(ScheduleJob job) {
3434
job.setStatus(EnumConstant.JobStatus.NORMAL.getCode());
3535
job.setCreateTime(LocalDateTime.now());
3636
scheduleJobRepository.save(job);
37-
quartzService.createJob();
37+
38+
quartzService.createJob(job);
3839
}
3940

40-
public void pauseJob() {
41-
quartzService.pauseJob();
41+
public void modifyJob(ScheduleJob job) {
42+
quartzService.modifyJob(job);
4243
}
4344

44-
public void restoreJob() {
45-
quartzService.restoreJob();
45+
public void pauseJob(Long jobId) {
46+
quartzService.pauseJob(jobId);
4647
}
4748

48-
public void modifyJob() {
49-
quartzService.modifyJob();
49+
public void resumeJob(long jobId) {
50+
quartzService.resumeJob(jobId);
5051
}
5152

52-
public void removeJob() {
53-
quartzService.restoreJob();
53+
public void removeJob(long jobId) {
54+
quartzService.removeJob(jobId);
5455
}
5556
}

0 commit comments

Comments
 (0)