pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
spring配置文件application.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">
<context:annotation-config></context:annotation-config>
<!-- 扫描com.quartz包下的文件 -->
<context:component-scan base-package="com.quartz"></context:component-scan>
</beans>
spring工具类SpringUtils
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtils implements ApplicationContextAware{
public static ApplicationContext applicationContext;
/**
* 初始化applicationContext对象
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
/**
* 获取对象
* @param beanName
* @return
*/
public static Object getBean(String beanName){
return applicationContext.getBean(beanName);
}
}
Quartz工具类QuartzUtils
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
/**
* 定时任务工具类
* 实现功能包括:添加任务、移除任务、修改任务时间、启动所有任务
* @author LWB
*/
public class QuartzUtils {
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
/**
* 添加定时任务
* @param jobName 任务名
* @param jobGroup 任务组
* @param triggerName 触发器名
* @param triggerGroup 触发器组
* @param cronExpression 触发时间表达式
* @param beanName 任务类的bean ID
*/
public static void addJob(String jobName,String jobGroup,String triggerName,String triggerGroup,String cronExpression,String beanName){
try {
Scheduler scheduler = schedulerFactory.getScheduler();
//任务名,任务组,任务类
Job job = (Job) SpringUtils.getBean(beanName);
JobDetail jobDetail = JobBuilder.newJob(job.getClass()).withIdentity(jobName, jobGroup).build();
//触发器名,触发器组
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup);
triggerBuilder.startNow();
//触发时间设定
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
//创建Trigger对象
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}finally {
System.out.println("......添加定时任务......");
}
}
/**
* 修改一个任务的时间的触发时间
* @param jobName 任务名
* @param jobGroup 任务组
* @param triggerName 触发器名
* @param triggerGroup 触发器组
* @param cronExpression 触发时间表达式
*/
public static void modifyJob(String jobName,String jobGroup,String triggerName,String triggerGroup,String cronExpression){
try {
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if(oldTrigger == null){
return ;
}
//获取当前触发器的时间表达式
String oldCronExpression = oldTrigger.getCronExpression();
if( !oldCronExpression.equalsIgnoreCase(cronExpression) ){
//触发器名,触发器组
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName,triggerGroup);
triggerBuilder.startNow();
//设置任务触发的时间表达式
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
//创建Trigger对象
CronTrigger newTrigger = (CronTrigger) triggerBuilder.build();
scheduler.rescheduleJob(triggerKey, newTrigger);
}
} catch (SchedulerException e) {
e.printStackTrace();
}finally {
System.out.println("......修改定时任务......");
}
}
/**
* 启动所有任务
*/
public static void startJob(){
try {
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}finally {
System.out.println("......启动所有定时任务......");
}
}
/**
* 移除一个任务
* @param jobName 任务名
* @param jobGroup 任务组
* @param triggerName 触发器名
* @param triggerGroup 触发器组
*/
public static void removeJob(String jobName,String jobGroup,String triggerName,String triggerGroup){
try {
Scheduler scheduler = schedulerFactory.getScheduler();
//移除触发器
scheduler.unscheduleJob(TriggerKey.triggerKey(triggerName, triggerGroup));
//删除任务
scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
} catch (SchedulerException e) {
e.printStackTrace();
}finally {
System.out.println("......移除一个定时任务......");
}
}
}
单元测试Quartz工具类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application4.xml"})
public class MyTestOne {
@Test
public void one() throws InterruptedException{
QuartzUtils.addJob("job", "one", "trigger", "one", "0/1 * * * * ?", "taskOne");
QuartzUtils.addJob("job", "two", "trigger", "two", "0/1 * * * * ?", "taskTwo");
Thread.sleep(3000);
QuartzUtils.modifyJob("job", "one", "trigger", "one", "0/3 * * * * ?");
Thread.sleep(6000);
}
}