Quartz入门

/**
 * 2012-8-9
 * [email protected]
 */
package org.quartz.examples.example2;

import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author  li
 * 简单Quatz例子
 */
public class Test {
	public static void main(String[] args) throws SchedulerException,
			InterruptedException {
		//创建一个定时器工厂
		SchedulerFactory scheduler = new StdSchedulerFactory();
		//创建一个简单的定时器生成器,该生成器根据一定的条件生成需要的定时器,比如按指定的时间间隔重复执行的简单定时器
		SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder
				.simpleSchedule().withIntervalInSeconds(1).repeatForever();
		//创建一个触发器,DateBuilder负责创建复杂的任务
		Trigger trigger = TriggerBuilder.newTrigger()
				.startAt(DateBuilder.nextGivenSecondDate(null, 2))
				.withSchedule(schedBuilder).build();
		//创建job明细,我们要执行的任务必须实现org.quartz.Job接口
		JobDetail jobDetail = JobBuilder.newJob(Work.class).build();
		//获取定时器
		Scheduler ss = scheduler.getScheduler();
		//添加job到定时器,需要2个东西==》JobDetail和JobDetail
		ss.scheduleJob(jobDetail, trigger);
		//启动定时器
		ss.start();
		Thread.sleep(65L * 1000L);
	}

}


你可能感兴趣的:(Quartz入门)