1.创建Job
@DisallowConcurrentExecution
@Component
public class SuspendCycleEndJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Trigger trigger = jobExecutionContext.getTrigger();
JobDataMap jobDataMap = trigger.getJobDataMap();
String id = jobDataMap.getString("id");
SiteStop siteStop = siteStopMapper.get(id);
if (siteStop!=null){
if(!StringUtils.isEmpty(id)){
siteStopMapper.updateState(id,"2");
}
}
}
}
通用JobService
@Service
public class CommonJobService {
@Autowired
private Scheduler scheduler;
public void pauseJob(String jName, String jGroup) {
try {
scheduler.pauseJob(JobKey.jobKey(jName, jGroup));
} catch (Exception e) {
e.printStackTrace();
}
}
public void pauseTrigger(String tName, String tGroup) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(tName, tGroup);
scheduler.pauseTrigger(triggerKey);
} catch (Exception e) {
e.printStackTrace();
}
}
public void resumeJob(String jName, String jGroup) {
try {
scheduler.resumeJob(JobKey.jobKey(jName, jGroup));
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public void resumeTrigger(String tName, String tGroup) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(tName, tGroup);
scheduler.resumeTrigger(triggerKey);
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteJob(String jName, String jGroup) {
try {
scheduler.deleteJob(JobKey.jobKey(jName, jGroup));
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public void deleteTrigger(String tName, String tGroup) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(tName, tGroup);
scheduler.pauseTrigger(triggerKey);
scheduler.unscheduleJob(triggerKey);
} catch (Exception e) {
e.printStackTrace();
}
}
}
创建触发器
@Service
public class SuspendCycleEndService {
@Autowired
private Scheduler scheduler;
public void addTrigger(String jName, String jGroup, String tName, String tGroup, String cron,String id) {
try {
JobKey jobKey = new JobKey(jName, jGroup);
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if(jobDetail == null){
jobDetail = JobBuilder.newJob(要实现的job.class)
.withIdentity(jName, jGroup)
.storeDurably()
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(tName, tGroup)
.usingJobData("id",id)
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule(cron))
.build();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
}else{
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(tName, tGroup)
.usingJobData("id",id)
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule(cron))
.forJob(jName,jGroup)
.build();
scheduler.scheduleJob(trigger);
scheduler.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用定时任务参考
public void save(SiteStop siteStop) {
Date now = new Date();
String state = "";
if(now.getTime() <= siteStop.getStartTime().getTime()){
state = "3";
}else if(now.getTime() >= siteStop.getEndTime().getTime()){
state = "2";
}else if(now.getTime() >= siteStop.getStartTime().getTime() && now.getTime() <= siteStop.getEndTime().getTime()){
state = "1";
}
siteStop.setState(state);
super.save(siteStop);
if(now.getTime() < siteStop.getStartTime().getTime()) {
Calendar startcal = Calendar.getInstance();
startcal.setTime(siteStop.getStartTime());
int year = startcal.get(Calendar.YEAR);
int month = startcal.get(Calendar.MONTH) + 1;
int day = startcal.get(Calendar.DATE);
String cron = "";
cron = "0"+" "+"0"+" "+"0"+" "+day+" "+month+" ? "+year;
suspendCycleStartService.addTrigger("暂停周期任务","2",siteStop.getId(),"启动暂停周期任务",cron,siteStop.getId());
}
if(now.getTime() < siteStop.getEndTime().getTime()) {
Calendar endcal = Calendar.getInstance();
endcal.setTime(siteStop.getEndTime());
int year = endcal.get(Calendar.YEAR);
int month = endcal.get(Calendar.MONTH) + 1;
int day = endcal.get(Calendar.DATE);
String cron = "";
cron = "0"+" "+"0"+" "+"0"+" "+day+" "+month+" ? "+year;
suspendCycleEndService.addTrigger("暂停周期任务","2",siteStop.getId(),"结束暂停周期任务",cron,siteStop.getId());
}
}
corn表达式封装
public class CronUtil {
public static String getCron(Date time){
Calendar startcal = Calendar.getInstance();
startcal.setTime(time);
int year = startcal.get(Calendar.YEAR);
int month = startcal.get(Calendar.MONTH) + 1;
int day = startcal.get(Calendar.DATE);
String cron = "";
cron = "0"+" "+"0"+" "+"0"+" "+day+" "+month+" ? "+year;
return cron;
}
}