Quartz2第一个程序 - Quartz教程

Quartz, 是一个企业级调度工作的框架,帮助Java应用程序到调度工作/任务在指定的日期和时间运行。

本教程教作为一个入门介绍如何开发使用调度工作(在写本教程时使用的最新Quartz 2.2.1 )

1. 下载Quartz

可以从官方网站或Maven中央存储库下载Quartz库文件;

_File : _quartz.properties

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

2. Quartz 作业

Quartz作业定义要运行什么?

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触发器

定义Quartz触发器,当Quartz运行在上面的Quartz作业。

像老版本的Quartz,仍然有两种类型的触发器在Quartz2,但API有些变化:

  • SimpleTrigger – 允许设置开始时间,结束时间,重复间隔。
  • CronTrigger – 允许UNIX cron表达式来指定日期和时间来运行作业。

SimpleTrigger – 每5秒运行。

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

CronTrigger – 每5秒运行。

    Trigger trigger = TriggerBuilder
    .newTrigger()
    .withIdentity("dummyTriggerName", "group1")
    .withSchedule(
        CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
    .build();

注意 请阅读 官方文档 更多的Quartz2触发器的例子。

4. Scheduler

调度类链接“工作”和“触发器”到一起,并执行它。

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

5. 完整的例子

Quartz2 两个 SimpleTrigger 和 CronTrigger 完整的例子。

SimpleTrigger的例子 - 每间隔5秒运行。

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秒运行作业。

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