zbb20170228_spring_quartz2动态添加任务

1、包

spring包

quartz2.*包

2、class文件

 

JobBean.java

package com.zbb.quartz2;

public class JobBean {
    private String jobId;
    private String jobGroupName;
    private String JobCronExpression;
    private String JobDesc;
    private Class<?> JobClass;
    private String JobMethod;
    private String JobMethodSwhere1;
    private String JobMethodSwhere2;
    private String JobStatus;
    public String getJobId() {
        return jobId;
    }
    public void setJobId(String jobId) {
        this.jobId = jobId;
    }
    public String getJobGroupName() {
        return jobGroupName;
    }
    public void setJobGroupName(String jobGroupName) {
        this.jobGroupName = jobGroupName;
    }
    public String getJobCronExpression() {
        return JobCronExpression;
    }
    public void setJobCronExpression(String jobCronExpression) {
        JobCronExpression = jobCronExpression;
    }
    public String getJobDesc() {
        return JobDesc;
    }
    public void setJobDesc(String jobDesc) {
        JobDesc = jobDesc;
    }
    public Class<?> getJobClass() {
        return JobClass;
    }
    public void setJobClass(Class<?> jobClass) {
        JobClass = jobClass;
    }
    public String getJobMethod() {
        return JobMethod;
    }
    public void setJobMethod(String jobMethod) {
        JobMethod = jobMethod;
    }
    public String getJobMethodSwhere1() {
        return JobMethodSwhere1;
    }
    public void setJobMethodSwhere1(String jobMethodSwhere1) {
        JobMethodSwhere1 = jobMethodSwhere1;
    }
    public String getJobMethodSwhere2() {
        return JobMethodSwhere2;
    }
    public void setJobMethodSwhere2(String jobMethodSwhere2) {
        JobMethodSwhere2 = jobMethodSwhere2;
    }
    public String getJobStatus() {
        return JobStatus;
    }
    public void setJobStatus(String jobStatus) {
        JobStatus = jobStatus;
    }
    
}

JobFactory.java

package com.zbb.quartz2;

import java.lang.reflect.Method;
import java.util.Date;

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

public class JobFactory implements Job{

    public void execute(JobExecutionContext context) throws JobExecutionException {
        // TODO Auto-generated method stub
        System.out.println(new Date().toLocaleString()+"JobFactory");
        JobBean jobBean = (JobBean)context.getMergedJobDataMap().get("jobBean");
        Class<?> jobClass = jobBean.getJobClass();
        try {
            Object newInstance = jobClass.newInstance();
            Method method = jobClass.getMethod(jobBean.getJobMethod(), new Class[]{});
            method.invoke(newInstance, new Object[]{});
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
        
    }

}

JobTask.java

package com.zbb.quartz2;

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.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class JobTask {
    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;
    public void execute() throws Exception {
        JobBean jobBean = new JobBean();
        jobBean.setJobId("id");
        jobBean.setJobGroupName("group");
        jobBean.setJobClass(Class.forName("com.zbb.quartz2.JobTest"));
        jobBean.setJobCronExpression("0/5 * * * * ?");
        jobBean.setJobMethod("run");
        
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        
        addJob(jobBean, scheduler);
        new Thread(new MyRunable(jobBean, scheduler, new JobTask())).start();
    }

    public void addJob(JobBean jobBean,Scheduler scheduler) throws Exception {
        TriggerKey triggerKey = TriggerKey.triggerKey(jobBean.getJobId(), jobBean.getJobGroupName());
        Trigger trigger = scheduler.getTrigger(triggerKey);
        JobDetail jobDetail = JobBuilder.newJob(JobFactory.class).withIdentity(jobBean.getJobId(), jobBean.getJobGroupName()).build();
        jobDetail.getJobDataMap().put("jobBean", jobBean);
        CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(jobBean.getJobCronExpression());
        trigger = TriggerBuilder.newTrigger().withIdentity(jobBean.getJobId(), jobBean.getJobGroupName()).withSchedule(cronSchedule).build();
        scheduler.scheduleJob(jobDetail, trigger);
    }

    public void deleteJob(JobBean jobBean,Scheduler scheduler) throws Exception {
        JobKey jobKey = JobKey.jobKey(jobBean.getJobId(), jobBean.getJobGroupName());
        scheduler.deleteJob(jobKey);
    }
}

JobTest.java

package com.zbb.quartz2;

import java.util.Date;

public class JobTest {
    public void run(){
        System.out.println(new Date().toLocaleString()+"JobTest");
    }
}

MyRunable.java

package com.zbb.quartz2;

import org.quartz.JobKey;
import org.quartz.Scheduler;

public class MyRunable implements Runnable {
    JobBean jobBean;
    Scheduler scheduler;
    JobTask jobTask;
    public MyRunable(JobBean jobBean,Scheduler scheduler,JobTask jobTask){
        this.jobBean=jobBean;
        this.scheduler=scheduler;
        this.jobTask=jobTask;
    }
    public void run() {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(10000);
            jobTask.deleteJob(jobBean, scheduler);
            jobBean.setJobCronExpression("0/1 * * * * ?");
            jobTask.addJob(jobBean, scheduler);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

Test.java

package com.zbb.quartz2;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("com/zbb/config/spring-servlet.xml");
    }
}

4、xml

spring-servlet.xml

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />
     <!-- com.zbb.quartz2 -->
    <bean id="jobMethod" class="com.zbb.quartz2.JobTask" init-method="execute"/>
    

 

posted @ 2017-02-28 22:36  DaryllD  阅读(692)  评论(0编辑  收藏  举报