java 使用Quartz定时任务调度

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public abstract class AbstractExpSched {
	/**
	 * 设置调度任务启动参数
	 * @param cls 启动任务类名称
	 * @param cronExpression 启动表达式
	 * @param taskName 任务名称
	 * @throws Exception
	 */
	public void set(Class<?> cls,String cronExpression,String taskName,String job)throws  Exception{
		//SchedulerFactory 提供一种获取调度程序实例的机制 
		SchedulerFactory schedFact = new StdSchedulerFactory();
		//获取调度程序
		Scheduler sched = schedFact.getScheduler();
		//Conveys the detail properties of a given Job instance.new JobDetail(String name, String group, Class jobClass) 
		JobDetail jobDetailStand = new JobDetail(job, "expGroup",cls);
		//Get the JobDataMap that is associated with the Job.
		jobDetailStand.getJobDataMap().put("name", taskName);
		//A concrete Trigger that is used to fire a JobDetail at given moments in time, defined with Unix 'cron-like' definitions.
		CronTrigger triggerStand = new CronTrigger(taskName, "expGroup");
		//设置定时机制cronExpression如:"0 0 12 * * ?"   
		triggerStand.setCronExpression(cronExpression); 
		// Add the given JobDetail to the Scheduler, and associate the given Trigger with it. 
		sched.scheduleJob(jobDetailStand, triggerStand);
		sched.start();
	}
	
	public abstract void start();
}

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