Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。今天我们就来学习一下它的使用,这里会分篇章对它进行介绍。只是希望能有个人,在我说没事的时候,知道我不是真的没事;能有个人,在我强颜欢笑的时候,知道我不是真的开心。

quartz的使用案例

我的测试环境用的是maven,这次的测试代码是作为一个maven模块编写的(可能引用了父模块的jar依赖),首先添加quartz的依赖。

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>

项目结构如下:

一、quartz的配置,这个不是必须的但是比较有用

  quartz是可以灵活配置的,而最好的方式就是在应用的classpath上创建quartz.properties文件进行配置。详细的配置项,可以参考:http://www.quartz-scheduler.org/documentation/quartz-2.1.x/configuration/。下面的内容是我们本次测试的配置。

# This scheduler’s name will be “MyScheduler”.
org.quartz.scheduler.instanceName = MyScheduler
# There are threads in the thread pool, which means that a maximum of jobs can be run simultaneously.
org.quartz.threadPool.threadCount =
# All of Quartz’s data, such as details of jobs and triggers, is held in memory (rather than in a database)
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

二、开始我们的程序的编写

package com.linux.huhx.example1;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory; /**
* @Author: huhx
* @Date: 2017-11-23 上午 8:59
*/
public class QuartzTest {
public static void main(String[] args) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start(); scheduler.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}

运行上述的代码,如果已经配置了日志,可以看到如下的控制台输出。

::18.593 [main] INFO  org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
::18.624 [main] INFO o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
::18.624 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3. created.
::18.640 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
::18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.0) 'MyScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed:
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. ::18.640 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
::18.640 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.
::18.640 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of triggers
::18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED started.
::18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutting down.
::18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED paused.
::18.640 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutting down threadpool...
::18.640 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutdown of threadpool complete.
::18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete.
::19.109 [MyScheduler_Worker-] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
::19.109 [MyScheduler_Worker-] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
::19.109 [MyScheduler_Worker-] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.

  需要注意的一点就是:如果我们通过StdSchedulerFactory.getDefaultScheduler()的方式维护一个调度器,我们的应用不会终止直到scheduler.shutdown()方法被调用。下面我们添加一个简单的Hello的任务并加以调度。这里面我们提供QuartzTest完整的代码,我们在调度器开始后,让程序睡眠10秒。这样可以在shutdown之前,可以看到HelloJob的调度执行。

package com.linux.huhx.example1;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory; import java.util.concurrent.TimeUnit; /**
* @Author: huhx
* @Date: 2017-11-23 上午 8:59
*/
public class QuartzTest {
public static void main(String[] args) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start(); // define the job and tie it to our HelloJob class
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build(); // 每5秒运行一次job
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever())
.build(); // Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
TimeUnit.SECONDS.sleep(10);
scheduler.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}

我们的HelloJob的代码比较简单就是输出字符串:hello world.

package com.linux.huhx.example1;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; /**
* @Author: huhx
* @Date: 2017-11-23 上午 9:02
*/
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("hello world.");
}
}

运行QuartzTest,可以看到如下的日志输出。由于我们的调度器是每5秒执行一次HelloJob,睡眠10秒之后会执行3次。

