Quartz任务调度入门(一)

CronTriggerRunner.java

package com.wj.quartz;

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
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 CronTriggerRunner
{

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

		/*
		 * 实例任务
		 * 指定任务名、组名、执行任务的类
		 */
		JobDetail jobDetail = new JobDetail("jobDetail2", "jobDetailGroup2", SimpleQuartzJob.class);
		/*
		 * 实例规则
		 * 指定表达式名、组名
		 */
		CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup2");		
		try
		{
			// 定义规则
			CronExpression cexp = new CronExpression("0/5 * * * * ?");
			// 设置规则
			cronTrigger.setCronExpression(cexp);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		// 添加任务、规则
		scheduler.scheduleJob(jobDetail, cronTrigger);

		// 启动
		scheduler.start();
	}

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

SimpleQuartzJob.java
package com.wj.quartz;

import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

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

	public SimpleQuartzJob()
	{
	}

	public void execute(JobExecutionContext context)
			throws JobExecutionException
	{
		System.out.println("In SimpleQuartzJob - executing its JOB at "
				+ new Date() + " by " + context.getTrigger().getGroup() + "=="
				+ context.getTrigger().getName());
	}
}

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