java框架---->quartz的使用(一)
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的使用(一)的更多相关文章
- java框架---->quartz整合spring(一)
今天我们学习一下quartz的定时器的使用.年轻时我们放弃,以为那只是一段感情,后来才知道,那其实是一生. quartz的简单实例 测试的项目结构如下: 一.pom.xml中定义quartz的依赖 & ...
- Java任务调度开源框架quartz学习
一.quartz学习 Java框架介绍:Quartz从入门到进阶 http://edu.yesky.com/edupxpt/233/2209233.shtml 1.例子:http://javacraz ...
- Java任务调度框架Quartz入门
Quartz[kwɔːts]:石英,其框架和名字一样简单朴素又不失魅力,在Java程序界,Quartz大名鼎鼎,很多Java应用几乎都集成或构建了一个定时任务调度系统,Quartz是一个定时任务调度框 ...
- 通过源码分析Java开源任务调度框架Quartz的主要流程
通过源码分析Java开源任务调度框架Quartz的主要流程 从使用效果.调用链路跟踪.E-R图.循环调度逻辑几个方面分析Quartz. github项目地址: https://github.com/t ...
- 开源调度框架Quartz最佳实践
开源调度框架Quartz最佳实践 Quartz是一个Java调度框架,当前的最新版本为2.2.1. 以Quartz 2.2.1版为例,Quartz最佳实践(用于生产系统)总结如下: 1.跳过更新检查Q ...
- java框架之SpringBoot(14)-任务
使用 maven 创建 SpringBoot 项目,引入 Web 场景启动器. 异步任务 1.编写异步服务类,注册到 IoC 容器: package zze.springboot.task.servi ...
- 任务调度开源框架Quartz概述
任务调度开源框架Quartz 几乎每个项目中都用到了自动任务处理功能.所以在任务调度的功能很常用,但是一个好的任务调度程序是一个颇具挑战性的工作.最近用到Quartz这个框架,感觉很好,所以进行学习. ...
- 企业级任务调度框架Quartz(2)-下载和安装Quartz
1.下载和安装 Quartz 根据资料上提供的网址http://www.opensymphony.com/quartz 我们可以下载到Quartz的最新版本1.6.4: 2.下载后包的说明 ...
- 企业级任务调度框架Quartz(1) --企业应用中的任务调度介绍
由于目前的工作内容为建行CLPM批处理业务的设计工作,所以很好的理解批处理所用的任务调度框架Quartz势在必行:为了能够更好的去服务于工作,也 为了提升自己,所以我学习了Quartz Job Sch ...
随机推荐
- Linux入门基础教程之Linux系统简介
Linux的历史: Multics项目开发不顺利,贝尔实验室退出该项目后,开发了Unix,商业化后价格昂贵.Dennis Ritchie和Ken Thompson开发了Unix内核以及C语言.Andr ...
- C# 获取Image图片格式
private void button1_Click_1(object sender, EventArgs e) { string path = Application.StartupPath; us ...
- Linux学习笔记(一):文件操作命令
命令 含义 cd / 切换到根目录 cd .. 上级目录 cd ./bin 到同级的bin目录中 cd bin 到同级的bin目录中 cd - usr文件夹 cd ~ 回到root用户的主文件夹 pw ...
- html5 返回当前地理位置的坐标点(经纬度)
BAIDU <!DOCTYPE html> <html> <body> <p id="demo">点击这个按钮,获得您的坐标:< ...
- 磁盘IO概念及优化入门知识
在数据库优化和存储规划过程中,总会提到IO的一些重要概念,在这里就详细记录一下,对这个概念的熟悉程度也决定了对数据库与存储优化的理解程度,以下这些概念并非权威文档,权威程度肯定就不能说了. 读/写IO ...
- python的redis简单使用
安装的Python版本 3.6.1 redis安装的2.8 安装redis模块 pip install redis 简单使用 redis-test.py import redis r=redis.Re ...
- win7 IIS7 发布网站遇到 HTTP 错误 500.19 由于权限不足
win7 IIS7 发布网站遇到 HTTP 错误 500.19 - Internal Server Error 一看是5xx错误,就知道是服务器的问题,网上查了下,原来是权限问题 解决方法 ...
- cmd.exe启动参数详解
https://blog.csdn.net/moonhillcity/article/details/53039763 各个系统中打开文件的命令 "windows系统: cmd " ...
- Java位运算加密
创建一个类,通过位运算中的”^"异或运算符把字符串与一个指定的值进行异或运算,从而改变字符串每个字符的值,这样就可以得到一个加密后的字符串.当把加密后的字符串作为程序输入内容,再与那个指定的 ...
- vue实现百度搜索下拉提示功能
这段代码用到vuejs和vue-resouece.实现对接智能提示接口,并通过上下键选择提示项,按enter进行搜索 <!DOCTYPE html> <html lang=" ...