简介
Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz 允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz 的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业。虽然可以通过属性文件(在属性文件中可以指定 JDBC 事务的数据源、全局作业和/或触发器侦听器、插件、线程池,以及更多)配置 Quartz,但它根本没有与应用程序服务器的上下文或引用集成在一起。结果就是作业不能访问 Web 服务器的内部函数;例如,在使用 WebSphere 应用服务器时,由 Quartz 调度的作业并不能影响服务器的动态缓存和数据源。
使用前奏
1. 新建一个maven项目(可以是web也可以是普通的java项目),添加上相关依赖。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <quartz.version>2.2.0</quartz.version> </properties>
<dependencies>
.... <!--quartz dependency--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>${quartz.version}</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>${quartz.version}</version> </dependency>
</dependencies>
2. 在resources下新建一个配置文件quartz.properties,添加如下内容:
org.quartz.scheduler.instanceName = MyScheduler #指定调度器的名称 org.quartz.threadPool.threadCount = 3 #线程池中最多同时有3个线程运行 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #指定Quartz的数据(job及trigger信息)存储位置,RamJobStore指内存
启动一个Scheduler
package com.netease.test.quartz; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; /** * User: hzwangxx * Date: 14-2-26 * Time: 0:16 */ public class QuartzTest { public static void main(String[] args) { try { //1.从StdSchedulerFactory工厂中获取一个任务调度器 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //2. 启动调度器 scheduler.start(); System.out.println("scheduler is start..."); //关闭调度器 scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } } /* console output: 2014-02-26 00:22:31,744 0 [main] INFO - Using default implementation for ThreadExecutor 2014-02-26 00:22:31,766 22 [main] INFO - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 2014-02-26 00:22:31,767 23 [main] INFO - Quartz Scheduler v.2.2.0 created. 2014-02-26 00:22:31,768 24 [main] INFO - RAMJobStore initialized. 2014-02-26 00:22:31,769 25 [main] INFO - Scheduler meta-data: Quartz Scheduler (v2.2.0) 'MyScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 2014-02-26 00:22:31,769 25 [main] INFO - Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 2014-02-26 00:22:31,769 25 [main] INFO - Quartz scheduler version: 2.2.0 2014-02-26 00:22:31,769 25 [main] INFO - Scheduler MyScheduler_$_NON_CLUSTERED started. scheduler is start... 2014-02-26 00:22:31,769 25 [main] INFO - Scheduler MyScheduler_$_NON_CLUSTERED shutting down. 2014-02-26 00:22:31,770 26 [main] INFO - Scheduler MyScheduler_$_NON_CLUSTERED paused. 2014-02-26 00:22:31,770 26 [main] INFO - Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete. */
简单Helloworld
package com.netease.test.quartz; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import java.util.Date; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; /** * User: hzwangxx * Date: 14-2-26 * Time: 0:16 */ public class QuartzTest { public static void main(String[] args) { try { //1.从StdSchedulerFactory工厂中获取一个任务调度器 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //2. 启动调度器 scheduler.start(); System.out.println("scheduler is start..."); //3. 添加定时任务 // 3.1 定义job JobDetail job = newJob(HelloJob.class) .withIdentity("job1", "group1") .build(); // 3.2 定义Trigger,使得job现在就运行,并每隔3s中运行一次,重复运行5次, withRepeatCount(number)设定job运行次数为number+1 Trigger trigger = newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(3) .withRepeatCount(4)) .build(); // 3.3 交给scheduler去调度 scheduler.scheduleJob(job, trigger); //4. 关闭调度器 //scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 自定义Job,实现Job接口并实现execute方法 */ public static class HelloJob implements Job{ public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("execute job at " + new Date() + " by trigger " + context.getTrigger().getJobKey()); } } } /* console output: 2014-02-26 01:05:25,766 0 [main] INFO - Using default implementation for ThreadExecutor 2014-02-26 01:05:25,794 28 [main] INFO - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 2014-02-26 01:05:25,795 29 [main] INFO - Quartz Scheduler v.2.2.0 created. 2014-02-26 01:05:25,797 31 [main] INFO - RAMJobStore initialized. 2014-02-26 01:05:25,798 32 [main] INFO - Scheduler meta-data: Quartz Scheduler (v2.2.0) 'MyScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 2014-02-26 01:05:25,798 32 [main] INFO - Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 2014-02-26 01:05:25,799 33 [main] INFO - Quartz scheduler version: 2.2.0 2014-02-26 01:05:25,799 33 [main] INFO - Scheduler MyScheduler_$_NON_CLUSTERED started. scheduler is start... execute job at Wed Feb 26 01:05:25 CST 2014 by trigger group1.job1 execute job at Wed Feb 26 01:05:28 CST 2014 by trigger group1.job1 execute job at Wed Feb 26 01:05:31 CST 2014 by trigger group1.job1 execute job at Wed Feb 26 01:05:34 CST 2014 by trigger group1.job1 execute job at Wed Feb 26 01:05:37 CST 2014 by trigger group1.job1 */
API说明
Quartz框架核心的几个类如下:
- Scheduler - 与scheduler进行交互的主要API,整个生命周期是由SchedulerFactory创建到调用shutdown()方法
- Job - 希望scheduler进行调度的自定义任务接口,主要实现该接口的execute方法。
- JobDetail - 定义Job的相关信息
- Trigger - 定义调度器调度任务的触发器,主要描述任务的执行时间,周期等信息
- JobBuilder - 用来定义和创建JobDetail实例
- TriggerBuilder - 用来定义和创建Trigger实例