任务调度框架Quartz(二) 使用job、trigger、schedule调用定时任务

读完第一节,我们已经对Quartz有了一个大体的认识,它可以定时帮我们执行一些处理程序,尽管可能你之前对Quartz不了解以至于第一节很多术语(job、trigger等)不理解,没关系,从现在开始,笔者和你一起在实例中深入理解它们。没有code,就没有话语权——程序界真理

本系列教程主要使用Maven风格编写代码,对于怎样使用maven,大家可以读读许晓斌的《Maven实战》,相当不错,笔者入门也是得此书。认真耐心读完哦。

本节主要内容

  • 下载 Quartz
  • 安装 Quartz
  • 根据你自己的需要配置 Quartz
  • 编写第一个示例应用程序

下载和安装Quartz

首先,你需要下载最新(不一定)稳定版本的Quartz。本系列教程我们使用2.2.1。你可以在官网http://www.quartz-scheduler.org/downloads/下载需要的版本jar包,现在我们使用maven方式:

pom.xml引入quartz依赖:

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.2.1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

quartz 属性配置文件

Quartz使用一个quartz.properties的属性配置文件。它不是必需的,但是使用它的话,你必须将它放置在你项目的classpath目录下,也就是最终打包后位于WEN-INF/classes目录下(web项目),使用Maven我们在src/main/resources目录放置该文件即可。 

这里写图片描述

Quartz是一个可配置化的应用程序。最好的配置Quartz的方式是使用quartz.properties配置文件。关于Quartz的配置属性文件的详细技巧,我们在本系列教程最后会单独讲到,务必精益求精。 
迅速开始一个配置文件的内容,可以参考如下示例编写:

quartz.properties

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

使用上述配置文件创建的任务调度程序有以下特点:

  • org.quartz.scheduler.instanceName - 设置调度程序scheduler的名称为MyScheduler
  • org.quartz.threadPool.threadCount - 线程里设置了3个线程,意味着最多同时运行3个job
  • org.quartz.jobStore.class - 指定为RAMJobStore,表示所有Quartz的数据,包括jobDetail、trigger等均运行在内存中(而不是数据库中)。 如果你想Quartz使用你的数据库,还是建议你在使用数据库配置之前使用RAMJobStore进行工作。通过使用一个数据库,你可以打开一个全新的维度,但在这之前,建议你使用RAMJobStore。

编写第一个quartz示例程序

第一步:编写一个job类,需要实现org.quartz.Job接口

这里编写一个样例,该任务只做一件事,就是打印任务执行时间以及汇报任务已经执行。Hello.java代码如下:

package org.byron4j.quartz;

import org.byron4j.utils.DateUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 实现org.quartz.Job接口,声明该类是一个可执行任务类
 * @author Administrator
 *
 */
public class HelloJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("现在是北京时间:" + DateUtil.getCurrDateTime() + " - helloJob任务执行");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

第二步:编写日期时间工具类,提供获取不同格式时间的公共方法

DateUtil.java代码如下:

package org.byron4j.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    private static SimpleDateFormat officerSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getCurrDateTime(){
        return officerSdf.format(new Date());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

第三步:使用job、trigger、schedule调用定时任务

在该实例中我们使用了静态引入,引入了3个静态方法

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
  • 1
  • 2
  • 3

你也可以分别使用替代,视个人习惯选择:

JobBuilder.newJob(...)
SimpleScheduleBuilder.simpleSchedule(...)
TriggerBuilder.newTrigger(...)
  • 1
  • 2
  • 3

QuartzTest.java代码如下:

package org.byron4j.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTest {

    public static void main(String[] args) {

        try {

            //从调度程序工厂获取一个调度程序的实例
            Scheduler  scheduler  = StdSchedulerFactory.getDefaultScheduler();

            //显示调度程序的名称(这里会展示我们在quartz.properties文件中的名称)
            System.out.println("scheduleName = " + scheduler.getSchedulerName());


            /** 重要:
             *  定义一个job,并绑定到我们自定义的HelloJob的class对象
             *  这里并不会马上创建一个HelloJob实例,实例创建是在scheduler安排任务触发执行时创建的
             *  这种机制也为后面使用Spring集成提供了便利
             */
              JobDetail job = newJob(HelloJob.class)
                  .withIdentity("job1", "group1")
                  .build();

              // 声明一个触发器,现在就执行(schedule.start()方法开始调用的时候执行);并且每间隔2秒就执行一次
              Trigger trigger = newTrigger()
                  .withIdentity("trigger1", "group1")
                  .startNow()
                        .withSchedule(simpleSchedule()
                          .withIntervalInSeconds(2)
                          .repeatForever())            
                  .build();

              // 告诉quartz使用定义的触发器trigger安排执行任务job
              scheduler.scheduleJob(job, trigger);

            //启动任务调度程序,内部机制是线程的启动
            scheduler.start();

            //关闭任务调度程序,如果不关闭,调度程序schedule会一直运行着
            //scheduler.shutdown();

        } catch (SchedulerException e) {

            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

重点指明一下job实例创建的时机: 
/** 重要: 
* 定义一个job,并绑定到我们自定义的HelloJob的class对象 
* 这里并不会马上创建一个HelloJob实例,实例创建是在scheduler安排任务触发执行时创建的 
* 这种机制也为后面使用Spring集成提供了便利 
*/ 
JobDetail job = newJob(HelloJob.class) 
.withIdentity(“job1”, “group1”) 
.build();

第四步:执行调用你的定时任务

运行QuartzTest.java的main方法,我们可以看到控制台输出,每隔2秒就执行了我们预先安排的打印时间和工作内容的任务:

scheduleName = MyScheduler
现在是北京时间:2016-11-05 13:08:30 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:32 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:34 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:36 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:38 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:40 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:42 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:44 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:46 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:48 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:50 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:52 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:54 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:56 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:58 - helloJob任务执行
现在是北京时间:2016-11-05 13:09:00 - helloJob任务执行

猜你喜欢

转载自blog.csdn.net/zzzffhh/article/details/80733706