Quartz任务调度入门(二)

SimpleTriggerRunner.java
package com.wj.quartz;

import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 必要的三个jar包为 
 * jta.jar 
 * commons-collections-3.2.1.jar 
 * quartz-all-1.6.0.jar
 * 
 * @author jcom
 * @date 2010-9-28
 * 
 */
public class SimpleTriggerRunner
{

	public void task() throws SchedulerException
	{
		//实例Scheduler工厂
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		//获取Scheduler任务
		Scheduler scheduler = schedulerFactory.getScheduler();

		//当前时间
		long ctime = System.currentTimeMillis();

		/*
		 * 实例任务
		 * 指定任务名、组名、执行任务的类
		 */
		JobDetail jobDetail = new JobDetail("jobDetail-s1", "jobDetailGroup-s1", SimpleQuartzJob.class);
		
		/*
		 * 实例规则
		 * 指定表达式名、组名
		 */
		SimpleTrigger simpleTrigger = new SimpleTrigger("simpleTrigger", "triggerGroup-s1");
		//设置启动的时间
		simpleTrigger.setStartTime(new Date(ctime));
		//设置执行任务的间隔时间
		simpleTrigger.setRepeatInterval(1000);
		//设置重复执行的次数
		simpleTrigger.setRepeatCount(1);
		// 添加任务、规则
		scheduler.scheduleJob(jobDetail, simpleTrigger);

		//启动
		scheduler.start();
	}

	public static void main(String args[])
	{
		try
		{
			SimpleTriggerRunner qRunner = new SimpleTriggerRunner();
			qRunner.task();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(quartz)