com.github.rexsheng.springboot.faster.system.job.domain.QuartzTool Maven / Gradle / Ivy
The newest version!
package com.github.rexsheng.springboot.faster.system.job.domain;
import com.github.rexsheng.springboot.faster.common.constant.CommonConstant;
import com.github.rexsheng.springboot.faster.quartz.builder.DynamicJobBuilder;
import com.github.rexsheng.springboot.faster.util.DateUtil;
import org.quartz.*;
import org.springframework.util.ObjectUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class QuartzTool {
private Scheduler scheduler;
public QuartzTool(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void startTrigger(SysJob job){
JobKey jobKey=buildJobKey(job);
JobDetail jobDetail=buildJobDetail(job);
for(SysJob.SysJobTrigger jobTrigger : job.getTriggerList()){
TriggerKey triggerKey=buildTriggerKey(jobTrigger);
try {
TriggerBuilder triggerBuilder=TriggerBuilder.newTrigger().withIdentity(triggerKey)
.withDescription(jobTrigger.getTriggerName())
.forJob(jobKey);
if(jobTrigger.getStartTime()!=null){
triggerBuilder.startAt(DateUtil.toDate(jobTrigger.getStartTime()));
}
if(jobTrigger.getEndTime()!=null){
triggerBuilder.endAt(DateUtil.toDate(jobTrigger.getEndTime()));
}
if(jobTrigger.getPriority()!=null){
triggerBuilder.withPriority(jobTrigger.getPriority());
}
if(CommonConstant.JOB_TRIGGER_TYPE_CRON.equals(jobTrigger.getExecuteType())){
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobTrigger.getExecuteExpression());
if(CommonConstant.JOB_MISFIRE_CRON_DONOTHING.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionDoNothing();
}
else if(CommonConstant.JOB_MISFIRE_CRON_IGNORE.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
}
else if(CommonConstant.JOB_MISFIRE_CRON_ONCE.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
}
triggerBuilder.withSchedule(scheduleBuilder);
}
else if(CommonConstant.JOB_TRIGGER_TYPE_SIMPLE.equals(jobTrigger.getExecuteType())){
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
if(CommonConstant.JOB_MISFIRE_SIMPLE_FIRE_NOW.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionFireNow();
}
else if(CommonConstant.JOB_MISFIRE_SIMPLE_IGNORE.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
}
else if(CommonConstant.JOB_MISFIRE_SIMPLE_NEXT_EXIST.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionNextWithExistingCount();
}
else if(CommonConstant.JOB_MISFIRE_SIMPLE_NEXT_REMAIN.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionNextWithRemainingCount();
}
else if(CommonConstant.JOB_MISFIRE_SIMPLE_NOW_EXIST.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionNowWithExistingCount();
}
else if(CommonConstant.JOB_MISFIRE_SIMPLE_NOW_REMAIN.equals(jobTrigger.getMisfireStrategy())){
scheduleBuilder.withMisfireHandlingInstructionNowWithRemainingCount();
}
Map setting= jobTrigger.getTriggerInterval();
if(setting.containsKey("interval")){
Integer interval=Integer.valueOf(String.valueOf(setting.get("interval").toString()));
if(interval!=null && interval>0){
scheduleBuilder.withIntervalInSeconds(interval);
}
}
if(setting.containsKey("repeat")){
Integer repeatCount=Integer.valueOf(String.valueOf(setting.get("repeat").toString()));
if(repeatCount!=null){
scheduleBuilder.withRepeatCount(repeatCount);
}
}
triggerBuilder.withSchedule(scheduleBuilder);
}
Trigger trigger = triggerBuilder.build();
if(scheduler.checkExists(triggerKey)){
scheduler.rescheduleJob(triggerKey,trigger);
}
else {
// if(scheduler.checkExists(jobKey)){
// Set sets=new HashSet<>();
// sets.add(trigger);
// scheduler.scheduleJob(jobDetail,sets,true);
// }
// else{
// scheduler.scheduleJob(jobDetail, trigger);
// }
Set sets=new HashSet<>();
sets.add(trigger);
scheduler.scheduleJob(jobDetail,sets,true);
}
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
}
public void pauseTrigger(SysJob.SysJobTrigger trigger){
TriggerKey triggerKey=buildTriggerKey(trigger);
try {
scheduler.pauseTrigger(triggerKey);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public void resumeTrigger(SysJob.SysJobTrigger trigger){
TriggerKey triggerKey=buildTriggerKey(trigger);
try {
scheduler.resumeTrigger(triggerKey);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public void stopTrigger(SysJob.SysJobTrigger trigger){
TriggerKey triggerKey=buildTriggerKey(trigger);
try {
scheduler.unscheduleJob(triggerKey);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public void deleteJobWithoutTriggers(SysJob job){
JobKey jobKey=buildJobKey(job);
try {
if(ObjectUtils.isEmpty(scheduler.getTriggersOfJob(jobKey))){
scheduler.deleteJob(jobKey);
}
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public void deleteJobs(List jobList){
try {
scheduler.deleteJobs(jobList.stream().map(data->buildJobKey(data)).collect(Collectors.toList()));
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public void executeJobOnce(SysJob job){
JobKey jobKey=buildJobKey(job);
try {
if(!scheduler.checkExists(jobKey)){
storeJob(job,false);
}
scheduler.triggerJob(jobKey,getJobDataMap(job));
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public void storeJob(SysJob job, Boolean replace){
try {
scheduler.addJob(buildJobDetail(job),replace,true);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
private JobDataMap getJobDataMap(SysJob sysJob){
JobDataMap jobDataMap=new JobDataMap();
if(!ObjectUtils.isEmpty(sysJob.getErrorMail())){
jobDataMap.put("errorMail",sysJob.getErrorMail());
}
if(sysJob.getExecuteParameter()!=null){
sysJob.getExecuteParameter().entrySet().forEach(entry->jobDataMap.put(entry.getKey(),entry.getValue()));
}
return jobDataMap;
}
public JobDetail buildJobDetail(SysJob job) {
JobKey jobKey = buildJobKey(job);
DynamicJobBuilder jobBuilder = DynamicJobBuilder.newJob()
.concurrent(job.getConcurrency());
if (job.getExecuteClass().indexOf(".") > -1) {
try {
jobBuilder.targetClass(Class.forName(job.getExecuteClass()));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
jobBuilder.targetBeanName(job.getExecuteClass());
}
jobBuilder.targetMethod(job.getExecuteMethod())
.withIdentity(jobKey)
.withDescription(job.getJobName())
.usingJobData(getJobDataMap(job))
.storeDurably()
.requestRecovery(false)
;
JobDetail jobDetail = jobBuilder.build();
return jobDetail;
}
public JobKey buildJobKey(SysJob job){
return buildJobKey(job.getJobId());
}
public JobKey buildJobKey(Long jobId){
return JobKey.jobKey(jobId.toString(),JobKey.DEFAULT_GROUP);
}
public TriggerKey buildTriggerKey(SysJob.SysJobTrigger trigger){
return TriggerKey.triggerKey(trigger.getTriggerId().toString(),TriggerKey.DEFAULT_GROUP);
}
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy