服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot+Quartz实现任务调度的示例代码

springboot+Quartz实现任务调度的示例代码

2021-03-30 11:45殷天文 Java教程

本篇文章主要介绍了springboot + Quartz 实现任务调度的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

在spring框架中通过 @Schedule 可以实现定时任务,通过该注解 cron 的属性描述的规则,spring会去调用这个方法。

spring已经简单粗暴的实现了定时任务,为什么要使用Quartz

如果你现在有很多个定时任务,规则不同,例如:

  1. 想每月25号,信用卡自动还款
  2. 想每年4月1日自己给当年暗恋女神发一封匿名贺卡
  3. 想每隔1小时,备份一下自己的爱情动作片 学习笔记到云盘

maven 依赖

?
1
2
3
4
5
6
7
8
9
10
<dependency>
   <groupId>org.quartz-scheduler</groupId>
   <artifactId>quartz</artifactId>
   <version>2.2.1</version>
 </dependency>
 <dependency>
   <groupId>org.quartz-scheduler</groupId>
   <artifactId>quartz-jobs</artifactId>
   <version>2.2.1</version>
 </dependency>

以下是bootdo开源学习框架的源码

我并没有用到全部的字段,思路是先查询DB,

封装以下两个对象

  1. JobDetail 负责存放 job 所需要的数据
  2. Trigger 设置 job 的 key,规则(cron)何时开启任务等属性

当触发条件满足时,会根据所设置的beanClass 找到该类(必须实现org.quartz.Job), 这时可以取出JobDetail 中的数据,执行具体业务逻辑

?
1
2
3
4
5
6
7
@Component
public class WelcomeJob implements Job{
  @Override
  public void execute(JobExecutionContext arg0) throws JobExecutionException {
    //你的业务逻辑
  }
}

表结构

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CREATE TABLE `sys_task` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `cronExpression` varchar(255) DEFAULT NULL COMMENT 'cron表达式',
 `methodName` varchar(255) DEFAULT NULL COMMENT '任务调用的方法名',
 `isConcurrent` varchar(255) DEFAULT NULL COMMENT '任务是否有状态',
 `description` varchar(255) DEFAULT NULL COMMENT '任务描述',
 `updateBy` varchar(64) DEFAULT NULL COMMENT '更新者',
 `beanClass` varchar(255) DEFAULT NULL COMMENT '任务执行时调用哪个类的方法 包名+类名',
 `createDate` datetime DEFAULT NULL COMMENT '创建时间',
 `jobStatus` varchar(255) DEFAULT NULL COMMENT '任务状态',
 `jobGroup` varchar(255) DEFAULT NULL COMMENT '任务分组',
 `updateDate` datetime DEFAULT NULL COMMENT '更新时间',
 `createBy` varchar(64) DEFAULT NULL COMMENT '创建者',
 `springBean` varchar(255) DEFAULT NULL COMMENT 'Spring bean',
 `jobName` varchar(255) DEFAULT NULL COMMENT '任务名',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

配置类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.IOException;
import java.util.Properties;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import com.txgl.common.quartz.factory.JobFactory;
@Configuration
public class QuartzConfigration {
 
  @Autowired
  JobFactory jobFactory;
 
  @Bean
  public SchedulerFactoryBean schedulerFactoryBean() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    try {
      schedulerFactoryBean.setOverwriteExistingJobs(true);
      schedulerFactoryBean.setQuartzProperties(quartzProperties());
      schedulerFactoryBean.setJobFactory(jobFactory);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return schedulerFactoryBean;
  }
 
  // 指定quartz.properties
  @Bean
  public Properties quartzProperties() throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/config/quartz.properties"));
    propertiesFactoryBean.afterPropertiesSet();
    return propertiesFactoryBean.getObject();
  }
 
  // 创建schedule
  @Bean(name = "scheduler")
  public Scheduler scheduler() {
    return schedulerFactoryBean().getScheduler();
  }
}

QuartzManager的代码是关键,通过注入Scheduler 对任务进行操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.DateBuilder;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.bootdo.common.domain.ScheduleJob;
import com.bootdo.common.quartz.factory.*;
import com.bootdo.common.utils.SpringContextHolder;;
 
/**
 *
 *
 * @title: QuartzManager.java
 * @description: 计划任务管理
 *
 */
