Springcloud项目集成Quartz定时任务框架,实现批…

2020-01-10 16:01:57来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Springcloud项目集成Quartz定时任务框架,实现批处理功能

一、Quartz简介

    主要做定时任务,即:在指定时间点或时间段,执行某项任务,可设置执行次数、时间间隔等。

二、Springcloud简介

    对比传统的、庞大的、复杂的、以ssm或ssh为框架的web项目而言,springcloud显得格外轻巧,不错乱,易管理,至少模块清晰,功能明确。

三、Springcloud集成quartz

    就个人理解,quartz可集成到某个功能模块,也可以单独做一个服务(功能模块),本文介绍的就是单独作为一个服务进行集成的(未开启集群)。

    第一步:在已有的父工程下新建一个子工程(可有可无)

    第二步:依赖

?    ?

    第三步:配置

 1 /**
 2 * @Title: MyJobFactory.java
 3 * @Package com.ewp.data.controller.quartz
 4 * @Description: 解决不能spring注入bean的问题
 5 * @author zxj
 6 * @date 2018年2月26日
 7 * @version V1.0
 8 */
 9 package com.ewp.data.config;
10 
11 import org.quartz.spi.TriggerFiredBundle;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
14 import org.springframework.scheduling.quartz.AdaptableJobFactory;
15 import org.springframework.stereotype.Component;
16 
17 /**
18  * @ClassName: MyJobFactory
19  * @Description: 解决不能spring注入bean的问题
20  * @author zxj
21  * @date 2018年2月26日
22  *
23  */
24 @Component 
25 public class MyJobFactory extends AdaptableJobFactory {
26     @Autowired  
27     private AutowireCapableBeanFactory capableBeanFactory;  
28      
29     @Override  
30     protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {  
31       Object jobInstance = super.createJobInstance(bundle);  
32       capableBeanFactory.autowireBean(jobInstance); 
33       return jobInstance;  
34     }  
35 }
 1  
 2 import java.io.IOException;
 3 import java.util.Properties;
 4  
 5 import org.quartz.Scheduler;
 6 import org.quartz.SchedulerException;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.beans.factory.annotation.Value;
 9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
