Quartz,spring动态加载定时任务

package des.tiny.job.base;

import java.util.ArrayList;
import java.util.List;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import des.tiny.job.entity.ScheduleJob;
import des.tiny.job.factory.QuartzJobFactory;
import des.tiny.job.method.Clazz1;
import des.tiny.job.method.Clazz2;

public class LoadkettleRepositoryJob {

	@Autowired(required=false)
	private SchedulerFactoryBean schedulerFactoryBean;

	public void execute() throws Exception{
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		 
		List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(){
			private static final long serialVersionUID = 2596601829579886713L;
			{
					ScheduleJob job = new ScheduleJob();
					job.setJobId("10001");
					job.setJobStatus("1");
					job.setCronExpression("0/5 * * * * ?");
					job.setDesc("数据导入任务");
					job.setClazz(Clazz1.class);
					job.setMethod("method1");
					this.add(job);
					
					ScheduleJob job1 = new ScheduleJob();
					job1.setJobId("10002");
					job1.setJobStatus("1");
					job1.setCronExpression("0/8 * * * * ?");
					job1.setDesc("数据导入任务");
					job1.setClazz(Clazz2.class);
					job1.setMethod("method2");
					this.add(job1);
			}
		};		 
		for (ScheduleJob job : jobList) {
		 
			TriggerKey triggerKey = TriggerKey.triggerKey(job.getClazz().getSimpleName(), job.getClazz().getSimpleName());
		 
			//获取trigger,即在spring配置文件中定义的 bean id="myTrigger"
			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
		 
			//不存在,创建一个
			if (null == trigger) {
				JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class)
					.withIdentity(job.getClazz().getSimpleName(), job.getClazz().getSimpleName()).build();
				jobDetail.getJobDataMap().put("scheduleJob", job);
		 
				//表达式调度构建器
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
					.getCronExpression());
		 
				//按新的cronExpression表达式构建一个新的trigger
				trigger = TriggerBuilder.newTrigger().withIdentity(job.getClazz().getSimpleName(), job.getClazz().getSimpleName()).withSchedule(scheduleBuilder).build();
		 
				scheduler.scheduleJob(jobDetail, trigger);
			} else {
				// Trigger已存在,那么更新相应的定时设置
				//表达式调度构建器
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
					.getCronExpression());
		 
				//按新的cronExpression表达式重新构建trigger
				trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
					.withSchedule(scheduleBuilder).build();
		 
				//按新的trigger重新设置job执行
				scheduler.rescheduleJob(triggerKey, trigger);
			}
		}
	}

}

package des.tiny.job.entity;

public class ScheduleJob {
	/** 任务id */
    private String jobId;
 
   private Class<?> clazz;
    
    private String method;
 
    /** 任务状态 0禁用 1启用 2删除*/
    private String jobStatus;
 
    /** 任务运行时间表达式 */
    private String cronExpression;
 
    /** 任务描述 */
    private String desc;

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	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 getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
}



package des.tiny.job.factory;

import java.lang.reflect.Method;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import des.tiny.job.entity.ScheduleJob;
@DisallowConcurrentExecution
public class QuartzJobFactory implements Job {
	
	

	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("任务成功运行");
		ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap()
				.get("scheduleJob");
		Class<?> clazz = scheduleJob.getClazz();
		String method = scheduleJob.getMethod();
		try {
			Object obj = clazz.newInstance();
			Method m = clazz.getMethod(method, null);
			m.invoke(obj, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}



package des.tiny.job.method;

public class Clazz1 {

	public void method1(){
		System.out.println("1----method---1");
	}
	
	public void method2(){
		System.out.println("1----method----2");
	}
}



package des.tiny.job.method;

public class Clazz2 {

	public void method1(){
		System.out.println("2----method---1");
	}
	
	public void method2(){
		System.out.println("2----method----2");
	}
}



<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"/>
	
	<bean id="loadkettleRepositoryJob" class="des.tiny.job.base.LoadkettleRepositoryJob" init-method="execute"/>




你可能感兴趣的:(spring,quartz)