一 : 首先了解spring定时器所使用到的场景
比如 : 定时统计报表,或者登录账户密码验证失败冻结账户后,每晚12点将账户解锁等等...
使用jar包 : 2.0使用方法不同
<!-- 定时器 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.6</version>
</dependency>
2. 代码示例
1. 定义被执行的任务类
package com.jk.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
// 准备定时调度的任务 工作类
public class TestDing implements Job {
@Override
public void execute(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println(new
SimpleDateFormat("yyyy-MM-dd").format(new Date())+"-
=-=-=-=-=");
}
}
2. 通过 工厂启动任务 主函数运行
package com.jk.controller;
import java.text.ParseException;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class TestDingshiqi {
public static void main(String[] args) throws
SchedulerException, ParseException {
// 实例化 定时器工厂
SchedulerFactory gSchedulerFactory =
new StdSchedulerFactory();
// 通过工厂获得任务调度器
Scheduler sche =
gSchedulerFactory.getScheduler();
// 设置 定时执行的 任务类 设定 任务名
JobDetail jobDetail = new JobDetail("testJob任
意起名字最好和业务相关", "JOB_GROUP_New1",
TestDing.class);// 任务名,任务组,任务执行类
// 获得 触发器 将任务添加到 指定的 触发器中
CronTrigger trigger = new CronTrigger
("testtrigger任意起名字最好和业务相关",
"TRIGGER_GROUP_name1");// 触发器名,触发器组
// 设置触发器的时间
trigger.setCronExpression("0/1 * * * * ?");//
触发器时间设定
//将触发器和 任务 放到 调度器中
sche.scheduleJob(jobDetail, trigger);
// 启动
if (!sche.isShutdown()) {
sche.start();
}
}
}
3. 封装的定时任务管理工具类
package com.jk.controller;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
/**
* Quartz调度管理器
*
* @author Administrator
*
*/
public class TestDingManger {
private static String JOB_GROUP_NAME =
"EXTJWEB_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME =
"EXTJWEB_TRIGGERGROUP_NAME";
/**
* @Description: 添加一个定时任务,使用默认的任务组名
,触发器名,触发器组名
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param cls
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String
jobName, @SuppressWarnings("rawtypes") Class cls,
String time) {
try {
JobDetail jobDetail = new JobDetail
(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执
行类
// 触发器
CronTrigger trigger = new CronTrigger
(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时
间设定
sched.scheduleJob(jobDetail, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 添加一个定时任务
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param triggerGroupName
* 触发器组名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String
jobName, String jobGroupName, String triggerName,
String triggerGroupName, @SuppressWarnings("rawtypes")
Class jobClass, String time) {
try {
JobDetail jobDetail = new JobDetail
(jobName, jobGroupName, jobClass);// 任务名,任务组,任
务执行类
// 触发器
CronTrigger trigger = new CronTrigger
(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时
间设定
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间(使用默认的任
务组名,触发器名,触发器组名)
*
* @param sched
* 调度器
* @param jobName
* @param time
*
* @Title: QuartzManager.java
*/
@SuppressWarnings("rawtypes")
public static void modifyJobTime(Scheduler sched,
String jobName, String time) {
try {
CronTrigger trigger = (CronTrigger)
sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
if (trigger == null) {
return;
}
String oldTime =
trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobDetail jobDetail =
sched.getJobDetail(jobName, JOB_GROUP_NAME);
Class objJobClass =
jobDetail.getJobClass();
removeJob(sched, jobName);
addJob(sched, jobName, objJobClass,
time);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间
*
* @param sched
* 调度器 *
* @param sched
* 调度器
* @param triggerName
* @param triggerGroupName
* @param time
*
* @Title: QuartzManager.java
*/
public static void modifyJobTime(Scheduler sched,
String triggerName, String triggerGroupName, String
time) {
try {
CronTrigger trigger = (CronTrigger)
sched.getTrigger(triggerName, triggerGroupName);
if (trigger == null) {
return;
}
String oldTime =
trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
CronTrigger ct = (CronTrigger)
trigger;
// 修改时间
ct.setCronExpression(time);
// 重启触发器
sched.resumeTrigger(triggerName,
triggerGroupName);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务(使用默认的任务组名,触
发器名,触发器组名)
*
* @param sched
* 调度器
* @param jobName
*
* @Title: QuartzManager.java
*/
public static void removeJob(Scheduler sched,
String jobName) {
try {
sched.pauseTrigger(jobName,
TRIGGER_GROUP_NAME);// 停止触发器
sched.unscheduleJob(jobName,
TRIGGER_GROUP_NAME);// 移除触发器
sched.deleteJob(jobName,
JOB_GROUP_NAME);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务
*
* @param sched
* 调度器
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*
* @Title: QuartzManager.java
*/
public static void removeJob(Scheduler sched,
String jobName, String jobGroupName, String
triggerName, String triggerGroupName) {
try {
sched.pauseTrigger(triggerName,
triggerGroupName);// 停止触发器
sched.unscheduleJob(triggerName,
triggerGroupName);// 移除触发器
sched.deleteJob(jobName, jobGroupName);//
删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:启动所有定时任务
*
* @param sched
* 调度器
*
* @Title: QuartzManager.java
*/
public static void startJobs(Scheduler sched) {
try {
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:关闭所有定时任务
*
*
* @param sched
* 调度器
*
*
* @Title: QuartzManager.java
*/
public static void shutdownJobs(Scheduler sched) {
try {
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
spring项目中整合定时器
一 : spring-quartz.xml
注入schedulerFactoryBean工厂
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
在service层中通过属性注入或者自动注入都可以,这里使用到了自动注入
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
public void test() throws Exception {
// 通过工厂获得任务调度器
Scheduler sche = schedulerFactoryBean.getScheduler();
// 设置 定时执行的 任务类 设定 任务名
JobDetail jobDetail = new JobDetail("testJob任意起名字最好和业务相关", "JOB_GROUP_New1", SpringTask.class);// 任务名,任务组,任务执行类
// 获得 触发器 将任务添加到 指定的 触发器中
CronTrigger trigger = new CronTrigger("testtrigger任意起名字最好和业务相关", "TRIGGER_GROUP_name1");// 触发器名,触发器组
// 设置触发器的时间
trigger.setCronExpression("0/3 * * * * ?");// 触发器时间设定
//将触发器和 任务 放到 调度器中
sche.scheduleJob(jobDetail, trigger);
// 启动
if (!sche.isShutdown()) {
sche.start();
}
}
每次都要使用工厂创建比较麻烦可以将创建一个工具类:
package com.jk.util;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
public class QuartzManager {
private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
/**
* @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param cls
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String jobName, @SuppressWarnings("rawtypes") Class cls, String time) {
try {
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 添加一个定时任务
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param triggerGroupName
* 触发器组名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName, @SuppressWarnings("rawtypes") Class jobClass, String time) {
try {
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
*
* @param sched
* 调度器
* @param jobName
* @param time
*
* @Title: QuartzManager.java
*/
@SuppressWarnings("rawtypes")
public static void modifyJobTime(Scheduler sched, String jobName, String time) {
try {
CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobDetail jobDetail = sched.getJobDetail(jobName, JOB_GROUP_NAME);
Class objJobClass = jobDetail.getJobClass();
removeJob(sched, jobName);
addJob(sched, jobName, objJobClass, time);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间
*
* @param sched
* 调度器 *
* @param sched
* 调度器
* @param triggerName
* @param triggerGroupName
* @param time
*
* @Title: QuartzManager.java
*/
public static void modifyJobTime(Scheduler sched, String triggerName, String triggerGroupName, String time) {
try {
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName, triggerGroupName);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
CronTrigger ct = (CronTrigger) trigger;
// 修改时间
ct.setCronExpression(time);
// 重启触发器
sched.resumeTrigger(triggerName, triggerGroupName);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
*
* @param sched
* 调度器
* @param jobName
*
* @Title: QuartzManager.java
*/
public static void removeJob(Scheduler sched, String jobName) {
try {
sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器
sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器
sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务
*
* @param sched
* 调度器
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*
* @Title: QuartzManager.java
*/
public static void removeJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
try {
sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
sched.deleteJob(jobName, jobGroupName);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:启动所有定时任务
*
* @param sched
* 调度器
*
* @Title: QuartzManager.java
*/
public static void startJobs(Scheduler sched) {
try {
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:关闭所有定时任务
*
*
* @param sched
* 调度器
*
*
* @Title: QuartzManager.java
*/
public static void shutdownJobs(Scheduler sched) {
try {
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}