12  
13 /**
14  * 
15 * @ClassName: QuartzConfig
16 * @Description: 配置任务调度中心(服务启动时启动)
17 * @author zxj
18 * @date 2018年2月26日
19 *
20  */
21 @Configuration
22 public class QuartzConfig {
23     
24     @Autowired  
25         private MyJobFactory myFactory;  //自定义的factory  
26     
27     @Value("${spring.datasource.url}")
28     private String url;// 数据源地址
29     
30     @Value("${spring.datasource.username}")
31     private String userName;// 用户名
32     
33     @Value("${spring.datasource.password}")
34     private String password;// 密码
35     
36        @Bean  
37        public SchedulerFactoryBean schedulerFactoryBean() {  
38           SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();  
39           try {  
40             schedulerFactoryBean.setQuartzProperties(quartzProperties());  
41             schedulerFactoryBean.setJobFactory(myFactory); //指向自建的调度工厂,用于解决方法类无法注入的问题 
42           } catch (IOException e) {  
43             e.printStackTrace();  
44           }  
45             return schedulerFactoryBean;  
46         }  
47     
48     @Bean
49     public Scheduler scheduler() throws IOException, SchedulerException {
50         Scheduler scheduler = schedulerFactoryBean().getScheduler();
51         scheduler.start();// 服务启动shi
52         return scheduler;
53     }
54     /**
55      * 
56     * @Title: quartzProperties
57     * @Description: 设置quartz属性
58     * @param @return
59     * @param @throws IOException    参数
60     * @return Properties    返回类型
61     * @throws
62      */
63     public Properties quartzProperties() throws IOException {
64         Properties prop = new Properties();
65         prop.put("org.quartz.scheduler.instanceName", "quartzScheduler");// 调度器的实例名
66         prop.put("org.quartz.scheduler.instanceId", "AUTO");// 实例的标识
67             prop.put("org.quartz.scheduler.skipUpdateCheck", "true");// 检查quartz是否有版本更新(true 不检查)
68             prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
69         prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
70         prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");// 表名前缀
71         prop.put("org.quartz.jobStore.isClustered", "false");// 集群开关
72         prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");// 线程池的名字
73             prop.put("org.quartz.threadPool.threadCount", "10");// 指定线程数量
74             prop.put("org.quartz.threadPool.threadPriority", "5");// 线程优先级(1-10)默认为5
75             prop.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
76             prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
77         
78             prop.put("org.quartz.dataSource.quartzDataSource.driver", "com.mysql.jdbc.Driver");
79             prop.put("org.quartz.dataSource.quartzDataSource.URL", url);
80         prop.put("org.quartz.dataSource.quartzDataSource.user", userName);
81         prop.put("org.quartz.dataSource.quartzDataSource.password", password);
82         prop.put("org.quartz.dataSource.quartzDataSource.maxConnections", "50");
83             return prop;
84     }
85     
86     
87 }

    第四步:建类(任务管理类)

  1  
  2 import java.io.Serializable;
  3  
  4 /**
  5  * 
  6 * @ClassName: TaskInfo
  7 * @Description:  管理定时任务
  8 * @author zxj
  9 * @date 2018年2月26日
 10 *
 11  */
 12 public class TaskInfo implements Serializable{
 13     private static final long serialVersionUID = -8054692082716173379L;
 14     
 15     /**
 16      * 增加或修改标识
 17      */
 18     private int id;
 19  
 20     /**
 21      * 任务名称
 22      */
 23     private String jobName;
 24     
 25     /**
 26      * 任务分组
 27      */
 28     private String jobGroup;
 29     
 30     /**
 31      * 任务描述
 32      */
 33     private String jobDescription;
 34     
 35     /**
 36      * 任务状态
 37      */
 38     private String jobStatus;
 39     
 40     /**
 41      * 任务表达式
 42      */
 43     private String cronExpression;
 44     
 45     /**
 46      * 创建时间
 47      */
 48     private String createTime;
 49     
 50     /**
 51      * 间隔时间(毫秒)
 52      */
 53     private String milliSeconds;
 54     
 55     /**
 56      * 重复次数
 57      */
 58     private String repeatCount;
 59     
 60     /**
 61      * 起始时间
 62      */
 63     private String startDate;
 64     
 65     /**
 66      * 终止时间
 67      */
 68     private String endDate;
 69     
 70     /**
 71      * @return the milliSeconds
 72      */
 73     public String getMilliSeconds() {
 74         return milliSeconds;
 75     }
 76  
 77     /**
 78      * @param milliSeconds the milliSeconds to set
 79      */
 80     public void setMilliSeconds(String milliSeconds) {
 81         this.milliSeconds = milliSeconds;
 82     }
 83  
 84     /**
 85      * @return the repeatCount
 86      */
 87     public String getRepeatCount() {
 88         return repeatCount;
 89     }
 90  
 91     /**
 92      * @param repeatCount the repeatCount to set
 93      */
 94     public void setRepeatCount(String repeatCount) {
 95         this.repeatCount = repeatCount;
 96     }
 97  
 98     /**
 99      * @return the startDate
100      */
101     public String getStartDate() {
102         return startDate;
103     }
104  
105     /**
106      * @param startDate the startDate to set
107      */
108     public void setStartDate(String startDate) {
109         this.startDate = startDate;
110     }
111  
112     /**
113      * @return the endDate
114      */
115     public String getEndDate() {
116         return endDate;
117     }
118  
119     /**
120      * @param endDate the endDate to set
121      */
122     public void setEndDate(String endDate) {
123         this.endDate = endDate;
124     }
125  
126     public String getJobName() {
127         return jobName;
128     }
129  
130     public void setJobName(String jobName) {
131         this.jobName = jobName;
132     }
133  
134     public String getJobGroup() {
135         return jobGroup;
136     }
137  
138     public void setJobGroup(String jobGroup) {
139         this.jobGroup = jobGroup;
140     }
141  
142     public String getJobDescription() {
143         return jobDescription;
144     }
145  
146     public void setJobDescription(String jobDescription) {
147         this.jobDescription = jobDescription;
148     }
149  
150     public String getJobStatus() {
151         return jobStatus;
152     }
153  
154     public void setJobStatus(String jobStatus) {
155         this.jobStatus = jobStatus;
156     }
157  
158     public String getCronExpression() {
159         return cronExpression;
160     }
161  
162     public void setCronExpression(String cronExpression) {
163         this.cronExpression = cronExpression;
164     }
165  
166     public String getCreateTime() {
167         return createTime;
168     }
169  
170     public void setCreateTime(String createTime) {
171         this.createTime = createTime;
172     }
173  
174     public int getId() {
175         return id;
176     }
177  
178     public void setId(int id) {
179         this.id = id;
180     }
181 }

    第五步:写方法(任务处理方法)

  1 package com.ewp.data.service;
  2  
  3 import java.util.ArrayList;
  4 import java.util.Date;
  5 import java.util.HashSet;
  6 import java.util.List;
  7  
  8 import org.apache.commons.lang3.time.DateFormatUtils;
  9 import org.quartz.CronScheduleBuilder;
 10 import org.quartz.CronTrigger;
 11 import org.quartz.Job;
 12 import org.quartz.JobBuilder;
 13 import org.quartz.JobDetail;
 14 import org.quartz.JobKey;
 15 import org.quartz.Scheduler;
 16 import org.quartz.SchedulerException;
 17 import org.quartz.SimpleScheduleBuilder;
 18 import org.quartz.SimpleTrigger;
 19 import org.quartz.Trigger;
 20 import org.quartz.TriggerBuilder;
 21 import org.quartz.TriggerKey;
 22 import org.quartz.impl.matchers.GroupMatcher;
 23 import org.slf4j.Logger;
 24 import org.slf4j.LoggerFactory;
 25 import org.springframework.beans.factory.annotation.Autowired;
 26 import org.springframework.stereotype.Service;
 27  
 28 import com.ewp.data.quartz.TaskInfo;
 29 import com.ewp.data.quartz.exception.ServiceException;
 30 import com.ewp.data.util.DateUtil;
 31  
 32 /**
 33  * 
 34  * @ClassName: TaskService
 35  * @Description: 任务处理类
 36  * @author zxj
 37  * @date 2018年2月26日
 38  *
 39  */
 40 @Service
 41 public class TaskService {
 42     private static final Logger LOGGER = LoggerFactory.getLogger(TaskService.class);
 43  
 44     @Autowired
 45     private Scheduler scheduler;
 46  
 47     /**
 48      * 
 49      * @Title: list
 50      * @Description: 任务列表
 51      * @param @return    参数
 52      * @return List<TaskInfo>    返回类型
 53      * @throws
 54      */
 55     public List<TaskInfo> queryJobList() {
 56         LOGGER.info("TaskService--data-s-->queryJobList()");
 57         List<TaskInfo> list = new ArrayList<>();
 58         try {
 59         for (String groupJob : scheduler.getJobGroupNames()) {
 60             for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.<JobKey> groupEquals(groupJob))) {
 61             List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
 62             for (Trigger trigger : triggers) {
 63                 Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
 64                 JobDetail jobDetail = scheduler.getJobDetail(jobKey);
 65                 String cronExpression = "";
 66                 String createTime = "";
 67                 String milliSeconds = "";
 68                 String repeatCount = "";
 69                 String startDate = "";
 70                 String endDate = "";
 71                 if (trigger instanceof CronTrigger) {
 72                     CronTrigger cronTrigger = (CronTrigger) trigger;
 73                     cronExpression = cronTrigger.getCronExpression();
 74                     createTime = cronTrigger.getDescription();
 75                 } else if (trigger instanceof SimpleTrigger) {
 76                 SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
 77                 milliSeconds = simpleTrigger.getRepeatInterval()+ "";
 78                 repeatCount = simpleTrigger.getRepeatCount() + "";
 79                 startDate = DateUtil.getDateStr(
 80                 simpleTrigger.getStartTime(),
 81                 DateUtil.FORMAT_HOUR_DATE_TIME);
 82                 endDate = DateUtil.getDateStr(simpleTrigger.getEndTime(),DateUtil.FORMAT_HOUR_DATE_TIME);
 83                 }
 84                 TaskInfo info = new TaskInfo();
 85                 info.setJobName(jobKey.getName());
 86                 info.setJobGroup(jobKey.getGroup());
 87                 info.setJobDescription(jobDetail.getDescription());
 88                 info.setJobStatus(triggerState.name());
 89                 info.setCronExpression(cronExpression);
 90                 info.setCreateTime(createTime);
 91  
 92                 info.setRepeatCount(repeatCount);
 93                 info.setStartDate(startDate);
 94                 info.setMilliSeconds(milliSeconds);
 95                 info.setEndDate(endDate);
 96                 list.add(info);
 97                 }
 98             }
 99         }
100         LOGGER.info("任务的数量为:---------------->" + list.size());
101         } catch (SchedulerException e) {
102         LOGGER.info("查询任务失败,原因是:------------------>" + e.getMessage());
103         e.printStackTrace();
104      }
105         return list;
106     }
107  
108     /**
109      * 
110      * @Title: setSimpleTrigger
111      * @Description: 简单调度
112      * @param @param inputMap
113      * @param @return    参数
114      * @return Boolean    返回类型
115      * @throws
116      */
117     @SuppressWarnings({ "unchecked" })
118     public void setSimpleTriggerJob(TaskInfo info) {
119         LOGGER.info("TaskService--data-s-->setSimpleTriggerJob()" + info);
120         String jobName = info.getJobName();
121         String jobGroup = info.getJobGroup();
122         String jobDescription = info.getJobDescription();
123         Long milliSeconds = Long.parseLong(info.getMilliSeconds());
124         Integer repeatCount = Integer.parseInt(info.getRepeatCount());
125         Date startDate = DateUtil.parseDate(info.getStartDate());
126         Date endDate = DateUtil.parseDate(info.getEndDate());
127         try {
128             TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);// 触发器的key值
129             JobKey jobKey = JobKey.jobKey(jobName, jobGroup);// job的key值
130             if (checkExists(jobName, jobGroup)) {
131                 LOGGER.info(
132                         "===> AddJob fail, job already exist, jobGroup:{}, jobName:{}",
133                         jobGroup, jobName);
134                 throw new ServiceException(String.format(
135                         "Job已经存在, jobName:{%s},jobGroup:{%s}", jobName,
136                         jobGroup));
137             }
138             /* 简单调度 */
139             SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder
140                     .newTrigger()
141                     .withIdentity(triggerKey)
142                     .startAt(startDate)
143                     .withSchedule(
144                             SimpleScheduleBuilder.simpleSchedule()
145                                     .withIntervalInMilliseconds(milliSeconds)
146                                     .withRepeatCount(repeatCount))
147                     .endAt(endDate).build();
148             Class<? extends Job> clazz = (Class<? extends Job>) Class
149                     .forName(jobName);
150             JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobKey)
151                     .withDescription(jobDescription).build();
152             scheduler.scheduleJob(jobDetail, trigger);
153         } catch (SchedulerException | ClassNotFoundException e) {
154             LOGGER.info("任务添加失败!--->简单调度" + e.getMessage());
155             throw new ServiceException("任务添加失败!--->简单调度");
156         }
157     }
158  
159     /**
160      * 
161      * @Title: addJob
162      * @Description: 保存定时任务
163      * @param @param info    参数
164      * @return void    返回类型
165      * @throws
166      */
167     @SuppressWarnings("unchecked")
168     public void addJob(TaskInfo info) {
169         LOGGER.info("TaskService--data-s-->addJob()" + info);
170         String jobName = info.getJobName(), jobGroup = info.getJobGroup(), cronExpression = info
171                 .getCronExpression(), jobDescription = info.getJobDescription(), createTime = DateFormatUtils
172                 .format(new Date(), "yyyy-MM-dd HH:mm:ss");
173         try {
174             if (checkExists(jobName, jobGroup)) {
175                 LOGGER.info(
176                         "===> AddJob fail, job already exist, jobGroup:{}, jobName:{}",
177                         jobGroup, jobName);
178                 throw new ServiceException(String.format(
179                         "Job已经存在, jobName:{%s},jobGroup:{%s}", jobName,
180                         jobGroup));
181             }
182  
183             TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
184             JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
185  
186             CronScheduleBuilder schedBuilder = CronScheduleBuilder
187                     .cronSchedule(cronExpression)
188                     .withMisfireHandlingInstructionDoNothing();
189             CronTrigger trigger = TriggerBuilder.newTrigger()
190                     .withIdentity(triggerKey).withDescription(createTime)
191                     .withSchedule(schedBuilder).build();
192  
193             Class<? extends Job> clazz = (Class<? extends Job>) Class
194                     .forName(jobName);
195             JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobKey)
196                     .withDescription(jobDescription).build();
197             scheduler.scheduleJob(jobDetail, trigger);
198         } catch (SchedulerException | ClassNotFoundException e) {
199             LOGGER.info("保存定时任务-->类名不存在或执行表达式错误--->复杂调度" + e.getMessage());
200             throw new ServiceException("类名不存在或执行表达式错误");
201         }
202     }
203  
204     /**
205      * 
206      * @Title: edit
207      * @Description: 修改定时任务
208      * @param @param info    参数
209      * @return void    返回类型
210      * @throws
211      */
212     public void editJob(TaskInfo info) {
213         LOGGER.info("TaskService--data-s-->editJob()" + info);
214         String jobName = info.getJobName(), jobGroup = info.getJobGroup(), cronExpression = info
215                 .getCronExpression(), jobDescription = info.getJobDescription(), createTime = DateFormatUtils
216                 .format(new Date(), "yyyy-MM-dd HH:mm:ss");
217         try {
218             if (!checkExists(jobName, jobGroup)) {
219                 throw new ServiceException(
220                         String.format("Job不存在, jobName:{%s},jobGroup:{%s}",
221                                 jobName, jobGroup));
222             }
223             TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
224             JobKey jobKey = new JobKey(jobName, jobGroup);
225             CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder
226                     .cronSchedule(cronExpression)
227                     .withMisfireHandlingInstructionDoNothing();
228             CronTrigger cronTrigger = TriggerBuilder.newTrigger()
229                     .withIdentity(triggerKey).withDescription(createTime)
230                     .withSchedule(cronScheduleBuilder).build();
231  
232             JobDetail jobDetail = scheduler.getJobDetail(jobKey);
233             jobDetail.getJobBuilder().withDescription(jobDescription);
234             HashSet<Trigger> triggerSet = new HashSet<>();
235             triggerSet.add(cronTrigger);
236  
237             scheduler.scheduleJob(jobDetail, triggerSet, true);
238         } catch (SchedulerException e) {
239             LOGGER.info("修改定时任务-->类名不存在或执行表达式错误--->复杂调度" + e.getMessage());
240             throw new ServiceException("类名不存在或执行表达式错误");
241         }
242     }
243  
244     /**
245      * 
246      * @Title: delete
247      * @Description: 删除定时任务
248      * @param @param jobName
249      * @param @param jobGroup    参数
250      * @return void    返回类型
251      * @throws
252      */
253     public void deleteJob(String jobName, String jobGroup) {
254         LOGGER.info("TaskService--data-s-->deleteJob()--jobName:" + jobName
255                 + "jobGroup:" + jobGroup);
256         TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
257         try {
258             if (checkExists(jobName, jobGroup)) {
259                 scheduler.pauseTrigger(triggerKey);
260                 scheduler.unscheduleJob(triggerKey);
261                 LOGGER.info("===> delete, triggerKey:{}", triggerKey);
262             }
263         } catch (SchedulerException e) {
264             LOGGER.info("删除定时任务-->复杂调度" + e.getMessage());
265             throw new ServiceException(e.getMessage());
266         }
267     }
268  
269     /**
270      * 
271      * @Title: pause
272      * @Description: 暂停定时任务
273      * @param @param jobName
274      * @param @param jobGroup    参数
275      * @return void    返回类型
276      * @throws
277      */
278     public void pauseJob(String jobName, String jobGroup) {
279         LOGGER.info("TaskService--data-s-->pauseJob()--jobName:" + jobName
280                 + "jobGroup:" + jobGroup);
281         TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
282         try {
283             if (checkExists(jobName, jobGroup)) {
284                 scheduler.pauseTrigger(triggerKey);
285                 LOGGER.info("===> Pause success, triggerKey:{}", triggerKey);
286             }
287         } catch (SchedulerException e) {
288             LOGGER.info("暂停定时任务-->复杂调度" + e.getMessage());
289             throw new ServiceException(e.getMessage());
290         }
291     }
292  
293     /**
294      * 
295      * @Title: resume
296      * @Description: 恢复暂停任务
297      * @param @param jobName
298      * @param @param jobGroup    参数
299      * @return void    返回类型
300      * @throws
301      */
302     public void resumeJob(String jobName, String jobGroup) {
303         LOGGER.info("TaskService--data-s-->resumeJob()--jobName:" + jobName
304                 + "jobGroup:" + jobGroup);
305         TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
306         try {
307             if (checkExists(jobName, jobGroup)) {
308                 scheduler.resumeTrigger(triggerKey);
309                 LOGGER.info("===> Resume success, triggerKey:{}", triggerKey);
310             }
311         } catch (SchedulerException e) {
312             LOGGER.info("重新开始任务-->复杂调度" + e.getMessage());
313             e.printStackTrace();
314         }
315     }
316  
317     /**
318      * 
319      * @Title: checkExists
320      * @Description: 验证任务是否存在
321      * @param @param jobName
322      * @param @param jobGroup
323      * @param @return
324      * @param @throws SchedulerException    参数
325      * @return boolean    返回类型
326      * @throws
327      */
328     private boolean checkExists(String jobName, String jobGroup)
329             throws SchedulerException {
330         LOGGER.info("TaskService--data-s-->checkExists()--jobName:" + jobName
331                 + "jobGroup:" + jobGroup);
332         TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
333         return scheduler.checkExists(triggerKey);
334     }
335  
336 }

    第六步:执行sql脚本

DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS;
DROP TABLE IF EXISTS QRTZ_SCHEDULER_STATE;
DROP TABLE IF EXISTS QRTZ_LOCKS;
DROP TABLE IF EXISTS QRTZ_SIMPLE_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_SIMPROP_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_CRON_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_BLOB_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_JOB_DETAILS;
DROP TABLE IF EXISTS QRTZ_CALENDARS;
 
 
CREATE TABLE QRTZ_JOB_DETAILS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    JOB_NAME  VARCHAR(200) NOT NULL,
    JOB_GROUP VARCHAR(200) NOT NULL,
    DESCRIPTION VARCHAR(250) NULL,
    JOB_CLASS_NAME   VARCHAR(250) NOT NULL,
    IS_DURABLE VARCHAR(1) NOT NULL,
    IS_NONCONCURRENT VARCHAR(1) NOT NULL,
    IS_UPDATE_DATA VARCHAR(1) NOT NULL,
    REQUESTS_RECOVERY VARCHAR(1) NOT NULL,
    JOB_DATA BLOB NULL,
    PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
);
 
CREATE TABLE QRTZ_TRIGGERS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    JOB_NAME  VARCHAR(200) NOT NULL,
    JOB_GROUP VARCHAR(200) NOT NULL,
    DESCRIPTION VARCHAR(250) NULL,
    NEXT_FIRE_TIME BIGINT(13) NULL,
    PREV_FIRE_TIME BIGINT(13) NULL,
    PRIORITY INTEGER NULL,
    TRIGGER_STATE VARCHAR(16) NOT NULL,
    TRIGGER_TYPE VARCHAR(8) NOT NULL,
    START_TIME BIGINT(13) NOT NULL,
    END_TIME BIGINT(13) NULL,
    CALENDAR_NAME VARCHAR(200) NULL,
    MISFIRE_INSTR SMALLINT(2) NULL,
    JOB_DATA BLOB NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
        REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP)
);
 