09:39:50.488 [main] INFO  org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
09:39:50.519 [main] INFO o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
09:39:50.519 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.0 created.
09:39:50.519 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
09:39:50.519 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.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. 09:39:50.519 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
09:39:50.519 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.0
09:39:50.519 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED started.
09:39:50.519 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
09:39:50.535 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
09:39:50.535 [MyScheduler_QuartzSchedulerThread] DEBUG o.q.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.job1', class=com.linux.huhx.example1.HelloJob
09:39:50.551 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
09:39:50.551 [MyScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.job1
hello world.
09:39:55.520 [MyScheduler_QuartzSchedulerThread] DEBUG o.q.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.job1', class=com.linux.huhx.example1.HelloJob
09:39:55.520 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
09:39:55.520 [MyScheduler_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.job1
hello world.
09:40:00.521 [MyScheduler_QuartzSchedulerThread] DEBUG o.q.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.job1', class=com.linux.huhx.example1.HelloJob
09:40:00.521 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
09:40:00.521 [MyScheduler_Worker-3] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.job1
hello world.
09:40:00.536 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutting down.
09:40:00.536 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED paused.
09:40:00.536 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutting down threadpool...
09:40:00.536 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutdown of threadpool complete.
09:40:00.536 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete.
09:40:00.583 [MyScheduler_Worker-2] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
09:40:00.615 [MyScheduler_Worker-1] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
09:40:01.021 [MyScheduler_Worker-3] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.

友情链接

java框架---->quartz的使用(一)的更多相关文章

  1. java框架---->quartz整合spring(一)

    今天我们学习一下quartz的定时器的使用.年轻时我们放弃,以为那只是一段感情,后来才知道,那其实是一生. quartz的简单实例 测试的项目结构如下: 一.pom.xml中定义quartz的依赖 & ...

  2. Java任务调度开源框架quartz学习

    一.quartz学习 Java框架介绍:Quartz从入门到进阶 http://edu.yesky.com/edupxpt/233/2209233.shtml 1.例子:http://javacraz ...

  3. Java任务调度框架Quartz入门

    Quartz[kwɔːts]:石英,其框架和名字一样简单朴素又不失魅力,在Java程序界,Quartz大名鼎鼎,很多Java应用几乎都集成或构建了一个定时任务调度系统,Quartz是一个定时任务调度框 ...

  4. 通过源码分析Java开源任务调度框架Quartz的主要流程

    通过源码分析Java开源任务调度框架Quartz的主要流程 从使用效果.调用链路跟踪.E-R图.循环调度逻辑几个方面分析Quartz. github项目地址: https://github.com/t ...

  5. 开源调度框架Quartz最佳实践

    开源调度框架Quartz最佳实践 Quartz是一个Java调度框架,当前的最新版本为2.2.1. 以Quartz 2.2.1版为例,Quartz最佳实践(用于生产系统)总结如下: 1.跳过更新检查Q ...

  6. java框架之SpringBoot(14)-任务

    使用 maven 创建 SpringBoot 项目,引入 Web 场景启动器. 异步任务 1.编写异步服务类,注册到 IoC 容器: package zze.springboot.task.servi ...

  7. 任务调度开源框架Quartz概述

    任务调度开源框架Quartz 几乎每个项目中都用到了自动任务处理功能.所以在任务调度的功能很常用,但是一个好的任务调度程序是一个颇具挑战性的工作.最近用到Quartz这个框架,感觉很好,所以进行学习. ...

  8. 企业级任务调度框架Quartz(2)-下载和安装Quartz

    1.下载和安装 Quartz 根据资料上提供的网址http://www.opensymphony.com/quartz 我们可以下载到Quartz的最新版本1.6.4: 2.下载后包的说明       ...

  9. 企业级任务调度框架Quartz(1) --企业应用中的任务调度介绍

    由于目前的工作内容为建行CLPM批处理业务的设计工作,所以很好的理解批处理所用的任务调度框架Quartz势在必行:为了能够更好的去服务于工作,也 为了提升自己,所以我学习了Quartz Job Sch ...

随机推荐

  1. POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3007   Accep ...

  2. Riak

    出处:http://www.oschina.net/p/riak Riak是以 Erlang 编写的一个高度可扩展的分布式数据存储,Riak的实现是基于Amazon的Dynamo论文,Riak的设计目 ...

  3. 用grep 筛选fastq 序列

    grep 从文件中筛选出 包含指定的字符或者正则表达式的行:默认只打印匹配到的行, 比如一个文件 test.txt, 其内容为: abc def ghi jkl grep a test.txt, 输出 ...

  4. Unity3D Shader基础教程

    原文地址:http://bbs.9ria.com/thread-212557-1-1.html 此教程将指引你如何建立自己的Shaders,让你的游戏场景看起来更好.Unity配备了强大的阴影和材料的 ...

  5. Logback中文文档(三):配置

    在第一部分,我们将介绍配置 logback 的各种方法,给出了很多配置脚本例子.在第二部分,我们将介绍 Joran,它是一个通用配置框架,你可以在自己的项目里使用 Joran. Logback里的配置 ...

  6. RMAN:简单的duplicate创建新数据库 for 12c+

    构建参数文件 *.db_name='test2' ##### 需要注意的地方,和rman的duplicate目标库一致 *.compatible='18.0.0' ##### 关键的地方,每个版本的模 ...

  7. 使用apache的poi实现导入导出excel

    1.jar包:poi-3.14-20160307.jar.poi-ooxml-3.14-20160307.jar 2.导入(本例实现了解析excel生成List): @Override public ...

  8. python中,如何将多行进行输出,同时将行尾的换行符去掉

    需求说明: 比如我要输出字符串的常量,字符串常量要输出多行,该怎么解决呢 操作过程: 1.可以通过三引号(""" .... """)将要输出 ...

  9. linux 中搜索命令的对比

    1.find find是最常用和最强大的查找命令.它能做到实时查找,精确查找,但速度慢. find的使用格式如下: #find [指定目录] [指定条件] [指定动作] 指定目录:是指所要搜索的目录和 ...

  10. 关于UIGestureRecognizerState

    UIGestureRecognizerState的定义如下 typedef enum { UIGestureRecognizerStatePossible, UIGestureRecognizerSt ...