Spring4+Springmvc+quartz實現多執行緒動態定時排程
阿新 • • 發佈:2019-08-05
scheduler定時排程系統是大多行業專案都需要的,傳統的spring-job模式,個人感覺已經out了,因為存在很多的問題,特別是定時排程的追加、修改、刪除等,需要修改xml,xml的配置生效無非是熱部署灰度釋出方案或者直接停止、重啟伺服器,完全不能做到自動啟動、修復方式。
提醒:可以對應用進行叢集部署,在對定時排程配置時可以使用叢集方式或者單邊配置應用方式,今天講解的是使用spring4+scheduler實現定時排程,閒話少說,直接把步驟記錄下來:
- 在專案的pom.xml檔案中引入quartz的jar包,如下:
<!-- quartz定時排程 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.5</version> <dependency>
- 定義quartz的配置檔案spring-context-quartz.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-lazy-init="false"> <!-- 排程器 --> <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 通過applicationContextSchedulerContextKey屬性配置spring上下文 --> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> </bean> <!--載入資料庫任務--> <bean id="jobService" class="top.alterem.job.service.JobService" init-method="loadJob" /> </beans> xml 3. 在專案的web.xml檔案中引入spring-context-quartz.xml配置檔案 ```xml classpath*:spring-context-quartz.xml
- 定義job實體物件
public class Job{ private static final long serialVersionUID = 1L; /** * 任務執行週期cron表示式 */ public static int EXECYCLE_CRON = 2; /** * 任務執行週期自定義 */ public static int EXECYCLE_DEFINE = 1; /** * 執行週期-分鐘 */ public static int EXECYCLE_MINUTE = 1; /** * 執行週期-小時 */ public static int EXECYCLE_HOUR = 2; /** * 執行週期-日 */ public static int EXECYCLE_DAY = 3; /** * 執行週期-月 */ public static int EXECYCLE_WEEK = 4; /** * 執行週期-月 */ public static int EXECYCLE_MONTH = 5; private String jobType; // 任務型別(1首頁靜態化、2欄目頁靜態化、3內容頁靜態化、4採集、5分發) private String jobName; // 任務名稱 private String jobClass; // 任務類 private String execycle; // 執行週期分類(1非表示式 2 cron表示式) private String dayOfMonth; // 每月的哪天 private String dayOfWeek; // 周幾 private String hour; // 小時 private String minute; // 分鐘 private String intervalHour; // 間隔小時 private String intervalMinute; // 間隔分鐘 private String jobIntervalUnit; // 1分鐘、2小時、3日、4周、5月 private String cronExpression; // 規則表示式 private String isEnable; // 是否啟用 public Job() { super(); } public Job(String id){ super(id); } @Length(min=1, max=1, message="任務型別(1首頁靜態化、2欄目頁靜態化、3內容頁靜態化、4採集、5分發)長度必須介於 1 和 1 之間") public String getJobType() { return jobType; } public void setJobType(String jobType) { this.jobType = jobType; } @Length(min=1, max=255, message="任務名稱長度必須介於 1 和 255 之間") public String getJobName() { return jobName; } public void setJobName(String jobName) { this.jobName = jobName; } @Length(min=1, max=255, message="任務類長度必須介於 1 和 255 之間") public String getJobClass() { return jobClass; } public void setJobClass(String jobClass) { this.jobClass = jobClass; } @Length(min=1, max=1, message="執行週期分類(1非表示式 2 cron表示式)長度必須介於 1 和 1 之間") public String getExecycle() { return execycle; } public void setExecycle(String execycle) { this.execycle = execycle; } @Length(min=0, max=11, message="每月的哪天長度必須介於 0 和 11 之間") public String getDayOfMonth() { return dayOfMonth; } public void setDayOfMonth(String dayOfMonth) { this.dayOfMonth = dayOfMonth; } @Length(min=0, max=1, message="周幾長度必須介於 0 和 1 之間") public String getDayOfWeek() { return dayOfWeek; } public void setDayOfWeek(String dayOfWeek) { this.dayOfWeek = dayOfWeek; } @Length(min=0, max=11, message="小時長度必須介於 0 和 11 之間") public String getHour() { return hour; } public void setHour(String hour) { this.hour = hour; } @Length(min=0, max=11, message="分鐘長度必須介於 0 和 11 之間") public String getMinute() { return minute; } public void setMinute(String minute) { this.minute = minute; } @Length(min=0, max=11, message="間隔小時長度必須介於 0 和 11 之間") public String getIntervalHour() { return intervalHour; } public void setIntervalHour(String intervalHour) { this.intervalHour = intervalHour; } @Length(min=0, max=11, message="間隔分鐘長度必須介於 0 和 11 之間") public String getIntervalMinute() { return intervalMinute; } public void setIntervalMinute(String intervalMinute) { this.intervalMinute = intervalMinute; } @Length(min=0, max=1, message="1分鐘、2小時、3日、4周、5月長度必須介於 0 和 1 之間") public String getJobIntervalUnit() { return jobIntervalUnit; } public void setJobIntervalUnit(String jobIntervalUnit) { this.jobIntervalUnit = jobIntervalUnit; } @Length(min=0, max=255, message="規則表示式長度必須介於 0 和 255 之間") public String getCronExpression() { return cronExpression; } public void setCronExpression(String cronExpression) { this.cronExpression = cronExpression; } @Length(min=1, max=1, message="是否啟用長度必須介於 1 和 1 之間") public String getIsEnable() { return isEnable; } public void setIsEnable(String isEnable) { this.isEnable = isEnable; } }
- 編寫quartz的jobServvice類:
package top.alterem.job.service;
import java.text.ParseException;
import java.util.List;
import java.util.UUID;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.alterem.StringUtils;
import top.alterem.common.persistence.Page;
import top.alterem.common.service.CrudService;
import top.alterem.job.dao.JobDao;
import top.alterem.job.entity.Job;
/**
* 定時排程任務Service
*
* @author honghu
*/
@Service
@Transactional(readOnly = true)
public class JobService extends CrudService<JobDao, Job> {
@Autowired
private JobDao jobDao;
private Logger logger = LoggerFactory.getLogger(getClass());
public Job get(String id) {
return super.get(id);
}
public List<Job> findList(Job job) {
return super.findList(job);
}
public Page<Job> findPage(Page<Job> page, Job job) {
return super.findPage(page, job);
}
@Transactional(readOnly = false)
public void save(Job job) {
super.save(job);
// 啟用則啟動任務
if (StringUtils.equals("1", job.getIsEnable())) {
startTask(job, job.getId());
}
}
@Transactional(readOnly = false)
public void update(Job job) {
//結束定時排程
endTask(job.getId());
job.preUpdate();
jobDao.update(job);
// 啟用則啟動任務
if (StringUtils.equals("1", job.getIsEnable())) {
startTask(job, job.getId());
}
}
@Transactional(readOnly = false)
public void delete(Job job) {
//結束任務
endTask(job.getId());
super.delete(job);
}
/**
* 系統初始載入任務
*/
public void loadJob() throws Exception {
List<Job> jobList = this.findList(new Job());
if ( != jobList && jobList.size() > 0) {
for (int i = 0; i < jobList.size(); i++) {
Job job = jobList.get(i);
// 任務開啟狀態 執行任務排程
if (StringUtils.equals("1", job.getIsEnable())) {
try {
JobDetail jobDetail = new JobDetail();
// 設定任務名稱
if (StringUtils.isNotBlank(job.getId())) {
jobDetail.setName(job.getId());
} else {
UUID uuid = UUID.randomUUID();
jobDetail.setName(uuid.toString());
job.setId(uuid.toString());
}
jobDetail.setGroup(Scheduler.DEFAULT_GROUP);
// 設定任務執行類
jobDetail.setJobClass(getClassByTask(job.getJobClass()));
// 新增任務引數
CronTrigger cronTrigger = new CronTrigger("cron_" + i, Scheduler.DEFAULT_GROUP,
jobDetail.getName(), Scheduler.DEFAULT_GROUP);
cronTrigger.setCronExpression(getCronExpressionFromDB(job.getId()));
// 排程任務
scheduler.scheduleJob(jobDetail, cronTrigger);
} catch (SchedulerException e) {
logger.error("JobService SchedulerException", e);
} catch (ClassNotFoundException e) {
logger.error("JobService ClassNotFoundException", e);
} catch (Exception e) {
logger.error("JobService Exception", e);
}
}
}
}
}
/**
*
* @param taskClassName
* 任務執行類名
* @return
* @throws ClassNotFoundException
*/
@SuppressWarnings("rawtypes")
private Class getClassByTask(String taskClassName) throws ClassNotFoundException {
return Class.forName(taskClassName);
}
public String getCronExpressionFromDB(String id) throws Exception {
// 設定任務規則
Job job = this.get(id);
if ( != job) {
if (Job.EXECYCLE_CRON == Integer.parseInt(job.getExecycle())) {
return job.getCronExpression();
} else {
Integer execycle = Integer.parseInt(job.getJobIntervalUnit());
String excep = "";
if (execycle.equals(Job.EXECYCLE_MONTH)) {
excep = "0 " + job.getMinute() + " " + job.getHour() + " " + job.getDayOfMonth() + " * ?";
} else if (execycle.equals(Job.EXECYCLE_WEEK)) {
excep = "0 " + job.getMinute() + " " + job.getHour() + " " + " ? " + " * " + job.getDayOfWeek();
} else if (execycle.equals(Job.EXECYCLE_DAY)) {
excep = "0 " + job.getMinute() + " " + job.getHour() + " " + " * * ?";
} else if (execycle.equals(Job.EXECYCLE_HOUR)) {
excep = "0 0 */" + job.getIntervalHour() + " * * ?";
} else if (execycle.equals(Job.EXECYCLE_MINUTE)) {
excep = "0 */" + job.getIntervalMinute() + " * * * ?";
}
return excep;
}
}
return "";
}
private void startTask(Job job, String id) {
try {
String cronExpress = getCronExpressionFromDB(id);
if (StringUtils.isNotEmpty(cronExpress) && cronExpress.indexOf("null") == -1) {
JobDetail jobDetail = new JobDetail();
jobDetail.setName(id);
jobDetail.setGroup(Scheduler.DEFAULT_GROUP);
jobDetail.setJobClass(getClassByTask(job.getJobClass()));
CronTrigger cronTrigger = new CronTrigger("cron_" + id, Scheduler.DEFAULT_GROUP, jobDetail.getName(),
Scheduler.DEFAULT_GROUP);
cronTrigger.setCronExpression(cronExpress);
scheduler.scheduleJob(jobDetail, cronTrigger);
}
} catch (ParseException e) {
logger.error("JobService ParseException", e);
} catch (Exception e) {
logger.error("JobService Exception", e);
}
}
private void endTask(String id) {
try {
scheduler.deleteJob(id, Scheduler.DEFAULT_GROUP);
} catch (SchedulerException e) {
logger.error("JobService endTask", e);
}
}
@Autowired
private Scheduler scheduler;
}
編寫相關job的Controller、dao、dao.xml我這邊就不寫了,其實就是對資料的增刪改查操作
啟動專案驗證quartz是否成功:
專案啟動個控制檯:
任務列表:
任務新增和修改介面:
到此完畢