14 changed files with 725 additions and 28 deletions
@ -1,9 +1,24 @@ |
|||||
package com.ccsens.scheduler; |
package com.ccsens.scheduler; |
||||
|
|
||||
|
import org.mybatis.spring.annotation.MapperScan; |
||||
|
import org.springframework.boot.SpringApplication; |
||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
import org.springframework.boot.web.servlet.ServletComponentScan; |
||||
|
import org.springframework.scheduling.annotation.EnableAsync; |
||||
|
import org.springframework.scheduling.annotation.EnableScheduling; |
||||
|
|
||||
/** |
/** |
||||
* @description: |
* @description: |
||||
* @author: whj |
* @author: whj |
||||
* @time: 2021/2/20 17:39 |
* @time: 2021/2/20 17:39 |
||||
*/ |
*/ |
||||
|
@EnableScheduling |
||||
|
@MapperScan(basePackages = {"com.ccsens.scheduler.persist.*"}) |
||||
|
@ServletComponentScan |
||||
|
@EnableAsync |
||||
|
@SpringBootApplication(scanBasePackages = "com.ccsens.scheduler") |
||||
public class SchedulerApplication { |
public class SchedulerApplication { |
||||
|
public static void main(String[] args) { |
||||
|
SpringApplication.run(SchedulerApplication.class, args); |
||||
|
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,31 @@ |
|||||
|
package com.ccsens.scheduler.api; |
||||
|
|
||||
|
|
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiImplicitParams; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import com.ccsens.scheduler.util.JsonResponse; |
||||
|
|
||||
|
@Api(tags = "DEBUG" , description = "DebugController | ") |
||||
|
@RestController |
||||
|
@RequestMapping("/debug") |
||||
|
@Slf4j |
||||
|
public class DebugController { |
||||
|
|
||||
|
@ApiOperation(value = "/测试",notes = "") |
||||
|
@ApiImplicitParams({ |
||||
|
}) |
||||
|
@RequestMapping(value="",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"}) |
||||
|
public JsonResponse debug(HttpServletRequest request) throws Exception { |
||||
|
|
||||
|
return JsonResponse.newInstance().ok("测试"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,71 @@ |
|||||
|
package com.ccsens.scheduler.api; |
||||
|
|
||||
|
import com.ccsens.scheduler.bean.dto.QuartzJobModule; |
||||
|
import com.ccsens.scheduler.util.JsonResponse; |
||||
|
import com.ccsens.scheduler.util.QuartzJobUtil; |
||||
|
import io.swagger.annotations.ApiImplicitParams; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import io.swagger.annotations.ApiParam; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: whj |
||||
|
* @time: 2021/6/30 16:26 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@RestController |
||||
|
@RequestMapping("/job") |
||||
|
public class JobController { |
||||
|
|
||||
|
@Resource |
||||
|
private QuartzJobUtil quartzJobUtil; |
||||
|
|
||||
|
@ApiOperation(value = "/添加任务",notes = "") |
||||
|
@ApiImplicitParams({ |
||||
|
}) |
||||
|
@RequestMapping(value="add",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"}) |
||||
|
public JsonResponse add(@ApiParam @Validated @RequestBody QuartzJobModule job) { |
||||
|
quartzJobUtil.addJob(job); |
||||
|
return JsonResponse.newInstance().ok(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@ApiOperation(value = "/修改任务时间",notes = "") |
||||
|
@ApiImplicitParams({ |
||||
|
}) |
||||
|
@RequestMapping(value="modifyTime",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"}) |
||||
|
public JsonResponse modifyTime(@ApiParam @Validated @RequestBody QuartzJobModule job) { |
||||
|
quartzJobUtil.modifyJobTime(job.getJobName(), job.getJobGroupName(), job.getCron()); |
||||
|
return JsonResponse.newInstance().ok(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@RequestMapping(value="pause",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"}) |
||||
|
public JsonResponse pause(String jobName) { |
||||
|
log.info("停止任务"); |
||||
|
quartzJobUtil.pauseJob(jobName); |
||||
|
return JsonResponse.newInstance().ok(); |
||||
|
} |
||||
|
|
||||
|
@PostMapping("/resume") |
||||
|
@ResponseBody |
||||
|
public JsonResponse resume(String jobName) { |
||||
|
log.info("恢复任务"); |
||||
|
quartzJobUtil.removeJob(jobName); |
||||
|
return JsonResponse.newInstance().ok(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@PostMapping("/remove") |
||||
|
@ResponseBody |
||||
|
public JsonResponse remove(String jobName) { |
||||
|
log.info("移除任务"); |
||||
|
quartzJobUtil.removeJob(jobName); |
||||
|
return JsonResponse.newInstance().ok(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,73 @@ |
|||||
|
package com.ccsens.scheduler.bean.dto; |
||||
|
|
||||
|
import cn.hutool.core.util.StrUtil; |
||||
|
import com.ccsens.scheduler.util.Constant; |
||||
|
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
|
import lombok.Data; |
||||
|
import org.quartz.Job; |
||||
|
import org.quartz.JobDataMap; |
||||
|
import org.quartz.JobExecutionContext; |
||||
|
import org.springframework.util.ClassUtils; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: whj |
||||
|
* @time: 2021/6/30 16:00 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class QuartzJobModule { |
||||
|
/** |
||||
|
* 触发器开始时间 |
||||
|
*/ |
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
private Date startTime; |
||||
|
/** |
||||
|
* 触发器结束时间 |
||||
|
*/ |
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
private Date endTime; |
||||
|
/** |
||||
|
* job名称 |
||||
|
*/ |
||||
|
private String jobName; |
||||
|
/** |
||||
|
* job组名 |
||||
|
*/ |
||||
|
private String jobGroupName = Constant.Quartz.QZ_JOB_GROUP_NAME; |
||||
|
/** |
||||
|
* 定时器名称 |
||||
|
*/ |
||||
|
private String triggerName; |
||||
|
/** |
||||
|
* 定时器组名 |
||||
|
*/ |
||||
|
private String triggerGroupName = Constant.Quartz.QZ_TRIGGER_GROUP_NAME; |
||||
|
/** |
||||
|
* 执行定时任务的具体操作 |
||||
|
*/ |
||||
|
private String jobClass; |
||||
|
/** |
||||
|
* cron表达式 |
||||
|
*/ |
||||
|
private String cron; |
||||
|
/** |
||||
|
* job的附加信息 |
||||
|
*/ |
||||
|
private JobDataMap jobDataMap = new JobDataMap(); |
||||
|
/** |
||||
|
* 校验 |
||||
|
* @return |
||||
|
*/ |
||||
|
public boolean verify(){ |
||||
|
return !(StrUtil.isEmpty(jobName) |
||||
|
|| StrUtil.isEmpty(jobGroupName) |
||||
|
|| StrUtil.isEmpty(triggerName) |
||||
|
|| StrUtil.isEmpty(triggerGroupName) |
||||
|
|| StrUtil.isEmpty(cron) |
||||
|
|| !ClassUtils.hasMethod(Job.class, "execute", JobExecutionContext.class) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
package com.ccsens.scheduler.config; |
||||
|
|
||||
|
import cn.hutool.core.io.resource.ClassPathResource; |
||||
|
import org.quartz.Scheduler; |
||||
|
import org.quartz.ee.servlet.QuartzInitializerListener; |
||||
|
import org.springframework.beans.factory.config.PropertiesFactoryBean; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.scheduling.quartz.SchedulerFactoryBean; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.util.Properties; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: whj |
||||
|
* @time: 2021/6/30 15:48 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class SchedulerConfig { |
||||
|
/** |
||||
|
* 读取quartz.properties 文件 |
||||
|
* 将值初始化 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Bean |
||||
|
public Properties quartzProperties() throws IOException { |
||||
|
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); |
||||
|
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); |
||||
|
propertiesFactoryBean.afterPropertiesSet(); |
||||
|
return propertiesFactoryBean.getObject(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将配置文件的数据加载到SchedulerFactoryBean中 |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
@Bean |
||||
|
public SchedulerFactoryBean schedulerFactoryBean() throws IOException { |
||||
|
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); |
||||
|
schedulerFactoryBean.setQuartzProperties(); |
||||
|
return schedulerFactoryBean; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 初始化监听器 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Bean |
||||
|
public QuartzInitializerListener executorListener(){ |
||||
|
return new QuartzInitializerListener(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获得Scheduler 对象 |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
@Bean |
||||
|
public Scheduler scheduler() throws IOException { |
||||
|
return schedulerFactoryBean().getScheduler(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
package com.ccsens.scheduler.service; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.quartz.DisallowConcurrentExecution; |
||||
|
import org.quartz.JobExecutionContext; |
||||
|
import org.quartz.JobExecutionException; |
||||
|
import org.springframework.scheduling.quartz.QuartzJobBean; |
||||
|
|
||||
|
/** |
||||
|
* @author: whj |
||||
|
* @time: 2021/6/30 16:22 |
||||
|
*/ |
||||
|
@DisallowConcurrentExecution// 禁止并发执行
|
||||
|
@Slf4j |
||||
|
public class TestJobDetail extends QuartzJobBean { |
||||
|
@Override |
||||
|
protected void executeInternal(JobExecutionContext context) throws JobExecutionException { |
||||
|
log.info("begin delwith batch task >>>>>>>>>>>>>>>>>>>>>>>"); |
||||
|
String batchId = context.getJobDetail().getKey().getName(); |
||||
|
log.info("执行的任务id为:[{}]", batchId); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,375 @@ |
|||||
|
package com.ccsens.scheduler.util; |
||||
|
|
||||
|
import com.ccsens.scheduler.bean.dto.QuartzJobModule; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.quartz.*; |
||||
|
import org.quartz.impl.triggers.CronTriggerImpl; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.text.ParseException; |
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: whj |
||||
|
* @time: 2021/6/30 16:03 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
public class QuartzJobUtil { |
||||
|
@Resource |
||||
|
private Scheduler scheduler; |
||||
|
|
||||
|
/** |
||||
|
* @Description: 添加一个定时任务 |
||||
|
* @param quartzModel |
||||
|
*/ |
||||
|
public void addJob(QuartzJobModule quartzModel) { |
||||
|
if (quartzModel.verify()) { |
||||
|
try { |
||||
|
JobDetail job = JobBuilder.newJob((Class<? extends Job>) Class.forName(quartzModel.getJobClass())) |
||||
|
.withIdentity(quartzModel.getJobName(), quartzModel.getJobGroupName()) |
||||
|
.setJobData(quartzModel.getJobDataMap()).build(); |
||||
|
// 表达式调度构建器
|
||||
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzModel.getCron()); |
||||
|
// 按新的cronExpression表达式构建一个新的trigger
|
||||
|
TriggerBuilder<Trigger> builder = TriggerBuilder.newTrigger() |
||||
|
.withIdentity(quartzModel.getTriggerName(), quartzModel.getTriggerGroupName()); |
||||
|
if (quartzModel.getStartTime() != null) { |
||||
|
builder.startAt(quartzModel.getStartTime()); |
||||
|
} |
||||
|
if (quartzModel.getEndTime() != null) { |
||||
|
builder.endAt(quartzModel.getEndTime()); |
||||
|
} |
||||
|
Trigger trigger = builder.withSchedule(scheduleBuilder).build(); |
||||
|
scheduler.scheduleJob(job, trigger); |
||||
|
// 启动
|
||||
|
if (!scheduler.isShutdown()) { |
||||
|
scheduler.start(); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
log.error("Add quartz job error, jobName = {}", quartzModel.getJobName()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
else { |
||||
|
log.error("QuartzModel is invalid!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) |
||||
|
* @param jobName |
||||
|
* @param cron |
||||
|
*/ |
||||
|
public void modifyJobTime(String jobName, String cron, Date startDate, Date endDate) { |
||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, Constant.Quartz.QZ_TRIGGER_GROUP_NAME); |
||||
|
|
||||
|
try { |
||||
|
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); |
||||
|
if (trigger == null) { |
||||
|
return; |
||||
|
} |
||||
|
String oldTime = trigger.getCronExpression(); |
||||
|
if (!oldTime.equalsIgnoreCase(cron)) { |
||||
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron); |
||||
|
// 按新的cronExpression表达式重新构建trigger
|
||||
|
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) |
||||
|
.startAt(startDate) |
||||
|
.endAt(endDate).withSchedule(scheduleBuilder).build(); |
||||
|
// 按新的trigger重新设置job执行
|
||||
|
scheduler.rescheduleJob(triggerKey, trigger); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description:修改任务,(可以修改任务名,任务类,触发时间) |
||||
|
* 原理:移除原来的任务,添加新的任务 |
||||
|
* @param oldJobName |
||||
|
* :原任务名 |
||||
|
* @param jobName |
||||
|
* @param jobclass |
||||
|
* @param cron |
||||
|
*/ |
||||
|
public void modifyJob(String oldJobName, String jobName, Class jobclass, String cron) { |
||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(oldJobName, Constant.Quartz.QZ_TRIGGER_GROUP_NAME); |
||||
|
JobKey jobKey = JobKey.jobKey(oldJobName, Constant.Quartz.QZ_JOB_GROUP_NAME); |
||||
|
try { |
||||
|
// 任务不存在
|
||||
|
if (removeJob(oldJobName, triggerKey, jobKey)) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
JobDetail job = JobBuilder.newJob(jobclass).withIdentity(jobName, |
||||
|
Constant.Quartz.QZ_JOB_GROUP_NAME) |
||||
|
.build(); |
||||
|
// 表达式调度构建器
|
||||
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron); |
||||
|
// 按新的cronExpression表达式构建一个新的trigger
|
||||
|
Trigger newTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, |
||||
|
Constant.Quartz.QZ_TRIGGER_GROUP_NAME) |
||||
|
.withSchedule(scheduleBuilder).build(); |
||||
|
|
||||
|
// 交给scheduler去调度
|
||||
|
scheduler.scheduleJob(job, newTrigger); |
||||
|
|
||||
|
// 启动
|
||||
|
if (!scheduler.isShutdown()) { |
||||
|
scheduler.start(); |
||||
|
System.err.println("添加新任务:" + jobName); |
||||
|
} |
||||
|
System.err.println("修改任务【" + oldJobName + "】为:" + jobName); |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
private boolean removeJob(String oldJobName, TriggerKey triggerKey, JobKey jobKey) throws SchedulerException { |
||||
|
Trigger trigger = scheduler.getTrigger(triggerKey); |
||||
|
if (trigger == null) { |
||||
|
return true; |
||||
|
} |
||||
|
// 停止触发器
|
||||
|
deleteJob(triggerKey, jobKey); |
||||
|
System.err.println("移除任务:" + oldJobName); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description: 修改一个任务的触发时间 |
||||
|
* @param triggerName |
||||
|
* @param triggerGroupName |
||||
|
* @param cron |
||||
|
*/ |
||||
|
public void modifyJobTime(String triggerName, String triggerGroupName, String cron) { |
||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); |
||||
|
try { |
||||
|
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); |
||||
|
if (trigger == null) { |
||||
|
return; |
||||
|
} |
||||
|
String oldTime = trigger.getCronExpression(); |
||||
|
if (!oldTime.equalsIgnoreCase(cron)) { |
||||
|
// trigger已存在,则更新相应的定时设置
|
||||
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron); |
||||
|
// 按新的cronExpression表达式重新构建trigger
|
||||
|
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); |
||||
|
// 按新的trigger重新设置job执行
|
||||
|
scheduler.resumeTrigger(triggerKey); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description 移除一个任务(使用默认的任务组名,触发器名,触发器组名) |
||||
|
* @param jobName |
||||
|
*/ |
||||
|
public void removeJob(String jobName) { |
||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, Constant.Quartz.QZ_TRIGGER_GROUP_NAME); |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, Constant.Quartz.QZ_JOB_GROUP_NAME); |
||||
|
try { |
||||
|
Trigger trigger = (Trigger) scheduler.getTrigger(triggerKey); |
||||
|
if (trigger == null) { |
||||
|
return; |
||||
|
} |
||||
|
deleteJob(triggerKey, jobKey); |
||||
|
System.err.println("移除任务:" + jobName); |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void deleteJob(TriggerKey triggerKey, JobKey jobKey) throws SchedulerException { |
||||
|
// 停止触发器
|
||||
|
scheduler.pauseTrigger(triggerKey); |
||||
|
// 移除触发器
|
||||
|
scheduler.unscheduleJob(triggerKey); |
||||
|
// 删除任务
|
||||
|
scheduler.deleteJob(jobKey); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description: 移除一个任务 |
||||
|
* @param jobName |
||||
|
* @param jobGroupName |
||||
|
* @param triggerName |
||||
|
* @param triggerGroupName |
||||
|
*/ |
||||
|
public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) { |
||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, triggerGroupName); |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); |
||||
|
try { |
||||
|
deleteJob(triggerKey, jobKey); |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description:暂停一个任务(使用默认组名) |
||||
|
* @param jobName |
||||
|
*/ |
||||
|
public void pauseJob(String jobName) { |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, Constant.Quartz.QZ_JOB_GROUP_NAME); |
||||
|
try { |
||||
|
scheduler.pauseJob(jobKey); |
||||
|
} |
||||
|
catch (SchedulerException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description:暂停一个任务 |
||||
|
* @param jobName |
||||
|
* @param jobGroupName |
||||
|
*/ |
||||
|
public void pauseJob(String jobName, String jobGroupName) { |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); |
||||
|
try { |
||||
|
scheduler.pauseJob(jobKey); |
||||
|
} |
||||
|
catch (SchedulerException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description:恢复一个任务(使用默认组名) |
||||
|
* @param jobName |
||||
|
*/ |
||||
|
public void resumeJob(String jobName) { |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, Constant.Quartz.QZ_JOB_GROUP_NAME); |
||||
|
try { |
||||
|
scheduler.resumeJob(jobKey); |
||||
|
} |
||||
|
catch (SchedulerException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description:恢复一个任务 |
||||
|
* @param jobName |
||||
|
* @param jobGroupName |
||||
|
*/ |
||||
|
public void resumeJob(String jobName, String jobGroupName) { |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); |
||||
|
try { |
||||
|
scheduler.resumeJob(jobKey); |
||||
|
} |
||||
|
catch (SchedulerException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description:启动所有定时任务 |
||||
|
*/ |
||||
|
public void startJobs() { |
||||
|
try { |
||||
|
scheduler.start(); |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description 关闭所有定时任务 |
||||
|
*/ |
||||
|
public void shutdownJobs() { |
||||
|
try { |
||||
|
if (!scheduler.isShutdown()) { |
||||
|
scheduler.shutdown(); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description: 立即运行任务,这里的立即运行,只会运行一次,方便测试时用。 |
||||
|
* @param jobName |
||||
|
*/ |
||||
|
public void triggerJob(String jobName) { |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, Constant.Quartz.QZ_JOB_GROUP_NAME); |
||||
|
try { |
||||
|
scheduler.triggerJob(jobKey); |
||||
|
} |
||||
|
catch (SchedulerException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description: 立即运行任务,这里的立即运行,只会运行一次,方便测试时用。 |
||||
|
* @param jobName |
||||
|
* @param jobGroupName |
||||
|
*/ |
||||
|
public void triggerJob(String jobName, String jobGroupName) { |
||||
|
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); |
||||
|
try { |
||||
|
scheduler.triggerJob(jobKey); |
||||
|
} |
||||
|
catch (SchedulerException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description: 获取任务状态 |
||||
|
* @param jobName |
||||
|
* 触发器名 |
||||
|
*/ |
||||
|
public String getTriggerState(String jobName) { |
||||
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, Constant.Quartz.QZ_TRIGGER_GROUP_NAME); |
||||
|
String name = null; |
||||
|
try { |
||||
|
Trigger.TriggerState triggerState = scheduler.getTriggerState(triggerKey); |
||||
|
name = triggerState.name(); |
||||
|
} |
||||
|
catch (SchedulerException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description:获取最近5次执行时间 |
||||
|
* @param cron |
||||
|
*/ |
||||
|
public List<String> getRecentTriggerTime(String cron) { |
||||
|
List<String> list = new ArrayList<String>(); |
||||
|
try { |
||||
|
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl(); |
||||
|
cronTriggerImpl.setCronExpression(cron); |
||||
|
List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, 5); |
||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
||||
|
for (Date date : dates) { |
||||
|
list.add(dateFormat.format(date)); |
||||
|
} |
||||
|
} |
||||
|
catch (ParseException e) { |
||||
|
log.error("GetRecentTriggerTime error, cron = {}", cron, e); |
||||
|
} |
||||
|
return list; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue