一、准备工作
1. 在maven的pom文件中添加Quartz的Jar坐标
<!-- quartz 定时器 --> <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>
2. 在web.xml中添加quartz的监听器
<!-- 配置quartz监听器 --> <listener> <listener-class> org.quartz.ee.servlet.QuartzInitializerListener </listener-class> </listener>
3. 添加quartz.properties配置文件
# Configure Main Scheduler Properties org.quartz.scheduler.instanceName: dufy_test org.quartz.scheduler.instanceId = AUTO org.quartz.scheduler.rmi.export: false org.quartz.scheduler.rmi.proxy: false org.quartz.scheduler.wrapJobExecutionInUserTransaction: false # Configure ThreadPool org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount: 2 org.quartz.threadPool.threadPriority: 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true org.quartz.jobStore.misfireThreshold: 60000 # Configure JobStore 持久化配置 #org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties:true # 是否集群 org.quartz.jobStore.isClustered = false # 表前缀 org.quartz.jobStore.tablePrefix:qrtz_ #org.quartz.jobStore.dataSource:qzDS # 下面是数据库的配置,交给Spring管理了 #org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver #org.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/quartz_test #org.quartz.dataSource.qzDS.user:root #org.quartz.dataSource.qzDS.password:123 #org.quartz.dataSource.qzDS.maxConnection:10
4. 添加Spring配置:applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- =========JDBC版=========== --> <!-- 持久化数据配置,需要添加quartz.properties --> <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref ="dataSource" /> <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/> <property name="configLocation" value="classpath:resource/quartz.properties"/> </bean> </beans>
5. 将Quartz自带的表导入Mysql
create table qrtz_job_details( sched_name varchar(120) not null, job_name varchar(80) not null, job_group varchar(80) not null, description varchar(120), job_class_name varchar(128) not null, is_durable integer not null, is_nonconcurrent integer not null, is_update_data integer not null, requests_recovery integer not null, job_data blob(2000), primary key (sched_name,job_name,job_group) ); create table qrtz_triggers( sched_name varchar(120) not null, trigger_name varchar(80) not null, trigger_group varchar(80) not null, job_name varchar(80) not null, job_group varchar(80) not null, description varchar(120), next_fire_time bigint, prev_fire_time bigint, priority integer, trigger_state varchar(16) not null, trigger_type varchar(8) not null, start_time bigint not null, end_time bigint, calendar_name varchar(80), misfire_instr smallint, job_data blob(2000), 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(80) not null, trigger_group varchar(80) not null, repeat_count bigint not null, repeat_interval bigint not null, times_triggered bigint 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(80) not null, trigger_group varchar(80) not null, cron_expression varchar(120) 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(80) not null, trigger_group varchar(80) not null, blob_data blob(2000), 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(80) not null, calendar blob(2000) not null, primary key (calendar_name) ); create table qrtz_fired_triggers( sched_name varchar(120) not null, entry_id varchar(95) not null, trigger_name varchar(80) not null, trigger_group varchar(80) not null, instance_name varchar(80) not null, fired_time bigint not null, sched_time bigint not null, priority integer not null, state varchar(16) not null, job_name varchar(80), job_group varchar(80), is_nonconcurrent integer, requests_recovery integer, primary key (sched_name,entry_id) ); create table qrtz_paused_trigger_grps( sched_name varchar(120) not null, trigger_group varchar(80) not null, primary key (sched_name,trigger_group) ); create table qrtz_scheduler_state( sched_name varchar(120) not null, instance_name varchar(80) not null, last_checkin_time bigint not null, checkin_interval bigint 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) );
6. 自己建立管理定时任务的表,我简单写了几个必须的属性
public class QuartzTask { /* * 这个类用于展示定时的任务,同时作用于定时任务的恢复、删除、中止; **/ private Long jobId; private String jobClass; //任务类的全限定类名 private String jobGroup; //任务组名 private String jobName; //任务名 private String triggerName; //任务触发器名 private String triggerGroupName; //任务触发器组名 private String cronExpr; //时间表达式 private Integer jobStatus; //任务状态 private String startTime; //任务开始时间
}
7. 准备俩简单的Jsp页面用于添加定时任务和定时任务列表展示
大致效果如下:
二、简单代码实现
1.写个任务类,本想做个功能,失败了,就简单控制台输出了
public class MyJob implements Job{ private static final Logger log = LoggerFactory.getLogger(MyJob.class); @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { log.info("MyJob is start .................."); log.info("Hello quzrtz "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())); log.info("MyJob is end ....................."); } }
2. Controller层:QuartzController
@Controller @RequestMapping("user/quartz") public class QuartzController { @Autowired private QuartzTaskService quartzTaskService; /** * 添加定时任务 * */ @RequestMapping(value = "add",method = RequestMethod.POST) public ModelAndView addQtz(QuartzTask qt){ try { //1.成功启动定时任务 this.quartzTaskService.addQtz(qt); //2.封装QuartzTask,执行保存 this.quartzTaskService.saveAddQtz(qt); return new ModelAndView("success"); } catch (Exception e) { e.printStackTrace(); } return new ModelAndView("error"); } /** * 定时任务列表 * */ @RequestMapping(value = "list",method = RequestMethod.GET) public ModelAndView listQtz(){ try { //1.查询所有定时任务 List<QuartzTask> list = this.quartzTaskService.listQtz(); ModelAndView mv = new ModelAndView(); mv.addObject("qtzList",list); mv.setViewName("quartz-list"); return mv; } catch (Exception e) { e.printStackTrace(); } return new ModelAndView("error"); } /** * 删除定时任务 * */ @RequestMapping(value = "delete",method = RequestMethod.GET) public ModelAndView deleteQtz(QuartzTask qt){ try { // 执行删除 this.quartzTaskService.deleteQtz(qt); // 根据id删除定时任务列表数据 this.quartzTaskService.deleteQtzTask(qt.getJobId()); return new ModelAndView("success"); } catch (Exception e) { e.printStackTrace(); } return new ModelAndView("error"); } /** * 中止定时任务 * */ @RequestMapping(value = "pause",method = RequestMethod.GET) public ModelAndView pauseQtz(QuartzTask qt){ try { // 执行中止 this.quartzTaskService.pauseQtz(qt); return new ModelAndView("success"); } catch (Exception e) { e.printStackTrace(); } return new ModelAndView("error"); } /** * 恢复定时任务 * */ @RequestMapping(value = "resume",method = RequestMethod.GET) public ModelAndView resumeQtz(QuartzTask qt){ try { // 执行中止 this.quartzTaskService.resumeQtz(qt); return new ModelAndView("success"); } catch (Exception e) { e.printStackTrace(); } return new ModelAndView("error"); } }
3. Service层:QuartzTaskServiceImpl
@Service public class QuartzTaskServiceImpl implements QuartzTaskService { @Autowired private QuartzDao quartzDao; @Autowired private Scheduler scheduler; @Override public void addQtz(QuartzTask qt) { try { // 1.创建一个JobDetail实例,指定Quartz String cla = qt.getJobClass(); Class clazz = Class.forName(cla); JobDetail jobDetail = JobBuilder.newJob(clazz) // 任务执行类 .withIdentity(qt.getJobName(), qt.getJobGroup())// 任务名,任务组 .build(); CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule(qt.getCronExpr()); // 2.创建Trigger(触发器) Trigger trigger = TriggerBuilder.newTrigger().withIdentity(qt.getTriggerName(), qt.getTriggerGroupName()) .startNow().withSchedule(builder).build(); // 3.告诉调度器使用该触发器来安排作业 scheduler.scheduleJob(jobDetail, trigger); // 4.启动 if (!scheduler.isShutdown()) { scheduler.start(); } } catch (Exception e) { throw new RuntimeException(e); } } @Override public void saveAddQtz(QuartzTask qt) { qt.setJobId(1l); qt.setJobStatus(1); qt.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())); this.quartzDao.saveAddQtz(qt); } @Override public List<QuartzTask> listQtz() { return quartzDao.listQtz(); } @Override public void deleteQtz(QuartzTask qt) { try { // 停止触发器 scheduler.pauseTrigger(TriggerKey.triggerKey(qt.getTriggerName(), qt.getTriggerGroupName())); // 移除触发器 scheduler.unscheduleJob(TriggerKey.triggerKey(qt.getTriggerName(), qt.getTriggerGroupName())); // 删除任务 scheduler.deleteJob(JobKey.jobKey(qt.getJobName(), qt.getJobGroup())); } catch (Exception e) { throw new RuntimeException(e); } } @Override public void deleteQtzTask(Long jobId) { this.quartzDao.deleteQtzTask(jobId); } @Override public void pauseQtz(QuartzTask qt) { try { scheduler.pauseJob(JobKey.jobKey(qt.getJobName(), qt.getJobGroup())); } catch (SchedulerException e) { e.printStackTrace(); } } @Override public void resumeQtz(QuartzTask qt) { try { scheduler.resumeJob(JobKey.jobKey(qt.getJobName(), qt.getJobGroup())); } catch (SchedulerException e) { e.printStackTrace(); } } }
4. 与数据库交互的QuartzTask.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- 根标签,对应一个Dao接口 -->
<mapper namespace="cn.jindou.crm.mapper.QuartzDao">
<resultMap type="cn.jindou.crm.pojo.QuartzTask" id="quartzMap">
<id column="job_id" property="jobId"/>
<result column="job_class" property="jobClass"/>
<result column="job_group" property="jobGroup"/>
<result column="job_name" property="jobName"/>
<result column="trigger_name" property="triggerName"/>
<result column="trigger_groupName" property="triggerGroupName"/>
<result column="cron_expr" property="cronExpr"/>
<result column="job_status" property="jobStatus"/>
<result column="start_time" property="startTime"/>
</resultMap>
<insert id="saveAddQtz" parameterType="cn.jindou.crm.pojo.QuartzTask">
INSERT INTO quartz
(job_id,job_class,job_group,job_name,trigger_name,trigger_groupName,cron_expr,job_status,start_time)
VALUES(#{jobId},#{jobClass},#{jobGroup},#{jobName},#{triggerName},#{triggerGroupName},#{cronExpr},#{jobStatus},#{startTime});
</insert>
<select id="listQtz" resultMap="quartzMap">
select * from quartz;
</select>
<delete id="deleteQtzTask" parameterType="java.lang.Long">
DELETE from quartz where job_id = #{jobId};
</delete>
</mapper>
我这里只做了简单实现,还有很多地方不懂,继续努力。