CREATE TABLE QRTZ_SIMPLE_TRIGGERS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    REPEAT_COUNT BIGINT(7) NOT NULL,
    REPEAT_INTERVAL BIGINT(12) NOT NULL,
    TIMES_TRIGGERED BIGINT(10) NOT NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
        REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
);
 
CREATE TABLE QRTZ_CRON_TRIGGERS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    CRON_EXPRESSION VARCHAR(200) NOT NULL,
    TIME_ZONE_ID VARCHAR(80),
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
        REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
);
 
CREATE TABLE QRTZ_SIMPROP_TRIGGERS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    STR_PROP_1 VARCHAR(512) NULL,
    STR_PROP_2 VARCHAR(512) NULL,
    STR_PROP_3 VARCHAR(512) NULL,
    INT_PROP_1 INT NULL,
    INT_PROP_2 INT NULL,
    LONG_PROP_1 BIGINT NULL,
    LONG_PROP_2 BIGINT NULL,
    DEC_PROP_1 NUMERIC(13,4) NULL,
    DEC_PROP_2 NUMERIC(13,4) NULL,
    BOOL_PROP_1 VARCHAR(1) NULL,
    BOOL_PROP_2 VARCHAR(1) NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
    REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
);
 
CREATE TABLE QRTZ_BLOB_TRIGGERS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    BLOB_DATA BLOB NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
        REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
);
 
