Quartz 持久化到MySQL 并创建触发器
作者:互联网
示例:
pom.xml
<!-- quartz -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
实现步骤:
1.
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.config.ConfigurationJSON;
import org.example.config.quartz.DelQuartzJob;
import org.example.utils.QuartzCronDateUtils;
import org.example.config.quartz.AddQuartzJob;
import org.example.model.common.CommonResult;
import org.example.model.common.JsonTMSBean;
import org.example.model.entity.PersonnelAccessAuthority;
import org.example.service.PersonnelReceivingService;
import org.example.service.QuartzService;
import org.example.utils.HttpClientUtils;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author
* @version 1.0
* @PACKAGE_NAME: org.example.service.impl
* @date 2021/11/29 14:41 周一
*/
@Slf4j
@Service
public class PersonnelReceivingServiceImpl implements PersonnelReceivingService {
@Autowired
QuartzService quartzService;
@Autowired
Scheduler scheduler;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map map = new HashMap(2);
@Override
public String receiveLeaveCancellation( ) {
try {
String personCode1 ="2222222";
String startTime ="2021-10-17 11:45:00";
String endTime ="2021-10-17 11:45:00";
有效期开始时间
String personCodeTime=personCode1+"_"+startTime;
map.put("personCode",personCode1);
Date parse = sdf.parse(startTime);
long starTime = parse.getTime();
//10分钟
long time1= 10*60*1000;
Date startDate = new Date(starTime - time1);
String cron = QuartzCronDateUtils.getCron(startDate);
JobKey jobKey = JobKey.jobKey("addJob","addGroup");
JobDetail jobDetail1 = scheduler.getJobDetail(jobKey);
if(jobDetail1==null){
quartzService.addJob(AddQuartzJob.class,"addJob", "addGroup",personCodeTime, cron,map);
}else {
quartzService.addTrigger(jobDetail1, personCodeTime,"addGroup", cron,map);
}
//有效期截止时间
String personCodeEndTime=personCode1+"_"+endTime;
Date parseendTime = sdf.parse(endTime);
long timeTime = parseendTime.getTime();
//13分钟
long time2= 30*60*1000;
Date endDate = new Date(timeTime + time2);
String endCron = QuartzCronDateUtils.getCron(endDate);
JobKey updatejobKey = JobKey.jobKey("updateJob","updateGroup");
JobDetail jobDetailUpdate = scheduler.getJobDetail(updatejobKey);
if(jobDetailUpdate==null){
quartzService.delJob(DelQuartzJob.class,"updateJob", "updateGroup",personCodeEndTime, endCron,map);
}else {
quartzService.delTrigger(jobDetailUpdate, personCodeEndTime,"updateGroup", endCron,map);
}
}catch (Exception ex){
log.error(personnelAccessAuthority +"人员信息数据转换失败:"+ex.getMessage());
}
return null
}
}
package org.example.service;
import org.example.config.quartz.AddQuartzJob;
import org.example.config.quartz.DelQuartzJob;
import org.quartz.JobDetail;
import java.util.Map;
/**
* @author
* @version 1.0
* @PACKAGE_NAME: org.example.service
* @date 2021/12/8 14:53 周三
*/
public interface QuartzService {
/**
* 增加一个job
* @param jobClass
* 任务实现类
* @param jobName
* 任务名称(建议唯一)
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param jobTime
* 时间表达式 (如:0/5 * * * * ? )
* @param jobData
* @return
*/
void addJob(Class<AddQuartzJob> jobClass, String jobName, String jobGroupName, String triggerName, String jobTime, Map jobData);
void addTrigger(JobDetail jobDetail, String personCodeTime, String jobGroupName, String cron, Map map);
void delJob(Class<DelQuartzJob> delQuartzJobClass, String updateJob, String updateGroup, String personCodeEndTime, String endCron, Map map);
void delTrigger(JobDetail jobDetailUpdate, String personCodeEndTime, String updateGroup, String endCron, Map map);
}
package org.example.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.example.config.quartz.AddQuartzJob;
import org.example.config.quartz.DelQuartzJob;
import org.example.service.QuartzService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
/**
* quartz逻辑
* @author gourd
*/
@Slf4j
@Service
public class QuartzServiceImpl implements QuartzService {
@Autowired
private Scheduler scheduler;
@PostConstruct
public void startScheduler() {
try {
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
/**
* 添加出入权限任务
* @param jobClass
* 任务实现类
* @param jobName
* 任务名称(建议唯一)
* @param jobGroupName
* 任务组名
* TriggerName 触发器名称
* @param jobTime
*
* 时间表达式
* @param jobData
*/
@Override
public void addJob(Class<AddQuartzJob> jobClass,
String jobName,
String jobGroupName,
String triggerName,
String jobTime,
Map jobData) {
try {
// 创建jobDetail实例,绑定Job实现类
// 指明job的名称,所在组的名称,以及绑定job类
// 任务名称和组构成任务key
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName)
.build();
// 设置job参数jobName
if(jobData!= null && jobData.size()>0){
jobDetail.getJobDataMap().putAll(jobData);
}
// 定义调度触发规则
// 使用cornTrigger规则
// 触发器key
Trigger trigger = newTrigger().withIdentity(triggerName, jobGroupName)
.startAt(DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
.withSchedule(cronSchedule(jobTime)).build();
// 把作业和触发器注册到任务调度中
scheduler.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
log.error("添加出入权限任务失败:"+e.getMessage());
}
}
/**
* 添加出入权限触发器
* @param jobDetail1
* @param personCodeTime
* @param jobGroupName
* @param cron
* @param jobData
*/
@Override
public void addTrigger(JobDetail jobDetail1, String personCodeTime, String jobGroupName, String cron, Map jobData) {
try {
// 设置job参数jobName
if(jobData!= null && jobData.size()>0){
jobDetail1.getJobDataMap().putAll(jobData);
}
// 定义调度触发规则
// 把作业和触发器注册到任务调度中
scheduler.scheduleJob(newTrigger().withIdentity(new TriggerKey(personCodeTime, jobGroupName)).withSchedule(
cronSchedule(cron)).forJob(jobDetail1).build());
} catch (Exception e) {
log.error("添加出入权限触发器失败:"+e.getMessage());
}
}
/**
* 新增修改权限job
* @param jobClass
* @param jobName
* @param jobGroupName
* @param triggerName
* @param jobTime
* @param jobData
*/
@Override
public void delJob(Class<DelQuartzJob> jobClass,
String jobName,
String jobGroupName,
String triggerName,
String jobTime,
Map jobData) {
try {
// 创建jobDetail实例,绑定Job实现类
// 指明job的名称,所在组的名称,以及绑定job类
// 任务名称和组构成任务key
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName)
.build();
// 设置job参数jobName
if(jobData!= null && jobData.size()>0){
jobDetail.getJobDataMap().putAll(jobData);
}
// 定义调度触发规则
// 使用cornTrigger规则
// 触发器key
Trigger trigger = newTrigger().withIdentity(triggerName, jobGroupName)
.startAt(DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
.withSchedule(cronSchedule(jobTime)).build();
// 把作业和触发器注册到任务调度中
scheduler.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
log.error("修改出入任务失败:"+e.getMessage());
}
}
/**
* 添加修改出入权限触发器
* @param jobDetailUpdate
* @param personCodeEndTime
* @param updateGroup
* @param endCron
* @param jobData
*/
@Override
public void delTrigger(JobDetail jobDetailUpdate, String personCodeEndTime, String updateGroup, String endCron, Map jobData) {
try {
// 设置job参数jobName
if (jobData != null && jobData.size() > 0) {
jobDetailUpdate.getJobDataMap().putAll(jobData);
}
// 定义调度触发规则
// 把作业和触发器注册到任务调度中
scheduler.scheduleJob(newTrigger().withIdentity(new TriggerKey(personCodeEndTime, updateGroup)).withSchedule(
cronSchedule(endCron)).forJob(jobDetailUpdate).build());
} catch (Exception e) {
log.error("添加修改出入权限触发器失败:" + e.getMessage());
}
}
}
package org.example.config.quartz;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.config.ConfigurationJSON;
import org.example.model.common.DataModel;
import org.example.model.common.JsonTMSBean;
import org.example.model.common.TmsLeaveCancelPerson;
import org.example.utils.HttpClientUtils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
/**
* 业务处理
* @author gourd
*/
@Component
public class AddQuartzJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0) {
JsonTMSBean jsonTMSBean=null;
//人员权限信息
TmsLeaveCancelPerson tmsLeaveCancelPerson =null;
// 获取参数
JobDataMap jobDatQuartzCronDateUtilsaMap = arg0.getJobDetail().getJobDataMap();
System.out.println(new Date() +" 新增执行的业务====="+JSON.toJSONString(jobDatQuartzCronDateUtilsaMap));
// 业务逻辑
String personCode = jobDatQuartzCronDateUtilsaMap.getString("personCode");
}
}
package org.example.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author
* @version 1.0
* @PACKAGE_NAME: org.example.config.quartz
* @date 2021/12/8 14:52 周三
*/
public class QuartzCronDateUtils {
/***
* 日期转换cron表达式时间格式
* @param date
* @param dateFormat
* @return
*/
public static String formatDateByPattern(Date date,String dateFormat){
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String formatTimeStr = null;
if (date != null) {
formatTimeStr = sdf.format(date);
}
return formatTimeStr;
}
/***
* convert Date to cron
* @param date:时间
* @return
*/
public static String getCron(Date date){
String dateFormat="ss mm HH dd MM ? yyyy";
return formatDateByPattern(date,dateFormat);
}
}
标签:Quartz,String,MySQL,param,化到,jobData,org,import,example 来源: https://blog.csdn.net/qq_39069718/article/details/121947208