1. 程式人生 > >在spring中實現quartz2.2.1的動態排程(開始、暫停、停止等)

在spring中實現quartz2.2.1的動態排程(開始、暫停、停止等)

需求: 需要在頁面設定某個時間,然後點選按鈕後,執行某個排程,並且可以在頁面刪除某個排程

1、導包

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>org.quartz-scheduler</groupId
>
<artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>

2、建立QuartzManager類,管理job

import org.apache.commons.lang.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import
org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; public class QuartzManager { private Scheduler scheduler; /** * @Description: 新增一個定時任務 * * @param jobName * 任務名 * @param
jobGroupName * 任務組名 * @param triggerName * 觸發器名 * @param triggerGroupName * 觸發器組名 * @param jobClass * 任務 * @param cron * 時間設定,參考quartz說明文件 */
@SuppressWarnings({ "unchecked", "rawtypes" }) public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron) { try { // 任務名,任務組,任務執行類 JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build(); // 觸發器 TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); // 觸發器名,觸發器組 triggerBuilder.withIdentity(triggerName, triggerGroupName); triggerBuilder.startNow(); // 觸發器時間設定 triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron)); // 建立Trigger物件 CronTrigger trigger = (CronTrigger) triggerBuilder.build(); // 排程容器設定JobDetail和Trigger scheduler.scheduleJob(jobDetail, trigger); // 啟動 if (!scheduler.isShutdown()) { scheduler.start(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 修改一個任務的觸發時間 * * @param jobName * @param jobGroupName * @param triggerName * 觸發器名 * @param triggerGroupName * 觸發器組名 * @param cron * 時間設定,參考quartz說明文件 */ public void modifyJobTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron) { try { TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(cron)) { /** 方式一 :呼叫 rescheduleJob 開始 */ // 觸發器 TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); // 觸發器名,觸發器組 triggerBuilder.withIdentity(triggerName, triggerGroupName); triggerBuilder.startNow(); // 觸發器時間設定 triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron)); // 建立Trigger物件 trigger = (CronTrigger) triggerBuilder.build(); // 方式一 :修改一個任務的觸發時間 scheduler.rescheduleJob(triggerKey, trigger); /** 方式一 :呼叫 rescheduleJob 結束 */ /** 方式二:先刪除,然後在建立一個新的Job */ // JobDetail jobDetail = // scheduler.getJobDetail(JobKey.jobKey(jobName, jobGroupName)); // Class<? extends Job> jobClass = jobDetail.getJobClass(); // removeJob(jobName, jobGroupName, triggerName, // triggerGroupName); // addJob(jobName, jobGroupName, triggerName, triggerGroupName, // jobClass, cron); /** 方式二 :先刪除,然後在建立一個新的Job */ } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 移除一個任務 * * @param jobName * @param jobGroupName * @param triggerName * @param triggerGroupName */ public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) { try { TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); scheduler.pauseTrigger(triggerKey);// 停止觸發器 scheduler.unscheduleJob(triggerKey);// 移除觸發器 scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 刪除任務 } catch (Exception e) { throw new RuntimeException(e); } } /** * @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); } } public Scheduler getScheduler() { return scheduler; } public void setScheduler(Scheduler scheduler) { this.scheduler = scheduler; } public String transCron(String time){ String seconds = StringUtils.substringAfterLast(time, ":"); String minute = StringUtils.substringAfter(time, ":").substring(0, 2); String hour = StringUtils.substringAfter(time, " ").substring(0, 2); String day = StringUtils.substringAfterLast(time, "-").substring(0, 2); String month = StringUtils.substringAfter(time, "-").substring(0, 2); return seconds + " " + minute + " " + hour + " " + day + " " + month + " ?"; } }

3、MyJob類,實現自己的排程業務,該類必須實現Job介面

package cn.tisson.qoe.quartz;

import java.util.Map;

import javax.annotation.Resource;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.tisson.qoe.utils.ApacheFtpUtil;
import cn.tisson.qoe.utils.CallShellUtil;

public class MyJob implements Job{

    private Logger logger = LoggerFactory.getLogger(MyJob.class);

    //直接注入  
    @Resource
    private QuartzManager quartzManager;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            test();
            downloadYZfile(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void test() throws Exception{
        logger.info("【test】動態定時排程測試");
    }

    /**
     * 同步光寬轉化率月清單
     * @throws Exception
     */
    public void downloadYZfile() throws Exception{
        //自己的業務邏輯在這裡實現
    }
}

4、在springmvc的配置檔案,新增配置

<bean id="startQuartz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
     <property name="triggers">  
         <list>  
             <!-- <ref bean="cronTrigger" /> -->
         </list>  
     </property>  
 </bean>  

<bean id="quartzManager" class="cn.tisson.qoe.quartz.QuartzManager" lazy-init="false" init-method="startJobs" >  
     <!--這個物件一定要注入,這樣類才能進行管理,還有在型別要用get set方法,不然會報錯。-->  
    <property name="scheduler" ref="startQuartz" />  
</bean>   

5、在Controller中

    //這個bean在spring中配置,可以直接注入
    @Resource
    private QuartzManager quartzManager;

    @RequestMapping(value = "startQuartz", method = {RequestMethod.GET})
    @ResponseBody
    public String testQuartz(String quartzTime) throws Exception{
        logger.info("===========開始執行排程=========時間為 " + quartzTime);
        String cronStr = quartzManager.transCron(quartzTime);
        logger.info("======cron表示式========" + cronStr);
        quartzManager.addJob("test", "test", "test", "test", MyJob.class, cronStr);    
        //quartzManager.modifyJobTime("test", "test", "test", "test", "0 50 10 * * ?");    
        //quartzManager.removeJob("test", "test", "test", "test");    
        //quartzManager.shutdownJobs();
        return "success";
    }

    @RequestMapping(value = "stopQuartz", method = {RequestMethod.GET})
    @ResponseBody
    public String shutDownQuartz() throws Exception{
        logger.info("===========關閉排程test==================");
        quartzManager.removeJob("test", "test", "test", "test");  
        return "關閉成功";
    }

因為需求是要配置時間執行排程,所以不能控制執行次數,如果需要控制執行次數,可以使用
triggerBuilder.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).withRepeatCount(1));//執行間隔和重複執行的次數
該方法參考連結: http://www.2cto.com/kf/201608/533723.html