Loading...
Spring集成分布式Quartz框架之二:动态添加、修改、删除任务

Spring专题 2015/12/23 Spring Framework , Quartz

在上一篇关于《Spring集成配置分布式Quartz框架之一:常规整合》的基础之上实现动态添加、修改、删除任务


通常我们将Quartz整合到Spring是通过配置文件的方式配置任务Job和触发器Trigger,每次更改或者添加配置,都需要重新启动项目,为了解决该问题,在网络上翻阅了一些资料后,整理了动态添加、修改和删除任务的功能。

主要封装了三个类:QuartzJob(任务信息类)、QuartzJobFactory(任务执行类)、DynamicQuartzJobFactory(动态执行任务工厂类)。


废话少说,上菜~~


1、任务信息类:QuartzJob

package cn.imethan.admin.quartz.dynamic;

import java.io.Serializable;

/**

 * ScheduleJob.java

 * 

 * @author Ethan Wong

 * @since JDK 1.7

 * @datetime 2015年12月22日下午3:42:38

 */

public class QuartzJob implements Serializable {

    //需要实现序列化接口,否则没办法持久化到数据库

    private static final long serialVersionUID = -1800494926172862932L;

    private String jobId;//任务id

    private String jobName;//任务名称

    private String jobGroup;//任务分组

    private String jobStatus;//任务状态 0禁用 1启用 2删除

    private String cronExpression;//任务运行时间表达式

    private String description;//任务描述    

    

public String getJobId() {

return jobId;

}

public void setJobId(String jobId) {

this.jobId = jobId;

}

public String getJobName() {

return jobName;

}

public void setJobName(String jobName) {

this.jobName = jobName;

}

public String getJobGroup() {

return jobGroup;

}

public void setJobGroup(String jobGroup) {

this.jobGroup = jobGroup;

}

public String getJobStatus() {

return jobStatus;

}

public void setJobStatus(String jobStatus) {

this.jobStatus = jobStatus;

}

public String getCronExpression() {

return cronExpression;

}

public void setCronExpression(String cronExpression) {

this.cronExpression = cronExpression;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}


2、任务执行工厂类:QuartzJobFactory

package cn.imethan.admin.quartz.dynamic;

import java.io.Serializable;

import java.text.SimpleDateFormat;

import java.util.Date;


import org.quartz.DisallowConcurrentExecution;

import org.quartz.Job;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;


/**

 * QuartzJobFactory.java

 *

 * @author Ethan Wong

 * @since JDK 1.7

 * @datetime 2015年12月21日下午5:36:08

 */

@DisallowConcurrentExecution

public class QuartzJobFactory  implements Job,Serializable{


private static final long serialVersionUID = 3352853281096701664L;


@Override

public void execute(JobExecutionContext context) throws JobExecutionException {

// TODO Auto-generated method stub

System.out.println("★★★★★★★★★★★QuartzJob★★★★★★★★★★★:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));    

        System.out.println("QuartzJob Name = [" + context.getJobDetail().getKey() + "]");

        

        try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}


}


3、动态执行任务工厂类:DynamicQuartzJobFactory

package cn.imethan.admin.quartz.dynamic;

import java.util.ArrayList;

import java.util.List;

import java.util.Set;


import javax.transaction.Transactional;


import org.quartz.CronScheduleBuilder;

import org.quartz.CronTrigger;

import org.quartz.JobBuilder;

import org.quartz.JobDetail;

import org.quartz.JobExecutionContext;

import org.quartz.JobKey;

import org.quartz.Scheduler;

import org.quartz.SchedulerException;

import org.quartz.Trigger;

import org.quartz.TriggerBuilder;

import org.quartz.TriggerKey;

import org.quartz.impl.matchers.GroupMatcher;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import org.springframework.stereotype.Component;


/**

 * DynamicQuartzJobFactory.java

 *

 * @author Ethan Wong

 * @since JDK 1.7

 * @datetime 2015年12月21日下午5:32:50

 */

@Component

@Transactional//必须添加事务注释,否则任务没办法持久化到数据库

public class DynamicQuartzJobFactory {

@Autowired

private SchedulerFactoryBean quartzScheduler;//将配置文件的注册的bean注入

/**

* 获取任务调度

* @return

*

* @author Ethan Wong

* @datetime 2015年12月23日上午11:23:49

*/

private Scheduler getScheduler(){

return quartzScheduler.getScheduler();

}

/**

* 准备任务数据

* @return

*

* @author Ethan Wong

* @datetime 2015年12月23日上午11:23:41

*/

private List<QuartzJob> getAllJob(){

List<QuartzJob> list = new ArrayList<QuartzJob>();

for (int i = 0; i < 5; i++) {

QuartzJob job = new QuartzJob();

job.setJobId("10001" + i);

job.setJobName("TESTJOB" + i);

job.setJobGroup("DEFAULT");

job.setJobStatus("1");

job.setCronExpression("0/5 * * * * ?");

job.setDescription("测试任务");

list.add(job);

}

return list;

}

/**

* 测试入口

*

* @author Ethan Wong

* @datetime 2015年12月23日上午11:24:11

*/

public void testOne(){

try {

List<QuartzJob> jobList = getAllJob();//这里获取任务信息数据

for (QuartzJob job : jobList) {

this.addJob(job.getJobName(), job.getJobGroup(), job.getCronExpression());

// this.deleteJob(job.getJobName(), job.getJobGroup());

}

} catch (SchedulerException e) {

e.printStackTrace();

}

}

/**

* 获取计划中的任务列表

* @return

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:55:44

*/

public List<QuartzJob> getTriggersOfJob() throws SchedulerException{

Scheduler scheduler = this.getScheduler();

GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();

Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);

List<QuartzJob> jobList = new ArrayList<QuartzJob>();

for (JobKey jobKey : jobKeys) {

   List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);

