配置spring自定义定时任务
package com.paxsz.tms.crontab;
import java.io.Serializable;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.quartz.CronTriggerBean;
import com.paxsz.tms.admin.model.TmstCrontab;
import com.paxsz.tms.admin.service.CrontabService;
import com.paxsz.tms.crontab.dao.ScheduleInfoManager;
public class InitializingCronTrigger extends CronTriggerBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ScheduleInfoManager scheduleInfoManager;
@Autowired
@Qualifier("tmsCrontabService")
private CrontabService crontabService;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager) throws Exception {
this.scheduleInfoManager = scheduleInfoManager;
// 因为在getCronExpressionFromDB使用到了scheduleInfoManager,所以
// 必须上一行代码设置scheduleInfoManager后进行getCronExpressionFromDB
String cronExpression = getCronExpressionFromDB(); // ①
// 因为extends CronTriggerBean ,此处调用父类方法初始化cronExpression
setCronExpression(cronExpression); // ②
}
private String getCronExpressionFromDB() throws Exception {
String dbCronExpression = "0 * * * * ? 2099";
TmstCrontab tmstCrontab = crontabService.get(new Long(1));
if(tmstCrontab == null){
return dbCronExpression;
}
String minute = StringUtils.isEmpty(tmstCrontab.getCron_minute()) ? "*" : tmstCrontab.getCron_minute();
String hour = StringUtils.isEmpty(tmstCrontab.getCron_hour()) ? "*" : tmstCrontab.getCron_hour();
String day = StringUtils.isEmpty(tmstCrontab.getCron_day()) ? "?" : tmstCrontab.getCron_day();
String month = StringUtils.isEmpty(tmstCrontab.getCron_month()) ? "*" : tmstCrontab.getCron_month();
String week = StringUtils.equals(day, "?") ? (StringUtils.isEmpty(tmstCrontab.getCron_week()) ? "*" : tmstCrontab.getCron_week()) : "?";
String year = StringUtils.isEmpty(tmstCrontab.getCron_year()) ? "*" : tmstCrontab.getCron_year();
dbCronExpression ="0 " + minute + " " + hour + " " + day + " " + month + " " + week + " " + year;
return dbCronExpression;
}
public ScheduleInfoManager getScheduleInfoManager() {
return scheduleInfoManager;
}
}
package com.paxsz.tms.crontab;
import java.net.URL;
import org.apache.commons.lang.StringUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerBean;
import com.paxsz.tms.admin.model.TmstCrontab;
import com.paxsz.tms.admin.service.BackupInfoService;
import com.paxsz.tms.admin.service.CrontabService;
import com.paxsz.tms.admin.web.util.BackupRunnable;
public class ScheduleInfoAction {
public static final String BACKUP_SHELL_FILE = "backup.sh";
public static final String RESOURCES_FILE = "resources.properties";
public static String bakdirtemp = "/home/backup";
private Scheduler scheduler;
@Autowired
@Qualifier("tmsBackupInfoService")
private BackupInfoService backupInfoService;
@Autowired
@Qualifier("tmsCrontabService")
private CrontabService crontabService;
@SuppressWarnings("unused")
private void reScheduleJob() throws SchedulerException, ParseException, Exception {
// 运行时可通过动态注入的scheduler 得到trigger
CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("cronTrigger", Scheduler.DEFAULT_GROUP);
String dbCronExpression = getCronExpressionFromDB();
String originConExpression = trigger.getCronExpression();
// 判断从DB 中取得的任务时间(dbCronExpression) 和现在的quartz
// 线程中的任务时间(originConExpression) 是否相等
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
if (!originConExpression.equalsIgnoreCase(dbCronExpression)) {
trigger.setCronExpression(dbCronExpression);
scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
}
Long id = this.backupInfoService.initBackupInfo(bakdirtemp, "0");
// 下面是具体的job 内容,可自行设置
// executeJobDetail();
try{
URL url = this.crontabService.getResourcesFile(BACKUP_SHELL_FILE);
Runtime run = Runtime.getRuntime();
Process chmodProcess = run.exec("chmod +x " + url.getPath());
if(chmodProcess.waitFor() != 0){
this.backupInfoService.updateStatusById(id, "-1", "msg.backup.authright.failed");
}
Thread t = new Thread(new BackupRunnable(this.crontabService.getBackupProcessBuilder(id), this.backupInfoService, id));
t.start();
}catch(Exception e){
this.backupInfoService.updateStatusById(id, "-1", "msg.backup.authright.failed");
}
}
private String getCronExpressionFromDB() throws Exception {
String dbCronExpression = "0 * * * * ? 2099";
TmstCrontab tmstCrontab = crontabService.get(new Long(1));
if(tmstCrontab == null){
return dbCronExpression;
}
String minute = StringUtils.isEmpty(tmstCrontab.getCron_minute()) ? "*" : tmstCrontab.getCron_minute();
String hour = StringUtils.isEmpty(tmstCrontab.getCron_hour()) ? "*" : tmstCrontab.getCron_hour();
String day = StringUtils.isEmpty(tmstCrontab.getCron_day()) ? "?" : tmstCrontab.getCron_day();
String month = StringUtils.isEmpty(tmstCrontab.getCron_month()) ? "*" : tmstCrontab.getCron_month();
String week = StringUtils.equals(day, "?") ? (StringUtils.isEmpty(tmstCrontab.getCron_week()) ? "*" : tmstCrontab.getCron_week()) : "?";
String year = StringUtils.isEmpty(tmstCrontab.getCron_year()) ? "*" : tmstCrontab.getCron_year();
dbCronExpression ="0 " + minute + " " + hour + " " + day + " " + month + " " + week + " " + year;
bakdirtemp = StringUtils.isNotEmpty(tmstCrontab.getBakdir()) ? tmstCrontab.getBakdir() : "/home/backup";
return dbCronExpression;
}
// public Scheduler getScheduler() {
// return scheduler;
// }
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
}
动态修改定时任务
public ModelAndView scheduleBackup(HttpServletRequest request, Model model,
@ModelAttribute("command") BackupForm backupForm) throws Exception {
String minute = StringUtils.isEmpty(backupForm.getMinute()) ? "*"
: backupForm.getMinute();
String hour = StringUtils.isEmpty(backupForm.getHour()) ? "*"
: backupForm.getHour();
String day = StringUtils.isEmpty(backupForm.getDay()) ? "?"
: backupForm.getDay();
String month = StringUtils.isEmpty(backupForm.getMonth()) ? "*"
: backupForm.getMonth();
String week = StringUtils.equals(day, "?") ? (StringUtils
.isEmpty(backupForm.getDayofweek()) ? "*" : StringUtils.equals(backupForm.getDayofweek(),"?") ? "*" : backupForm.getDayofweek()) : "?";
String year = "*";
String dbCronExpression = "0 " + minute + " " + hour + " " + day + " " + month + " " + week + " " + year;
CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("cronTrigger", Scheduler.DEFAULT_GROUP);
trigger.setCronExpression(dbCronExpression);
trigger.setStartTime(new Date());
scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger);
backupForm.setYear("*");
this.crontabService.saveOrUpdate(backupForm);
return this.ajaxDoneSuccess(this.getMessage("msg.operation.success"));
}
如果不设置setStartTime,那么根据系统部署的时间不同,可能会在rescheduleJob的时候,立刻执行一次计划任务。