CREATE TABLE QRTZ_CALENDARS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    CALENDAR_NAME  VARCHAR(200) NOT NULL,
    CALENDAR BLOB NOT NULL,
    PRIMARY KEY (SCHED_NAME,CALENDAR_NAME)
);
 
CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_GROUP  VARCHAR(200) NOT NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP)
);
 
CREATE TABLE QRTZ_FIRED_TRIGGERS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    ENTRY_ID VARCHAR(95) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    INSTANCE_NAME VARCHAR(200) NOT NULL,
    FIRED_TIME BIGINT(13) NOT NULL,
    SCHED_TIME BIGINT(13) NOT NULL,
    PRIORITY INTEGER NOT NULL,
    STATE VARCHAR(16) NOT NULL,
    JOB_NAME VARCHAR(200) NULL,
    JOB_GROUP VARCHAR(200) NULL,
    IS_NONCONCURRENT VARCHAR(1) NULL,
    REQUESTS_RECOVERY VARCHAR(1) NULL,
    PRIMARY KEY (SCHED_NAME,ENTRY_ID)
);
 
CREATE TABLE QRTZ_SCHEDULER_STATE
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    INSTANCE_NAME VARCHAR(200) NOT NULL,
    LAST_CHECKIN_TIME BIGINT(13) NOT NULL,
    CHECKIN_INTERVAL BIGINT(13) NOT NULL,
    PRIMARY KEY (SCHED_NAME,INSTANCE_NAME)
);
 
