Quartz核心的概念:scheduler任务调度、Job任务、Trigger触发器、JobDetail任务细节。
scheduler任务调度:
是最核心的概念,需要把JobDetail和Trigger注册到scheduler中,才可以执行。
Job任务:
其实Job是接口,其中只有一个execute方法:
Trigger触发器
a)作用:它是来执行工作任务,在什么条件下触发,什么时间执行,多久执行一次。
b)四大类型:SimpleTrigger,CronTirgger,DateIntervalTrigger, 和 NthIncludedDayTrigger。
SimpleTrigger 一般用于实现每隔一定时间执行任务,以及重复多少次,如每 2 小时执行一次,重复执行 5 次。SimpleTrigger 内部实现机制是通过计算间隔时间来计算下次的执行时间,这就导致其不适合调度定时的任务。例如我们想每天的 1:00AM 执行任务,如果使用 SimpleTrigger 的话间隔时间就是一天。注意这里就会有一个问题,即当有 misfired 的任务并且恢复执行时,该执行时间是随机的(取决于何时执行 misfired 的任务,例如某天的 3:00PM)。这会导致之后每天的执行时间都会变成 3:00PM,而不是我们原来期望的 1:00AM。
CronTirgger 类似于 LINUX 上的任务调度命令 crontab,即利用一个包含 7 个字段的表达式来表示时间调度方式。例如,"0 15 10 * * ? *" 表示每天的 10:15AM 执行任务。对于涉及到星期和月份的调度,CronTirgger 是最适合的,甚至某些情况下是唯一选择。例如,"0 10 14 ? 3 WED" 表示三月份的每个星期三的下午 14:10PM 执行任务。读者可以在具体用到该 trigger 时再详细了解每个字段的含义。
源码下载:http://d2zwv9pap9ylyd.cloudfront.net/quartz-2.2.3-distribution.tar.gz
首先要在mysql里面建一个数据库,这里起名:quartz,并导入以下sql内容
以下的sql为实现quartz集群必须要的表,quartz集群就是通过共享一个数据库来实现的,不像redis那样的集群,节点与节点需要通信;quartz集群的节点之间是不需要通信的。
数据库执行doc中的tables_mysql.sql
#
# Quartz seems to work best with the driver mm.mysql-2.0.7-bin.jar
#
# PLEASE consider using mysql with innodb tables to avoid locking issues
#
# In your Quartz properties file, you'll need to set
# org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#
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)
);
commit;
完整的工程结构如下图
项目创建之后,引入Springboot及quartz依赖,另外,由于页面是使用的jsp,所以需要引入关于jsp的依赖和servlet的依赖(外部tomcat部署不需要引入jsp相关的依赖),pom文件相关依赖如下:
4.0.0
com.hongguaninfo
quartz-job
pom
1.0-SNAPSHOT
quartz-job-admin
UTF-8
UTF-8
UTF-8
1.7
1.7
true
1.2.1
1.5.17.RELEASE
1.3.2
5.1.47
4.3.20.RELEASE
1.7.25
2.3.28
4.12
1.3
4.2
3.8.1
1.5
2.5.3
2.2.3
2.9.7
9.2.26.v20180806
2、pom.xml(spring-boot-quartz)
4.0.0
com.hongguaninfo
quartz-job
1.0-SNAPSHOT
quartz-job-admin
war
org.springframework.boot
spring-boot-starter-parent
${spring-boot.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot-starter.version}
org.springframework.boot
spring-boot-devtools
true
mysql
mysql-connector-java
${mysql-connector-java.version}
org.quartz-scheduler
quartz
${quartz.version}
org.quartz-scheduler
quartz-jobs
${quartz.version}
org.springframework
spring-context-support
${spring.version}
org.apache.commons
commons-collections4
${commons-collections4.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
org.apache.commons
commons-email
${commons-email.version}
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
javax.servlet
javax.servlet-api
org.springframework.boot
spring-boot-maven-plugin
${spring-boot.version}
repackage
true
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
application.properties
详细的springboot配置如下:
### web
server.port=8083
server.context-path=/quartz-job-admin
#默认的位置是src/main/webapp 这里可以更换,比如/page/ 那么目录就变成了src/main/webapp/pages
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp
#关闭默认模板引擎
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=false
### resources
#表示所有的访问都经过静态资源路径
spring.mvc.static-path-pattern=/**
#覆盖默认配置,所以需要将默认的也加上否则static、public等这些路径将不能被当作静态资源路径
spring.resources.static-locations=classpath:/META-INF/static/,classpath:/static/
### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*sqlmap.xml
mybatis.type-aliases-package=com.hongguaninfo.quartz.entity
###日志
logging.path=/logs/quartz
logging.file=springboot-quartz.log
logging.level.root=info
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://192.168.8.179:3306/quartz?Unicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=30
spring.datasource.tomcat.test-on-borrow=true
详细quartz配置如下:
(注意:如果部署的是quartz集群,必须加上org.quartz.jobStore.isClustered = true,如果只是单节点quartz可以不用加)
#默认或是自己改名字都行
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
#如果使用集群,instanceId必须唯一,设置成AUTO
org.quartz.scheduler.instanceId = AUTO
#rmi远程调用代理默认不用
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
# Job事务是否自定义的 spring quartz采用了这个特性
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
#Job执行的线程池
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
#线程权重
org.quartz.threadPool.threadPriority: 5
#线程类加载器是否继承父类加载器
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
#job阻塞时间,超过此时间将跳过此次执行
org.quartz.jobStore.misfireThreshold: 60000
#============================================================================
# Configure JobStore
#============================================================================
# 默认内存存储模式(单机)
#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
#集群配置模式与单机配置唯一区别
#存储方式使用JobStoreTX,也就是数据库,quartz jdbc事务持久化
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
#jdbc类,默认的
#org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#自定义的jdbc类,重写selectJobDetail,改变加载job class的方式,继承了org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.driverDelegateClass:com.hongguaninfo.hgdf.core.glue.StdJDBCDelegateMy
#使用自己的配置文件
org.quartz.jobStore.useProperties:true
#数据库中quartz表的表名前缀
org.quartz.jobStore.tablePrefix:QRTZ_
#指定数据源
org.quartz.jobStore.dataSource:qzDS
#是否使用集群(如果项目只部署到 一台服务器,就不用了)
org.quartz.jobStore.isClustered = true
# 同一实例集群检测其他实例是否存活的轮询时间
org.quartz.jobStore.clusterCheckinInterval = 7500
#============================================================================
# Configure Datasources
#============================================================================
#配置数据源
org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL:jdbc:mysql://192.168.8.179:3306/quartz?useUnicode=true&characterEncoding=utf8
org.quartz.dataSource.qzDS.user:root
org.quartz.dataSource.qzDS.password:root
org.quartz.dataSource.qzDS.validationQuery=select 0 from dual
package com.hongguaninfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
/**
* Created by chenqinglong on 2019/2/1 0001.
*/
@SpringBootApplication
@ComponentScan(basePackages = { "com.hongguaninfo" })
@MapperScan("com.hongguaninfo.quartz.mapper")
public class QuartzApplication extends SpringBootServletInitializer {
public static void main(String[] args){
SpringApplication.run(QuartzApplication.class, args);
}
/**
* 继承SpringBootServletInitializer 实现configure 方便打war 外部服务器部署。
* 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式
* @param application
* @return
*/
@Override
protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
return application.sources(QuartzApplication.class);
}
}
说明:由于我们是war项目,需要外部tomcat部署运行,
所以需要继承SpringBootServletInitializer并重写configure方法,改变其启动方式。
解决spring注入问题,如果使用默认会导致spring的@Autowired 无法注入问题(很重要)
package com.springquartz.factory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;
@Component
public class MyJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
package com.hongguaninfo.quartz.config;
import com.hongguaninfo.quartz.factory.MyJobFactory;
import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
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 java.io.IOException;
import java.util.Properties;
/**
* quartz的初始化配置
* Created by chenqinglong on 2019/2/1 0001.
*/
@Configuration
public class SchedulerConfiguration {
@Autowired
private MyJobFactory myJobFactory;
@Bean(name = "schedulerFactoryBean")
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
//获取配置属性
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("quartz.properties"));
//在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
//创建SchedulerFactoryBean
SchedulerFactoryBean factory = new SchedulerFactoryBean();
Properties pro = propertiesFactoryBean.getObject();
factory.setOverwriteExistingJobs(true);
factory.setAutoStartup(true);
factory.setQuartzProperties(pro);
factory.setJobFactory(myJobFactory);
return factory;
}
/**
* quartz初始化监听器
* 这个监听器可以监听到工程的启动,在工程停止再启动时可以让已有的定时任务继续进行。
* 由于我们目前的工程是Spring Boot,没有web.xml的配置方式,
* 所以我们在上文的SchedulerConfig类中直接注入了这个Bean。
*/
@Bean
public QuartzInitializerListener executorListener() {
return new QuartzInitializerListener();
}
/**
* 通过SchedulerFactoryBean获取Scheduler的实例
*/
@Bean(name="Scheduler")
public Scheduler scheduler() throws IOException {
return schedulerFactoryBean().getScheduler();
}
}
包括:添加任务、修改任务、暂停任务、恢复任务、删除任务、任务列表查询,具体代码如下:
package com.hongguaninfo.quartz.service;
import com.hongguaninfo.hgdf.core.base.BasePage;
import com.hongguaninfo.hgdf.core.utils.StringUtil;
import com.hongguaninfo.hgdf.core.utils.exception.BaseException;
import com.hongguaninfo.hgdf.core.utils.logging.Log;
import com.hongguaninfo.hgdf.core.utils.logging.LogFactory;
import com.hongguaninfo.hgdf.core.utils.page.Page;
import com.hongguaninfo.quartz.dao.QrtzJobDetailsDao;
import com.hongguaninfo.quartz.entity.JobAndTrigger;
import com.hongguaninfo.quartz.entity.QrtzJobDetails;
import com.hongguaninfo.quartz.job.AsyncJob;
import org.quartz.*;
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 java.util.List;
import java.util.Map;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
/**
* Created by chenqinglong on 2019/2/1 0001.
*/
@Service
public class JobService {
private Log log = LogFactory.getLog(JobService.class);
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
@Autowired
private QrtzJobDetailsDao qrtzJobDetailsDao;
@Autowired @Qualifier("Scheduler")
private Scheduler scheduler;
/**
* 添加一个定时任务
* @param jobAndTrigger
*/
public void addCronJob(JobAndTrigger jobAndTrigger) throws BaseException{
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(jobAndTrigger.getJobName(), jobAndTrigger.getJobGroup());
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail != null) {
System.out.println("job:" + jobAndTrigger.getJobName() + " 已存在");
} else {
//构建job信息
jobDetail = JobBuilder.newJob((Class)Class.forName(jobAndTrigger.getJobClassName()))
.withIdentity(jobAndTrigger.getJobName(), jobAndTrigger.getJobGroup())
.withDescription(jobAndTrigger.getDescription())
.build();
//用JopDataMap来传递数据
jobDetail.getJobDataMap().put("taskData", "hzb-cron-001");
//表达式调度构建器,首先校验表达式格式
if (!CronExpression.isValidExpression(jobAndTrigger.getCronExpression())) {
throw new BaseException("定时任务规则表达式不正确");
}
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobAndTrigger.getCronExpression());
//按新的cronExpression表达式构建一个新的trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobAndTrigger.getJobName() + "_trigger", jobAndTrigger.getJobGroup() + "_trigger")
.withSchedule(scheduleBuilder)
.withDescription(jobAndTrigger.getDescription())
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
} catch (Exception e) {
log.error("任务创建失败:", e);
throw new BaseException(e.getMessage());
}
}
/**
* 添加异步任务
* @param jobName
* @param jobGroup
*/
public void addAsyncJob(String jobName, String jobGroup, String jobClass) {
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail != null) {
System.out.println("job:" + jobName + " 已存在");
}
else {
//构建job信息,在用JobBuilder创建JobDetail的时候,有一个storeDurably()方法,可以在没有触发器指向任务的时候,将任务保存在队列中了。然后就能手动触发了
jobDetail = JobBuilder.newJob(AsyncJob.class).withIdentity(jobName, jobGroup).storeDurably().build();
jobDetail.getJobDataMap().put("asyncData","this is a async task");
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName + "_trigger", jobGroup + "_trigger") //定义name/group
.startNow()//一旦加入scheduler,立即生效
.withSchedule(simpleSchedule())//使用SimpleTrigger
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 暂停任务
* @param jobName
* @param jobGroup
*/
public void pauseJob(String jobName, String jobGroup) throws BaseException {
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobName + "_trigger", jobGroup + "_trigger");
scheduler.pauseTrigger(triggerKey);
} catch (SchedulerException e) {
log.error("暂停失败:", e);
throw new BaseException("暂停失败:" + e.getMessage());
}
}
/**
* 恢复任务
* @param jobName
* @param jobGroup
*/
public void resumeJob(String jobName, String jobGroup) throws BaseException {
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobName + "_trigger", jobGroup + "_trigger");
scheduler.resumeTrigger(triggerKey);
} catch (SchedulerException e) {
log.error("恢复失败:", e);
throw new BaseException("恢复失败:" + e.getMessage());
}
}
/**
* 修改任务表达式并重新调度
* @param jobAndTrigger
*/
public void rescheduleJob(JobAndTrigger jobAndTrigger ) throws BaseException {
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobAndTrigger.getJobName() + "_trigger", jobAndTrigger.getJobGroup() + "_trigger");
// 表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobAndTrigger.getCronExpression());
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
String oldTime = trigger.getCronExpression();
if (oldTime.isEmpty() || !oldTime.equals(jobAndTrigger.getCronExpression())){
//表达式调度构建器,首先校验表达式格式
if (!CronExpression.isValidExpression(jobAndTrigger.getCronExpression())) {
throw new BaseException("定时任务规则表达式不正确");
}
// 按新的cronExpression表达式重新构建trigger
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
// 按新的trigger重新设置job执行
scheduler.rescheduleJob(triggerKey, trigger);
}
} catch (SchedulerException e) {
log.error("修改失败:", e);
throw new BaseException("修改失败:" + e.getMessage());
}
}
/**
* 删除job
* @param jobName
* @param jobGroup
*/
public void deleteJob(String jobName, String jobGroup) throws BaseException {
try {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(jobName,jobGroup);
scheduler.deleteJob(jobKey);
} catch (SchedulerException e) {
log.error("删除失败:", e);
throw new BaseException("删除失败:" + e.getMessage());
}
}
public List getJobList(BasePage basePage, JobAndTrigger vo,
Map map) throws Exception {
basePage.setFilters(vo);
Page page = qrtzJobDetailsDao.pageQuery("getJobAndTriggerList",basePage);
List list = page.getResult();
map.put("rows", list);
map.put("total", page.getTotalCount());
return list;
}
public List getJobAndTriggerList(JobAndTrigger jobAndTrigger){
List jobAndTriggerList = qrtzJobDetailsDao.getJobAndTriggerList(jobAndTrigger);
return jobAndTriggerList;
}
public JobAndTrigger getJobAndTrigger(JobAndTrigger jobAndTrigger){
List jobAndTriggerList = qrtzJobDetailsDao.getJobAndTriggerList(jobAndTrigger);
if (jobAndTriggerList.size() > 0){
return jobAndTriggerList.get(0);
}else{
return new JobAndTrigger();
}
}
}
package com.hongguaninfo.quartz.controller;
import com.hongguaninfo.hgdf.core.base.BaseController;
import com.hongguaninfo.hgdf.core.base.BasePage;
import com.hongguaninfo.hgdf.core.templete.OperateTemplete;
import com.hongguaninfo.hgdf.core.utils.exception.BaseException;
import com.hongguaninfo.quartz.entity.JobAndTrigger;
import com.hongguaninfo.quartz.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* Created by chenqinglong on 2019/2/1 0001.
*/
@Controller
@RequestMapping("/quartzJob")
public class JobController extends BaseController{
@Autowired
private JobService jobService;
@RequestMapping(value="/job")
public String job(){
return "job";
}
@RequestMapping(value="/jobDetail/{jobName}/{jobGroup}/{mode}")
public String jobDetail(@PathVariable String jobName, @PathVariable String jobGroup,
@PathVariable String mode,
HttpServletRequest request, HttpServletResponse response,
Model model){
JobAndTrigger jobAndTrigger = new JobAndTrigger();
jobAndTrigger.setJobName(jobName);
jobAndTrigger.setJobGroup(jobGroup);
model.addAttribute("job", jobService.getJobAndTrigger(jobAndTrigger));
model.addAttribute("mode", mode);
return "job_detail";
}
@RequestMapping("/jobList")
@ResponseBody
public Map getJobList(final JobAndTrigger vo, final BasePage basePage,
HttpServletResponse response, HttpServletRequest request){
Map map = new HashMap<>();
try {
jobService.getJobList(basePage, vo, map);
} catch (Exception e) {
LOG.error("任务列表查询错误:", e);
map.put("success",false);
map.put("data",e.getStackTrace());
}
return map;
}
/**
* 创建cron任务
* @param jobAndTrigger
* @return
*/
@RequestMapping(value = "/addCronJob",method = RequestMethod.POST)
@ResponseBody
public Map addCronJob(final JobAndTrigger jobAndTrigger){
OperateTemplete operateTemplete = new OperateTemplete() {
@Override
protected void doSomething() throws BaseException {
jobService.addCronJob(jobAndTrigger);
}
};
return operateTemplete.operate();
}
/**
* 创建异步任务
* @param jobName
* @param jobGroup
* @return
*/
@RequestMapping(value = "/async",method = RequestMethod.POST)
public String startAsyncJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup,
@RequestParam("jobGroup") String jobClass){
jobService.addAsyncJob(jobName,jobGroup,jobClass);
return "create async task success";
}
/**
* 暂停任务
* @param jobName
* @param jobGroup
* @return
*/
@RequestMapping(value = "/pause",method = RequestMethod.POST)
@ResponseBody
public Map pauseJob(@RequestParam("jobName") final String jobName, @RequestParam("jobGroup") final String jobGroup){
OperateTemplete operateTemplete = new OperateTemplete() {
@Override
protected void doSomething() throws BaseException {
jobService.pauseJob(jobName,jobGroup);
}
};
return operateTemplete.operate();
}
/**
* 恢复任务
* @param jobName
* @param jobGroup
* @return
*/
@RequestMapping(value = "/resume",method = RequestMethod.POST)
@ResponseBody
public Map resumeJob(@RequestParam("jobName") final String jobName, @RequestParam("jobGroup") final String jobGroup){
OperateTemplete operateTemplete = new OperateTemplete() {
@Override
protected void doSomething() throws BaseException {
jobService.resumeJob(jobName,jobGroup);
}
};
return operateTemplete.operate();
}
/**
* 修改任务
* @param jobAndTrigger
* @return
*/
@RequestMapping(value = "/rescheduleJob",method = RequestMethod.POST)
@ResponseBody
public Map rescheduleJob(final JobAndTrigger jobAndTrigger){
OperateTemplete operateTemplete = new OperateTemplete() {
@Override
protected void doSomething() throws BaseException {
jobService.rescheduleJob(jobAndTrigger);
}
};
return operateTemplete.operate();
}
/**
* 删除务
* @param jobName
* @param jobGroup
* @return
*/
@RequestMapping(value = "/delete")
@ResponseBody
public Map deleteJob(@RequestParam("jobName") final String jobName, @RequestParam("jobGroup") final String jobGroup){
OperateTemplete operateTemplete = new OperateTemplete() {
@Override
protected void doSomething() throws BaseException {
jobService.deleteJob(jobName,jobGroup);
}
};
return operateTemplete.operate();
}
}
这里的job仅仅只是打印信息,不做复杂的业务
package com.hongguaninfo.quartz.job;
import com.hongguaninfo.hgdf.core.utils.logging.Log;
import com.hongguaninfo.hgdf.core.utils.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Created by chenqinglong on 2019/2/1 0001.
*/
public class CronJob implements Job {
private Log log = LogFactory.getLog(CronJob.class);
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("=========================定时任务每5秒执行一次===============================");
log.info("jobName=====:"+jobExecutionContext.getJobDetail().getKey().getName());
log.info("jobGroup=====:"+jobExecutionContext.getJobDetail().getKey().getGroup());
log.info("taskData=====:"+jobExecutionContext.getJobDetail().getJobDataMap().get("taskData"));
}
}
发布本地版本
本地调试运行启动方式最终走的是main方法,所以需要把pom.xml中以下三处依赖注释掉:
目的是让springboot用其内置tomcat启动,并且能够加载请求jsp页面。
启动方式:
一、进入quartz-job-admin目录,使用mvn spring-boot:run方式启动
二、直接运行spring-boot:run,如图:
(注意:由于本人创建的项目结构的问题,在idea中直接运行main方法会出现不能访问jsp页面的问题,相关说明请见本人博客:
https://blog.csdn.net/chen_lay/article/details/86741858)
外部tomcat war部署发布
参考博客:https://blog.csdn.net/chen_lay/article/details/87802921 war部署部分
项目启动之后打开管理页面,创建一个job,规则为1分钟执行一次CronJob,如下图:
地址:http://localhost:8083/quartz-job-admin/home
按第11步,把项目在本地部署一个,在centos linux部署一个,然后监控运行日志,如下图:
服务器一:
服务器二:
由执行日志得知:服务器一在按规则执行job,而服务器二一直在监控服务器的调度实例的状态,当服务器二监控到服务器一宕机之后,就会接管任务的执行。
(说明:由于jsp代码比较简单所以在此没有贴出来!!!)
源码地址:https://github.com/1208168209/quartz-job.git
后续补贴链接,或者加QQ:1208168209找本人要
实现思路:
1、页面编辑任务执行类,任务执行类要实现org.quartz.Job,例:
package com.hongguaninfo.quartz.job;
import com.hongguaninfo.hgdf.core.utils.logging.Log;
import com.hongguaninfo.hgdf.core.utils.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Created by chenqinglong on 2019/2/22 0001.
*/
public class GlueJavaJob implements Job {
private Log log = LogFactory.getLog(GlueJavaJob.class);
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("========================GlueJavaJob任务===============================");
log.info("jobName=====:"+jobExecutionContext.getJobDetail().getKey().getName());
log.info("jobGroup=====:"+jobExecutionContext.getJobDetail().getKey().getGroup());
log.info("taskData=====:"+jobExecutionContext.getJobDetail().getJobDataMap().get("glueType"));
}
}
2、后台使用groovyClassLoader.parseClass(String text) 将String解析成java,后续再进行实例化。
3、重写StdJDBCDelegate.selectJobDetail()方法,修改job类的加载方式
4、修改quartz.properties配置文件,修改org.quartz.jobStore.driverDelegateClass参数的值为重写方法所对应的类,此类要继承StdJDBCDelegate:org.quartz.jobStore.driverDelegateClass:com.hongguaninfo.hgdf.core.glue.StdJDBCDelegateMy
5、页面如下图:
package com.hongguaninfo.hgdf.core.glue;
import groovy.lang.GroovyClassLoader;
import org.quartz.Job;
/**
* glue factory, product class/object by name
*
* @author
*/
public class GlueFactory {
private static GlueFactory glueFactory = new GlueFactory();
public static GlueFactory getInstance(){
return glueFactory;
}
public static void refreshInstance(int type){
glueFactory = new GlueFactory();
}
/**
* groovy class loader
*/
private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
/**
* load new instance, prototype
*
* @param codeSource
* @return
* @throws Exception
*/
public Job loadNewInstance(String codeSource) throws Exception{
if (codeSource!=null && codeSource.trim().length()>0) {
Class> clazz = groovyClassLoader.parseClass(codeSource);
if (clazz != null) {
Object instance = clazz.newInstance();
if (instance!=null) {
if (instance instanceof Job) {
this.injectService(instance);
return (Job) instance;
} else {
throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, "
+ "cannot convert from instance["+ instance.getClass() +"] to IJobHandler");
}
}
}
}
throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
}
/**
* inject service of bean field
*
* @param instance
*/
public void injectService(Object instance) {
// do something
}
}
package com.hongguaninfo.hgdf.core.glue;
import com.hongguaninfo.hgdf.core.utils.logging.Log;
import com.hongguaninfo.hgdf.core.utils.logging.LogFactory;
import com.hongguaninfo.quartz.job.CronJob;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.jdbcjobstore.StdJDBCDelegate;
import org.quartz.spi.ClassLoadHelper;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* 自定义的jdbc类,重写selectJobDetail,改变加载job class的方式,
* 继承了org.quartz.impl.jdbcjobstore.StdJDBCDelegate
* Created by chenqinglong on 2019/2/22 0022.
*/
public class StdJDBCDelegateMy extends StdJDBCDelegate {
private Log log = LogFactory.getLog(StdJDBCDelegateMy.class);
@Override
public JobDetail selectJobDetail(Connection conn, JobKey jobKey, ClassLoadHelper loadHelper) throws ClassNotFoundException, IOException, SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
JobDetailImpl map1;
try {
ps = conn.prepareStatement(this.rtp("SELECT * FROM {0}JOB_DETAILS WHERE SCHED_NAME = {1} AND JOB_NAME = ? AND JOB_GROUP = ?"));
ps.setString(1, jobKey.getName());
ps.setString(2, jobKey.getGroup());
rs = ps.executeQuery();
JobDetailImpl job = null;
if(rs.next()) {
job = new JobDetailImpl();
job.setName(rs.getString("JOB_NAME"));
job.setGroup(rs.getString("JOB_GROUP"));
job.setDescription(rs.getString("DESCRIPTION"));
job.setDurability(this.getBoolean(rs, "IS_DURABLE"));
job.setRequestsRecovery(this.getBoolean(rs, "REQUESTS_RECOVERY"));
Map map = null;
if(this.canUseProperties()) {
map = this.getMapFromProperties(rs);
} else {
map = (Map)this.getObjectFromBlob(rs, "JOB_DATA");
}
if(null != map) {
job.setJobDataMap(new JobDataMap(map));
}
if (null != job && null != job.getJobDataMap() && null != job.getJobDataMap().get("glueType") && "GLUE_JAVA".equals(String.valueOf(job.getJobDataMap().get("glueType")))){
//根据job参数判断jobClass是项目内置的类还是页面动态编辑的类:
// 如果是页面编辑的类,那么修改类的加载方式
try {
job.setJobClass(GlueFactory.getInstance().loadNewInstance(String.valueOf(job.getJobDataMap().get("glueJava"))).getClass());
} catch (Exception e) {
e.printStackTrace();
}
}else {
job.setJobClass(loadHelper.loadClass(rs.getString("JOB_CLASS_NAME"), Job.class));
}
}
map1 = job;
} finally {
closeResultSet(rs);
closeStatement(ps);
}
return map1;
}
private Map, ?> getMapFromProperties(ResultSet rs) throws ClassNotFoundException, IOException, SQLException {
InputStream is = (InputStream)this.getJobDataFromBlob(rs, "JOB_DATA");
if(is == null) {
return null;
} else {
Properties properties = new Properties();
if(is != null) {
try {
properties.load(is);
} finally {
is.close();
}
}
Map map = this.convertFromProperty(properties);
return map;
}
}
}
#jdbc类
#org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#自定义的jdbc类,重写selectJobDetail,改变加载job class的方式,继承了org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.driverDelegateClass:com.hongguaninfo.hgdf.core.glue.StdJDBCDelegateMy
源码地址:https://github.com/1208168209/quartz-job.git
cron表达式用于配置cronTrigger的实例。cron表达式实际上是由七个子表达式组成。这些表达式之间用空格分隔。
1.Seconds (秒)
2.Minutes(分)
3.Hours(小时)
4.Day-of-Month (天)
5.Month(月)
6.Day-of-Week (周)
7.Year(年)
例:"0 0 12 ? * WED” 意思是:每个星期三的中午12点执行。
个别子表达式可以包含范围或者列表。例如:上面例子中的WED可以换成"MON-FRI","MON,WED,FRI",甚至"MON-WED,SAT"。
各表达式范围:
1.Seconds (0~59)
2.Minutes (0~59)
3.Hours (0~23)
4.Day-of-Month (1~31,但是要注意有些月份没有31天)
5.Month (0~11,或者"JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV,DEC")
6.Day-of-Week (1~7,1=SUN 或者"SUN, MON, TUE, WED, THU, FRI, SAT”)
7.Year (1970~2099)
Cron表达式的格式:秒 分 时 日 月 周 年(可选)。
字段名 允许的值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日 1-31 , - * ? / L W C
月 1-12 or JAN-DEC , - * /
周几 1-7 or SUN-SAT , - * ? / L C #
年(可选字段) empty 1970-2099 , - * /
* :代表所有可能的值。因此,“*”在Month中表示每个月,在Day-of-Month中表示每天,在Hours表示每小时
- :表示指定范围。
, :表示列出枚举值。例如:在Minutes子表达式中,“5,20”表示在5分钟和20分钟触发。
/ :被用于指定增量。例如:在Minutes子表达式中,“0/15”表示从0分钟开始,每15分钟执行一次。"3/20"表示从第三分钟开始,每20分钟执行一次。和"3,23,43"(表示第3,23,43分钟触发)的含义一样。
? :用在Day-of-Month和Day-of-Week中,指“没有具体的值”。当两个子表达式其中一个被指定了值以后,为了避免冲突,需要将另外一个的值设为“?”。例如:想在每月20日触发调度,不管20号是星期几,只能用如下写法:0 0 0 20 * ?,其中最后以为只能用“?”,而不能用“*”。
L :用在day-of-month和day-of-week字串中。它是单词“last”的缩写。它在两个子表达式中的含义是不同的。
在day-of-month中,“L”表示一个月的最后一天,一月31号,3月30号。
在day-of-week中,“L”表示一个星期的最后一天,也就是“7”或者“SAT”
如果“L”前有具体内容,它就有其他的含义了。例如:“6L”表示这个月的倒数第六天。“FRIL”表示这个月的最后一个星期五。
注意:在使用“L”参数时,不要指定列表或者范围,这样会出现问题。
W :“Weekday”的缩写。只能用在day-of-month字段。用来描叙最接近指定天的工作日(周一到周五)。例如:在day-of-month字段用“15W”指“最接近这个月第15天的工作日”,即如果这个月第15天是周六,那么触发器将会在这个月第14天即周五触发;如果这个月第15天是周日,那么触发器将会在这个月第 16天即周一触发;如果这个月第15天是周二,那么就在触发器这天触发。注意一点:这个用法只会在当前月计算值,不会越过当前月。“W”字符仅能在 day-of-month指明一天,不能是一个范围或列表。也可以用“LW”来指定这个月的最后一个工作日,即最后一个星期五。
# :只能用在day-of-week字段。用来指定这个月的第几个周几。例:在day-of-week字段用"6#3" or "FRI#3"指这个月第3个周五(6指周五,3指第3个)。如果指定的日期不存在,触发器就不会触发。
0 * * * * ? 每1分钟触发一次
0 0 * * * ? 每天每1小时触发一次
0 0 10 * * ? 每天10点触发一次
0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
0 30 9 1 * ? 每月1号上午9点半
0 15 10 15 * ? 每月15日上午10:15触发
*/5 * * * * ? 每隔5秒执行一次
0 */1 * * * ? 每隔1分钟执行一次
0 0 5-15 * * ? 每天5-15点整点触发
0 0/3 * * * ? 每三分钟触发一次
0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0 12 ? * WED 表示每个星期三中午12点
0 0 17 ? * TUES,THUR,SAT 每周二、四、六下午五点
0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
0 0 23 L * ? 每月最后一天23点执行一次
0 15 10 L * ? 每月最后一日的上午10:15触发
0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
0 15 10 * * ? 2005 2005年的每天上午10:15触发
0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发