Spring4+Springmvc+quartz實現多線程動態定時調度

scheduler定時調度系統是大多行業項目都須要的,傳統的spring-job模式,我的感受已經out了,由於存在不少的問題,特別是定時調度的追加、修改、刪除等,須要修改xml,xml的配置生效無非是熱部署灰度發佈方案或者直接中止、重啓服務器,徹底不能作到自動啓動、修復方式。java

提醒:能夠對應用進行集羣部署,在對定時調度配置時能夠使用集羣方式或者單邊配置應用方式,今天講解的是使用spring4+scheduler實現定時調度,閒話少說,直接把步驟記錄下來:web

  1. 在項目的pom.xml文件中引入quartz的jar包,以下:
<!-- quartz定時調度 -->        
<dependency>        
  <groupId>org.quartz-scheduler</groupId>           
  <artifactId>quartz</artifactId>           
  <version>1.8.5</version>          
<dependency>
  1. 定義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
  1. 定義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;  
    }  
      
}
  1. 編寫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;  
  
}
  1. 編寫相關job的Controller、dao、dao.xml我這邊就不寫了,其實就是對數據的增刪改查操做spring

  2. 啓動項目驗證quartz是否成功:數據庫

項目啓動個控制檯:服務器

點擊查看原始大小圖片
任務列表:
點擊查看原始大小圖片
任務添加和修改界面:
點擊查看原始大小圖片
到此完畢!app

相關文章
相關標籤/搜索