   for (Trigger trigger : triggers) {

    QuartzJob job = new QuartzJob();

       job.setJobName(jobKey.getName());

       job.setJobGroup(jobKey.getGroup());

       job.setDescription("触发器:" + trigger.getKey());

       Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());

       job.setJobStatus(triggerState.name());

       if (trigger instanceof CronTrigger) {

           CronTrigger cronTrigger = (CronTrigger) trigger;

           String cronExpression = cronTrigger.getCronExpression();

           job.setCronExpression(cronExpression);

       }

       jobList.add(job);

   }

}

return jobList;

}


/**

* 获取正在执行的任务列表

* @return

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:56:02

*/

public List<QuartzJob> getCurrentlyExecutingJobs() throws SchedulerException{

Scheduler scheduler = this.getScheduler();

List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();

List<QuartzJob> jobList = new ArrayList<QuartzJob>(executingJobs.size());

for (JobExecutionContext executingJob : executingJobs) {

QuartzJob job = new QuartzJob();

   JobDetail jobDetail = executingJob.getJobDetail();

   JobKey jobKey = jobDetail.getKey();

   Trigger trigger = executingJob.getTrigger();

   job.setJobName(jobKey.getName());

   job.setJobGroup(jobKey.getGroup());

   job.setDescription("触发器:" + trigger.getKey());

   Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());

   job.setJobStatus(triggerState.name());

   if (trigger instanceof CronTrigger) {

       CronTrigger cronTrigger = (CronTrigger) trigger;

       String cronExpression = cronTrigger.getCronExpression();

       job.setCronExpression(cronExpression);

   }

   jobList.add(job);

}

return jobList;

}

/**

* 添加任务

* @param jobName

* @param jobGroup

* @param timeExpression

* @return

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:03:02

*/

public boolean addJob(String jobName,String jobGroup,String timeExpression) throws SchedulerException{

Scheduler scheduler = this.getScheduler();

//获取trigger,即在spring配置文件中定义的 bean id="myTrigger"

TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);

CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

if (trigger == null) {

//不存在,创建一个

JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class).withIdentity(jobName, jobGroup).storeDurably().build();

jobDetail.getJobDataMap().put("scheduleJob", "job");

scheduler.addJob(jobDetail, true);

//表达式调度构建器

CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(timeExpression);

//按新的cronExpression表达式构建一个新的trigger

trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup).forJob(jobDetail).withSchedule(scheduleBuilder).build();

boolean isExists = scheduler.checkExists(jobDetail.getKey());

if(!isExists){

scheduler.scheduleJob(jobDetail, trigger);

}else{

}

scheduler.scheduleJob(trigger);

} else {

// Trigger已存在,那么更新相应的定时设置

//表达式调度构建器

CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(timeExpression);

//按新的cronExpression表达式重新构建trigger

trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

//按新的trigger重新设置job执行

scheduler.rescheduleJob(triggerKey, trigger);

}

return false;

}

/**

* 暂停任务

* @param jobName

* @param jobGroup

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:17:00

*/

public void pauseJob(String jobName,String jobGroup) throws SchedulerException{

Scheduler scheduler = this.getScheduler();

JobKey jobKey = JobKey.jobKey(jobName, jobName);

scheduler.pauseJob(jobKey);

}

/**

* 恢复任务

* @param jobName

* @param jobGroup

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:17:41

*/

public void resumeJob(String jobName,String jobGroup) throws SchedulerException{

Scheduler scheduler = this.getScheduler();

JobKey jobKey = JobKey.jobKey(jobName, jobGroup);

scheduler.resumeJob(jobKey);

}

/**

* 删除任务

* @param jobName

* @param jobGroup

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:18:38

*/

public void  deleteJob(String jobName,String jobGroup) throws SchedulerException{

Scheduler scheduler = this.getScheduler();

JobKey jobKey = JobKey.jobKey(jobName, jobGroup);

scheduler.deleteJob(jobKey);

}

/**

* 触发任务

* @param jobName

* @param jobGroup

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:19:49

*/

public void  triggerJob(String jobName,String jobGroup) throws SchedulerException{

Scheduler scheduler = this.getScheduler();

JobKey jobKey = JobKey.jobKey(jobName, jobGroup);

scheduler.triggerJob(jobKey);

}

/**

* 更新任务触发器时间

* @param jobName

* @param jobGroup

* @param timeExpression

* @throws SchedulerException

*

* @author Ethan Wong

* @datetime 2015年12月23日上午10:21:55

*/

public void modifyTimeExpression(String jobName,String jobGroup,String timeExpression) throws SchedulerException{

Scheduler scheduler = this.getScheduler();

TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);

//获取trigger,即在spring配置文件中定义的 bean id="myTrigger"

CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

//表达式调度构建器

CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(timeExpression);

//按新的cronExpression表达式重新构建trigger

trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

//按新的trigger重新设置job执行

scheduler.rescheduleJob(triggerKey, trigger);

}


}



参考文档

http://www.quartz-scheduler.org/

http://www.dexcoder.com/selfly/article/308


Comments