导入相应的jar包
<!-- quartz 的jar -->
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
创建QuartzUtils工具类
import com.vrv.platform.domain.TimingSetting;
import com.vrv.platform.orm.zzb.TimingSettingMapper;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class QuartzUtils {
private static final Logger log = LoggerFactory.getLogger(QuartzUtils.class);
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
/**
* 功能: 添加一个定时任务
*
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务的类类型 eg:TimedMassJob.class
* @param cron 时间设置 表达式,参考quartz说明文档
* @param objects 可变参数需要进行传参的值
*/
public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron, Object... objects) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
// 任务名,任务组,任务执行类
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
// 触发器
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
//该数据可以通过Job中的JobDataMap dataMap = context.getJobDetail().getJobDataMap();来进行参数传递值
jobDetail.getJobDataMap().put("data" + (i + 1), objects[i]);
}
}
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
// 触发器名,触发器组
triggerBuilder.withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
// 触发器时间设定
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
// 创建Trigger对象
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
// 调度容器设置JobDetail和Trigger
scheduler.scheduleJob(jobDetail, trigger);
// 启动
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 功能:修改一个任务的触发时间
*
* @param jobName
* @param jobGroupName
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param cron 时间设置,参考quartz说明文档
*/
public static void modifyJobTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(cron)) {
// 触发器
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
// 触发器名,触发器组
triggerBuilder.withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
// 触发器时间设定
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
// 创建Trigger对象
trigger = (CronTrigger) triggerBuilder.build();
// 方式一 :修改一个任务的触发时间
scheduler.rescheduleJob(triggerKey, trigger);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 功能: 移除一个任务
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*/
public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
// 停止触发器
scheduler.pauseTrigger(triggerKey);
// 移除触发器
scheduler.unscheduleJob(triggerKey);
// 删除任务
scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Autowired
private TimingSettingMapper settingMapper;
private static TimingSettingMapper settingMapperStatic;
@PostConstruct
public void init() {
settingMapperStatic = this.settingMapper;
}
@PostConstruct
public static void post() {
log.debug("---------容器启动 初始化定时任务----------");
TimingSetting allSetting = settingMapperStatic.findAll(1);
TimingSetting addSetting = settingMapperStatic.findAll(2);
if (allSetting.getWay() == 0){
log.debug("---------停用全量备份定时任务----------");
QuartzUtils.removeJob("all", "allList", "all", "alls");
return;
}
if (addSetting.getWay() == 0){
log.debug("---------停用增量备份定时任务----------");
QuartzUtils.removeJob("add", "addList", "add", "adds");
return;
}
if (allSetting != null && allSetting.getWay() != 0 ) {
log.debug("---------创建全量备份定时任务----------");
QuartzUtils.removeJob("all", "allList", "all", "alls");
if (allSetting.getBackuoTime()!=null && !"".equals(allSetting.getBackuoTime())){
QuartzUtils.addJob("all", "allList", "all", "alls", FullTimingQuartzJob.class, allSetting.getBackuoTime());
}
}
if (addSetting != null && addSetting.getWay() != 0) {
log.debug("---------创建增量备份定时任务----------");
QuartzUtils.removeJob("add", "addList", "add", "adds");
if (addSetting.getBackuoTime()!=null && !"".equals(addSetting.getBackuoTime())){
QuartzUtils.addJob("add", "addList", "add", "adds", IncrementalTimingQuartzJob.class, addSetting.getBackuoTime());
}
}
}
}
创建执行定时任务的类,实现org.quartz.Job接口
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vrv.platform.service.IDataBackupService;
import com.vrv.platform.util.SpringUtil;
import javax.annotation.PostConstruct;
/**
* @version V1.0
* @Description: 全量备份定时
* @date 2019/6/2512:57
*/
@Component
public class FullTimingQuartzJob implements org.quartz.Job {
private static final Logger log = LoggerFactory.getLogger(FullTimingQuartzJob.class);
private IDataBackupService dataBackupService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
log.debug("---------全量备份定时任务开始----------");
dataBackupService = SpringUtil.getBean(IDataBackupService.class);
dataBackupService.allBackup();
}
}
这边定义了两个定时任务
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vrv.platform.service.IDataBackupService;
import com.vrv.platform.util.SpringUtil;
/**
* @version V1.0
* @Description: 增量备份定时
* @date 2019/6/2512:57
*/
@Component
public class IncrementalTimingQuartzJob implements org.quartz.Job {
private static final Logger log = LoggerFactory.getLogger(IncrementalTimingQuartzJob.class);
private IDataBackupService dataBackupService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
log.debug("---------增量备份定时任务开始----------");
dataBackupService = SpringUtil.getBean(IDataBackupService.class);
dataBackupService.incrementalCackup();
}
}
在需要改变定时任务的方法中调用工具类,传入相应的参数即可改变定时任务;
案例来源:https://www.cnblogs.com/yipiaoganlu/p/9229874.html;写的非常详细,学习到了很多;工具栏中有更多的功能如:停止全部任务,启动全部任务。我这里只取了自己需要的功能。