CREATE TABLE QRTZ_LOCKS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    LOCK_NAME  VARCHAR(40) NOT NULL,
    PRIMARY KEY (SCHED_NAME,LOCK_NAME)
);

    第七步:编写自定义任务(需实现Job接口)

 1 /**
 2 * @Title: ReportMailBatch.java
 3 * @Package com.ewp.data.batch
 4 * @Description: 邮件汇报批
 5 * @author zxj
 6 * @date 2018年2月26日
 7 * @version V1.0
 8 */
 9 package com.ewp.data.batch;
10  
11 import org.quartz.Job;
12 import org.quartz.JobExecutionContext;
13 import org.quartz.JobExecutionException;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.stereotype.Component;
18  
19 import com.ewp.data.service.ReportMailService;
20  
21 /**
22  * @ClassName: ReportMailBatch
23  * @Description: 邮件汇报批
24  * @author zxj
25  * @date 2018年2月26日
26  *
27  */
28 @Component
29 public class ReportMailBatch implements Job{
30  
31     private static final Logger log = LoggerFactory.getLogger(ReportMailBatch.class);
32     
33     @Autowired
34     private ReportMailService service;
35     
36     @Override
37     public void execute(JobExecutionContext arg0) throws JobExecutionException {
38         log.info("ReportMailBatch--data-c->execute()");
39         service.sendReportMail();// 可换成自己业务逻辑
40         log.info("--本次邮件汇报批处理结束--");
41     }
42 }

     第八步:创建任务处理controller注入TaskService,将第七步编写的任务加入调度器,由线程调用执行。


原文链接:https://www.cnblogs.com/yzss/p/12176542.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java网络考试系统的设计与实现 jsp 源码

下一篇:关于Integer 和Double包装类创建对象时的底层解析