鍍金池/ 教程/ Java/ Quartz2第一個(gè)程序
Quartz教程
Quartz2作業(yè)監(jiān)聽(tīng)
Quartz特點(diǎn)
Quartz執(zhí)行多作業(yè)
Quartz2第一個(gè)程序
Quartz列出調(diào)度器所有作業(yè)

Quartz2第一個(gè)程序

Quartz, 是一個(gè)企業(yè)級(jí)調(diào)度工作的框架,幫助Java應(yīng)用程序到調(diào)度工作/任務(wù)在指定的日期和時(shí)間運(yùn)行。

本教程教作為一個(gè)入門介紹如何開(kāi)發(fā)使用調(diào)度工作(在寫本教程時(shí)使用的最新Quartz 2.2.1 )

1. 下載Quartz

可以從官方網(wǎng)站或Maven中央存儲(chǔ)庫(kù)下載Quartz庫(kù)文件;

File : quartz.properties

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class =org.quartz.simpl.RAMJobStore

2. Quartz 作業(yè)

Quartz作業(yè)定義要運(yùn)行什么?

File : HelloJob

package com.yiibai.common;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class HelloJob implements Job
{
	public void execute(JobExecutionContext context)
	throws JobExecutionException {
 
		System.out.println("Hello Quartz!");	
 
	}
}

3. Quartz觸發(fā)器

定義Quartz觸發(fā)器,當(dāng)Quartz運(yùn)行在上面的Quartz作業(yè)。

像老版本的Quartz,仍然有兩種類型的觸發(fā)器在Quartz2,但API有些變化:

  • SimpleTrigger – 允許設(shè)置開(kāi)始時(shí)間,結(jié)束時(shí)間,重復(fù)間隔。
  • CronTrigger – 允許UNIX cron表達(dá)式來(lái)指定日期和時(shí)間來(lái)運(yùn)行作業(yè)。

SimpleTrigger – 每5秒運(yùn)行。

    Trigger trigger = TriggerBuilder
	.newTrigger()
	.withIdentity("dummyTriggerName", "group1")
	.withSchedule(
	    SimpleScheduleBuilder.simpleSchedule()
		.withIntervalInSeconds(5).repeatForever())
	.build();

CronTrigger – 每5秒運(yùn)行。

    Trigger trigger = TriggerBuilder
	.newTrigger()
	.withIdentity("dummyTriggerName", "group1")
	.withSchedule(
		CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
	.build();
注意
請(qǐng)閱讀 官方文檔 更多的Quartz2觸發(fā)器的例子。

4. Scheduler

調(diào)度類鏈接“工作”和“觸發(fā)器”到一起,并執(zhí)行它。

    	Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    	scheduler.start();
    	scheduler.scheduleJob(job, trigger);

5. 完整的例子

Quartz2 兩個(gè) SimpleTrigger 和 CronTrigger 完整的例子。

SimpleTrigger的例子 - 每間隔5秒運(yùn)行。

package com.yiibai.quartz;
 
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
 
public class SimpleTriggerExample {
	public static void main(String[] args) throws Exception {
 
		// Quartz 1.6.3
		// JobDetail job = new JobDetail();
		// job.setName("dummyJobName");
		// job.setJobClass(HelloJob.class);
 
		JobDetail job = JobBuilder.newJob(HelloJob.class)
			.withIdentity("dummyJobName", "group1").build();
 
                //Quartz 1.6.3
		// SimpleTrigger trigger = new SimpleTrigger();
		// trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
		// trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
		// trigger.setRepeatInterval(30000);
 
		// Trigger the job to run on the next round minute
		Trigger trigger = TriggerBuilder
			.newTrigger()
			.withIdentity("dummyTriggerName", "group1")
			.withSchedule(
				SimpleScheduleBuilder.simpleSchedule()
					.withIntervalInSeconds(5).repeatForever())
			.build();
 
		// schedule it
		Scheduler scheduler = new StdSchedulerFactory().getScheduler();
		scheduler.start();
		scheduler.scheduleJob(job, trigger);
 
	}
}

CronTrigger例子 - 同樣,在每5秒運(yùn)行作業(yè)。

package com.yiibai.quartz;
 
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
 
public class CronTriggerExample 
{
    public static void main( String[] args ) throws Exception
    {
    	//Quartz 1.6.3
    	//JobDetail job = new JobDetail();
    	//job.setName("dummyJobName");
    	//job.setJobClass(HelloJob.class);    	
    	JobDetail job = JobBuilder.newJob(HelloJob.class)
		.withIdentity("dummyJobName", "group1").build();
 
	//Quartz 1.6.3
    	//CronTrigger trigger = new CronTrigger();
    	//trigger.setName("dummyTriggerName");
    	//trigger.setCronExpression("0/5 * * * * ?");
 
    	Trigger trigger = TriggerBuilder
		.newTrigger()
		.withIdentity("dummyTriggerName", "group1")
		.withSchedule(
			CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
		.build();
 
    	//schedule it
    	Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    	scheduler.start();
    	scheduler.scheduleJob(job, trigger);
 
    }
}
代碼下載:http://pan.baidu.com/s/1c0s6xoO