package com.supwisdom.portal.service.quartz;
import com.supwisdom.portal.utils.CalendarUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
/**
* Created by jyl on 2020/6/2.
*/
@Service
public class QuartzManager {
private static SchedulerFactory SchedulerFactory = new StdSchedulerFactory();
// 任务调度
@Autowired
private Scheduler scheduler;
/**
* 开始执行任务
*
* @throws SchedulerException
*/
public void startJob(String jobName, String jobGroup, String cron) throws SchedulerException {
//查到信息进行修改操作 查不到进行新增操作
String jobInfo = getJobInfo(jobName, jobGroup);
if (StringUtils.isBlank(jobInfo)) {
startJob(scheduler, jobName, jobGroup, formatDateByPattern(CalendarUtils.format(cron, CalendarUtils.DATETIME_FORMAT)));
} else {
modifyJob(jobName, jobGroup, cron);
}
}
/**
* 获取Job信息
*
* @param name
* @param group
* @return
* @throws SchedulerException
*/
public String getJobInfo(String name, String group) throws SchedulerException {
TriggerKey triggerKey = new TriggerKey(name, group);
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (null == cronTrigger ) {
return null;
}
return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
scheduler.getTriggerState(triggerKey).name());
}
/**
* 修改某个任务的执行时间
*
* @param name
* @param group
* @param time
* @return
* @throws SchedulerException
*/
public boolean modifyJob(String name, String group, String time) throws SchedulerException {
String newTime = formatDateByPattern(CalendarUtils.format(time, CalendarUtils.DATETIME_FORMAT));
Date date = null;
TriggerKey triggerKey = new TriggerKey(name, group);
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
String oldTime = cronTrigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(newTime)) {
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(newTime);
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
.withSchedule(cronScheduleBuilder).build();
date = scheduler.rescheduleJob(triggerKey, trigger);
}
return date != null;
}
/**
* 暂停所有任务
*
* @throws SchedulerException
*/
public void pauseAllJob() throws SchedulerException {
scheduler.pauseAll();
}
/**
* 暂停某个任务
*
* @param name
* @param group
* @throws SchedulerException
*/
public void pauseJob(String name, String group) throws SchedulerException {
JobKey jobKey = new JobKey(name, group);
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail == null)
return;
scheduler.pauseJob(jobKey);
}
/**
* 恢复所有任务
*
* @throws SchedulerException
*/
public void resumeAllJob() throws SchedulerException {
scheduler.resumeAll();
}
/**
* 恢复某个任务
*
* @param name
* @param group
* @throws SchedulerException
*/
public void resumeJob(String name, String group) throws SchedulerException {
JobKey jobKey = new JobKey(name, group);
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail == null)
return;
scheduler.resumeJob(jobKey);
}
/**
* 删除某个任务
*
* @param name
* @param group
* @throws SchedulerException
*/
public void deleteJob(String name, String group) throws SchedulerException {
JobKey jobKey = new JobKey(name, group);
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail == null)
return;
scheduler.deleteJob(jobKey);
}
private void startJob(Scheduler scheduler, String jobName, String jobGroup, String cron) throws SchedulerException {
// 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例
// JobDetail 是具体Job实例
JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob.class).withIdentity(jobName, jobGroup).build();
// 基于表达式构建触发器
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
// CronTrigger表达式触发器 继承于Trigger
// TriggerBuilder 用于构建触发器实例
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup)
.withSchedule(cronScheduleBuilder).build();
scheduler.scheduleJob(jobDetail, cronTrigger);
}
private static final SimpleDateFormat sdf = new SimpleDateFormat("ss mm HH dd MM ?");
/***
* 功能描述:日期转换cron表达式
* @param date
* @return
*/
public static String formatDateByPattern(Date date) {
String formatTimeStr = null;
if (Objects.nonNull(date)) {
formatTimeStr = sdf.format(date);
}
return formatTimeStr;
}
/***
* convert Date to cron, eg "0 07 10 15 1 ?"
* @param date : 时间点
* @return
*/
public static String getCron(Date date) {
return formatDateByPattern(date);
}
}
根据需求编写业务逻辑
package com.supwisdom.portal.service.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Created by jyl on 2020/6/2.
*/
public class SchedulerQuartzJob implements Job{
private void before(){
System.out.println("任务执行开始");
}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
before();
System.out.println("开始:"+System.currentTimeMillis());
// TODO 业务
System.out.println("结束:"+System.currentTimeMillis());
after();
}
private void after(){
System.out.println("任务执行结束");
}
}
pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>