@Service
public class QuartzManager {
  public final Logger log = Logger.getLogger(this.getClass());
  // private SchedulerFactoryBean schedulerFactoryBean
  // =SpringContextHolder.getBean(SchedulerFactoryBean.class);
  // @Autowired
  // @Qualifier("schedulerFactoryBean")
  // private SchedulerFactoryBean schedulerFactoryBean;
  @Autowired
  private Scheduler scheduler;
 
  /**
   * 添加任务
   *
   * @param scheduleJob
   * @throws SchedulerException
   */
  
  public void addJob(ScheduleJob job) {
    try {
      // 创建jobDetail实例,绑定Job实现类
      // 指明job的名称,所在组的名称,以及绑定job类
      Class<? extends Job> jobClass = (Class<? extends Job>) (Class.forName(job.getBeanClass()).newInstance()
          .getClass());
      JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(job.getJobName(), job.getJobGroup())// 任务名称和组构成任务key
          .build();
      // 定义调度触发规则
      // 使用cornTrigger规则
      Trigger trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())// 触发器key
          .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
          .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression())).startNow().build();
      // 把作业和触发器注册到任务调度
      scheduler.scheduleJob(jobDetail, trigger);
      // 启动
      if (!scheduler.isShutdown()) {
        scheduler.start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
// public void addJob(ScheduleJob job) throws SchedulerException {
//   if (job == null || !ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) {
//     return;
//   }
//
//   TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
//
//   CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//
//   // 不存在,创建一个
//
//   if (null == trigger) {
//     Class<? extends Job> clazz = ScheduleJob.CONCURRENT_IS.equals(job.getIsConcurrent())
//         ? QuartzJobFactory.class
//         : QuartzJobFactoryDisallowConcurrentExecution.class;
//
//     JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getJobName(), job.getJobGroup()).build();
//
//     jobDetail.getJobDataMap().put("scheduleJob", job);
//
//     CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//
//     trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())
//         .withSchedule(scheduleBuilder).build();
//
//     scheduler.scheduleJob(jobDetail, trigger);
//   } else {
//     // Trigger已存在,那么更新相应的定时设置
//
//     CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//
//     // 按新的cronExpression表达式重新构建trigger
//
//     trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//
//     // 按新的trigger重新设置job执行
//
//     scheduler.rescheduleJob(triggerKey, trigger);
//   }
// }
 
  /**
   * 获取所有计划中的任务列表
   *
   * @return
   * @throws SchedulerException
   */
  public List<ScheduleJob> getAllJob() throws SchedulerException {
    GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
    Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
    List<ScheduleJob> jobList = new ArrayList<ScheduleJob>();
    for (JobKey jobKey : jobKeys) {
      List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
      for (Trigger trigger : triggers) {
        ScheduleJob job = new ScheduleJob();
        job.setJobName(jobKey.getName());
        job.setJobGroup(jobKey.getGroup());
        job.setDescription("触发器:" + trigger.getKey());
        Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
        job.setJobStatus(triggerState.name());
        if (trigger instanceof CronTrigger) {
          CronTrigger cronTrigger = (CronTrigger) trigger;
          String cronExpression = cronTrigger.getCronExpression();
          job.setCronExpression(cronExpression);
        }
        jobList.add(job);
      }
    }
    return jobList;
  }
 
  /**
   * 所有正在运行的job
   *
   * @return
   * @throws SchedulerException
   */
  public List<ScheduleJob> getRunningJob() throws SchedulerException {
    List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
    List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(executingJobs.size());
    for (JobExecutionContext executingJob : executingJobs) {
      ScheduleJob job = new ScheduleJob();
      JobDetail jobDetail = executingJob.getJobDetail();
      JobKey jobKey = jobDetail.getKey();
      Trigger trigger = executingJob.getTrigger();
      job.setJobName(jobKey.getName());
      job.setJobGroup(jobKey.getGroup());
      job.setDescription("触发器:" + trigger.getKey());
      Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
      job.setJobStatus(triggerState.name());
      if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = (CronTrigger) trigger;
        String cronExpression = cronTrigger.getCronExpression();
        job.setCronExpression(cronExpression);
      }
      jobList.add(job);
    }
    return jobList;
  }
 
  /**
   * 暂停一个job
   *
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void pauseJob(ScheduleJob scheduleJob) throws SchedulerException {
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    scheduler.pauseJob(jobKey);
  }
 
  /**
   * 恢复一个job
   *
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void resumeJob(ScheduleJob scheduleJob) throws SchedulerException {
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    scheduler.resumeJob(jobKey);
  }
 
  /**
   * 删除一个job
   *
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void deleteJob(ScheduleJob scheduleJob) throws SchedulerException {
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    scheduler.deleteJob(jobKey);
 
  }
 
  /**
   * 立即执行job
   *
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void runAJobNow(ScheduleJob scheduleJob) throws SchedulerException {
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    scheduler.triggerJob(jobKey);
  }
 
  /**
   * 更新job时间表达式
   *
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void updateJobCron(ScheduleJob scheduleJob) throws SchedulerException {
 
    TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
 
    CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
 
    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
 
    trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
 
    scheduler.rescheduleJob(triggerKey, trigger);
  }
}

service实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import com.bootdo.common.config.Constant;
import com.bootdo.common.dao.TaskDao;
import com.bootdo.common.domain.ScheduleJob;
import com.bootdo.common.domain.TaskDO;
import com.bootdo.common.quartz.utils.QuartzManager;
import com.bootdo.common.service.JobService;
import com.bootdo.common.utils.ScheduleJobUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class JobServiceImpl implements JobService {
 
  @Autowired
  private TaskDao taskScheduleJobMapper;
 
  @Autowired
  QuartzManager quartzManager;
 
  @Override
  public TaskDO get(Long id) {
    return taskScheduleJobMapper.get(id);
  }
 
  @Override
  public List<TaskDO> list(Map<String, Object> map) {
    return taskScheduleJobMapper.list(map);
  }
 
  @Override
  public int count(Map<String, Object> map) {
    return taskScheduleJobMapper.count(map);
  }
 
  @Override
  public int save(TaskDO taskScheduleJob) {
    return taskScheduleJobMapper.save(taskScheduleJob);
  }
 
  @Override
  public int update(TaskDO taskScheduleJob) {
    return taskScheduleJobMapper.update(taskScheduleJob);
  }
 
  @Override
  public int remove(Long id) {
    try {
      TaskDO scheduleJob = get(id);
      quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
      return taskScheduleJobMapper.remove(id);
    } catch (SchedulerException e) {
      e.printStackTrace();
      return 0;
    }
 
  }
 
  @Override
  public int batchRemove(Long[] ids) {
    for (Long id : ids) {
      try {
        TaskDO scheduleJob = get(id);
        quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
      } catch (SchedulerException e) {
        e.printStackTrace();
        return 0;
      }
    }
    return taskScheduleJobMapper.batchRemove(ids);
  }
 
  @Override
  public void initSchedule() throws SchedulerException {
    // 这里获取任务信息数据
    List<TaskDO> jobList = taskScheduleJobMapper.list(new HashMap<String, Object>(16));
    for (TaskDO scheduleJob : jobList) {
      if ("1".equals(scheduleJob.getJobStatus())) {
        ScheduleJob job = ScheduleJobUtils.entityToData(scheduleJob);
        quartzManager.addJob(job);
      }
 
    }
  }
 
  @Override
  public void changeStatus(Long jobId, String cmd) throws SchedulerException {
    TaskDO scheduleJob = get(jobId);
    if (scheduleJob == null) {
      return;
    }
    if (Constant.STATUS_RUNNING_STOP.equals(cmd)) {
      quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
      scheduleJob.setJobStatus(ScheduleJob.STATUS_NOT_RUNNING);
    } else {
      if (!Constant.STATUS_RUNNING_START.equals(cmd)) {
      } else {
        scheduleJob.setJobStatus(ScheduleJob.STATUS_RUNNING);
        quartzManager.addJob(ScheduleJobUtils.entityToData(scheduleJob));
      }
    }
    update(scheduleJob);
  }
 
  @Override
  public void updateCron(Long jobId) throws SchedulerException {
    TaskDO scheduleJob = get(jobId);
    if (scheduleJob == null) {
      return;
    }
    if (ScheduleJob.STATUS_RUNNING.equals(scheduleJob.getJobStatus())) {
      quartzManager.updateJobCron(ScheduleJobUtils.entityToData(scheduleJob));
    }
    update(scheduleJob);
  }
}

启动一个监听去初始化Quartz

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.bootdo.common.quartz.utils.QuartzManager;
import com.bootdo.common.service.JobService;
@Component
@Order(value = 1)
public class ScheduleJobInitListener implements CommandLineRunner {
 
  @Autowired
  JobService scheduleJobService;
 
  @Autowired
  QuartzManager quartzManager;
 
  @Override
  public void run(String... arg0) throws Exception {
    try {
      scheduleJobService.initSchedule();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.jianshu.com/p/800053e50ddf

延伸 · 阅读

精彩推荐