Quartz 정시 작업 도구 클래스

8684 단어 JavaWeb
package com.jeeplus.common.utils;

import java.util.Map;

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.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Quartz    util
 * 
 * @author xc
 *
 */
public class QuartzManager {

	private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); //     SchedulerFactory    
	private static String JOB_GROUP_NAME = "FH_JOBGROUP_NAME"; //    
	private static String TRIGGER_GROUP_NAME = "FH_TRIGGERGROUP_NAME"; //     

	/**
	 *         ,         ,    ,     
	 * 
	 * @param jobName
	 *               
	 * @param cls
	 *              
	 * @param time
	 *                ,  quartz    
	 */
	public static void addJob(String jobName, Class extends Job> cls, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler(); //   SchedulerFactory  Scheduler  
			JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build(); //     Job             ,        
			CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() //       TriggerBuilder        
					.withIdentity(jobName, TRIGGER_GROUP_NAME) //             
					.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
			sched.scheduleJob(jobDetail, trigger);
			if (!sched.isShutdown()) {
				sched.start(); //   
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *         ,         ,    ,      (   )
	 * 
	 * @param jobName
	 *               
	 * @param cls
	 *              
	 * @param time
	 *                ,  quartz    
	 */
	public static void addJob(String jobName, Class extends Job> cls, String time, Map parameter) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler(); //   SchedulerFactory  Scheduler  
			JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build(); //     Job             ,        
			jobDetail.getJobDataMap().put("parameterList", parameter); //    
			CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() //       TriggerBuilder        
					.withIdentity(jobName, TRIGGER_GROUP_NAME) //             
					.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
			sched.scheduleJob(jobDetail, trigger);
			if (!sched.isShutdown()) {
				sched.start(); //   
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *         
	 * 
	 * @param jobName
	 *               
	 * @param jobGroupName
	 *                
	 * @param triggerName
	 *                
	 * @param triggerGroupName
	 *                 
	 * @param jobClass
	 *              
	 * @param time
	 *                ,  quartz    
	 */
	public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName,
			Class extends Job> jobClass, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();//    ,   ,     
			CronTrigger trigger = (CronTrigger) TriggerBuilder //    
					.newTrigger().withIdentity(triggerName, triggerGroupName)
					.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
			sched.scheduleJob(jobDetail, trigger);
			if (!sched.isShutdown()) {
				sched.start(); //   
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *          (   )
	 * 
	 * @param jobName
	 *               
	 * @param jobGroupName
	 *                
	 * @param triggerName
	 *                
	 * @param triggerGroupName
	 *                 
	 * @param jobClass
	 *              
	 * @param time
	 *                ,  quartz    
	 */
	public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName,
			Class extends Job> jobClass, String time, Map parameter) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			JobDetail jobDetail = JobBuilder.newJob((Class extends Job>) jobClass).withIdentity(jobName, jobGroupName)
					.build();//    ,   ,     
			jobDetail.getJobDataMap().put("parameterList", parameter); //    
			CronTrigger trigger = (CronTrigger) TriggerBuilder //    
					.newTrigger().withIdentity(triggerName, triggerGroupName)
					.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
			sched.scheduleJob(jobDetail, trigger);
			if (!sched.isShutdown()) {
				sched.start(); //   
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *            (         ,    ,     )
	 * 
	 * @param jobName
	 *               
	 * @param time
	 *                  
	 */
	public static void modifyJobTime(String jobName, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler(); //   SchedulerFactory  Scheduler  
			TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME); //            TriggerKey
			CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); //   TriggerKey  CronTrigger
			if (trigger == null) {
				return;
			}
			String oldTime = trigger.getCronExpression();
			if (!oldTime.equalsIgnoreCase(time)) {
				JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME); //           JobKey
				JobDetail jobDetail = sched.getJobDetail(jobKey);
				Class extends Job> objJobClass = jobDetail.getJobClass();
				removeJob(jobName);
				addJob(jobName, objJobClass, time);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *            
	 * 
	 * @param triggerName
	 *                
	 * @param triggerGroupName
	 *                    
	 * @param time
	 *                    
	 */
	public static void modifyJobTime(String triggerName, String triggerGroupName, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler(); //   SchedulerFactory  Scheduler  
			TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); //            TriggerKey
			CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); //   TriggerKey  CronTrigger
			if (trigger == null)
				return;
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(trigger.getCronExpression());
			String oldTime = trigger.getCronExpression();
			if (!oldTime.equalsIgnoreCase(time)) {
				trigger = (CronTrigger) trigger.getTriggerBuilder() //     trigger
						.withIdentity(triggerKey).withSchedule(scheduleBuilder)
						.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
				sched.rescheduleJob(triggerKey, trigger); //    trigger    job  
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *       (         ,    ,     )
	 * 
	 * @param jobName
	 *                
	 */
	public static void removeJob(String jobName) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME); //            TriggerKey
			JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME); //           JobKey
			sched.pauseTrigger(triggerKey); //      
			sched.unscheduleJob(triggerKey);//      
			sched.deleteJob(jobKey); //     
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *       
	 * 
	 * @param jobName
	 *               
	 * @param jobGroupName
	 *                
	 * @param triggerName
	 *                
	 * @param triggerGroupName
	 *                 
	 */
	public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); //            TriggerKey
			JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); //           JobKey
			sched.pauseTrigger(triggerKey); //      
			sched.unscheduleJob(triggerKey);//      
			sched.deleteJob(jobKey); //     
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *         
	 */
	public static void startJobs() {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			sched.start();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *         
	 */
	public static void shutdownJobs() {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			if (!sched.isShutdown()) {
				sched.shutdown();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}

좋은 웹페이지 즐겨찾기