自定义Quartz工具类,实现任务添加、移除、修改功能

pom.xml

    
        
            junit
            junit
            4.12
            test
        

        
            org.quartz-scheduler
            quartz
            2.3.0
        

        
            org.quartz-scheduler
            quartz-jobs
            2.3.0
        

        
            org.springframework
            spring-context
            4.3.13.RELEASE
        

        
            org.springframework
            spring-core
            4.3.13.RELEASE
        

        
            org.springframework
            spring-beans
            4.3.13.RELEASE
        

        
            org.springframework
            spring-test
            4.3.13.RELEASE
            test
        

        
            org.springframework
            spring-context-support
            4.3.13.RELEASE
        

        
            org.springframework
            spring-tx
            4.3.13.RELEASE
        

        
            org.slf4j
            slf4j-api
            1.7.25
        

        
            log4j
            log4j
            1.2.17
        
    

spring配置文件application.xml



    
    
    
    
    


spring工具类SpringUtils

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware{
    public static ApplicationContext applicationContext;

    /**
     * 初始化applicationContext对象
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }
    
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
    
    /**
     * 获取对象
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }
}

Quartz工具类QuartzUtils

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

/**
 *  定时任务工具类
 *  实现功能包括:添加任务、移除任务、修改任务时间、启动所有任务
 *  @author LWB 
 */
public class QuartzUtils {
    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory(); 
    
    /**
     * 添加定时任务
     * @param jobName   任务名
     * @param jobGroup  任务组
     * @param triggerName   触发器名
     * @param triggerGroup  触发器组
     * @param cronExpression    触发时间表达式
     * @param beanName  任务类的bean ID
     */
    public static void addJob(String jobName,String jobGroup,String triggerName,String triggerGroup,String cronExpression,String beanName){
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            
            //任务名,任务组,任务类
            Job job = (Job) SpringUtils.getBean(beanName);
            JobDetail jobDetail = JobBuilder.newJob(job.getClass()).withIdentity(jobName, jobGroup).build();
            
            //触发器名,触发器组
            TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroup);
            triggerBuilder.startNow();
            //触发时间设定
            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
            //创建Trigger对象
            CronTrigger trigger = (CronTrigger) triggerBuilder.build();
            
            scheduler.scheduleJob(jobDetail, trigger);
        
            scheduler.start();
            
        } catch (SchedulerException e) {
            e.printStackTrace();
        }finally {
            System.out.println("......添加定时任务......");
        }
    }
    
    /**
     * 修改一个任务的时间的触发时间
     * @param jobName   任务名
     * @param jobGroup  任务组
     * @param triggerName   触发器名
     * @param triggerGroup  触发器组
     * @param cronExpression    触发时间表达式
     */
    public static void modifyJob(String jobName,String jobGroup,String triggerName,String triggerGroup,String cronExpression){
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if(oldTrigger == null){
                return ;
            }
            
            //获取当前触发器的时间表达式
            String oldCronExpression = oldTrigger.getCronExpression();
            
            if( !oldCronExpression.equalsIgnoreCase(cronExpression) ){
                //触发器名,触发器组
                TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerName,triggerGroup);
                triggerBuilder.startNow();
                
                //设置任务触发的时间表达式
                triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
                
                //创建Trigger对象
                CronTrigger newTrigger = (CronTrigger) triggerBuilder.build();
                
                scheduler.rescheduleJob(triggerKey, newTrigger);
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }finally {
            System.out.println("......修改定时任务......");
        }
    }
    
    /**
     * 启动所有任务
     */
    public static void startJob(){
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }finally {
            System.out.println("......启动所有定时任务......");
        }
    }
    
    /**
     * 移除一个任务
     * @param jobName   任务名
     * @param jobGroup  任务组
     * @param triggerName   触发器名
     * @param triggerGroup  触发器组
     */
    public static void removeJob(String jobName,String jobGroup,String triggerName,String triggerGroup){
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            //移除触发器 
            scheduler.unscheduleJob(TriggerKey.triggerKey(triggerName, triggerGroup));
            //删除任务
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
            
        } catch (SchedulerException e) {
            e.printStackTrace();
        }finally {
            System.out.println("......移除一个定时任务......");
        }
    }
}

单元测试Quartz工具类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application4.xml"})
public class MyTestOne {

    @Test
    public void one() throws InterruptedException{
        QuartzUtils.addJob("job", "one", "trigger", "one", "0/1 * * * * ?", "taskOne");
        QuartzUtils.addJob("job", "two", "trigger", "two", "0/1 * * * * ?", "taskTwo");
        Thread.sleep(3000);
        QuartzUtils.modifyJob("job", "one", "trigger", "one", "0/3 * * * * ?");
        Thread.sleep(6000);
    }
}

你可能感兴趣的:(自定义Quartz工具类,实现任务添加、移除、修改功能)