网站首页 > 技术文章 正文
作者:青石路
cnblogs.com/youzhibing/p/10024558.html
java定时任务调度的实现方式
Timer
这个相信大家都有用过,我也用过,但用的不多;
特点是:简单易用,但由于所有任务都是由同一个线程来调度,因此所有任务都是串行执行的,同一时间只能有一个任务在执行,前一个任务的延迟或异常都将会影响到之后的任务;能实现简单的定时任务,稍微复杂点(或要求高一些)的定时任务却不好实现。
ScheduledExecutor
这个我相信大家也都用过,而且用的比Timer多;正是鉴于Timer的缺陷,Java 5推出了基于线程池设计的ScheduledExecutor;
特点:每一个被调度的任务都会由线程池中一个线程去执行,因此任务是并发执行的,相互之间不会受到干扰。需要注意的是,只有当任务的执行时间到来时,ScheduedExecutor 才会真正启动一个线程,其余时间 ScheduledExecutor 都是在轮询任务的状态。
虽然用ScheduledExecutor和Calendar能够实现复杂任务调度,但实现起来还是比较麻烦,对开发还是不够友善。
Spring Scheduler
spring对任务调度的实现支持,可以指定任务的执行时间,但对任务队列和线程池的管控较弱;一般集成于项目中,小任务很方便。
JCronTab
JCronTab则是一款完全按照crontab语法编写的java任务调度工具。
特点:
- 可指定任务的执行时间;
- 提供完全按照Unix的UNIX-POSIX crontab的格式来规定时间;
- 支持多种任务调度的持久化方法,包括普通文件、数据库以及 XML 文件进行持久化;
- JCronTab内置了发邮件功能,可以将任务执行结果方便地发送给需要被通知的人;
- 设计和部署是高性能并可扩展。
Quartz
本文主角,请往下看
当然还有XXL-JOB、Elastic-Job、Saturn等等
quartz相关概念
- Scheduler:调度器,进行任务调度;quartz的大脑
- Job:业务job,亦可称业务组件;定时任务的具体执行业务需要实现此接口,调度器会调用此接口的execute方法完成我们的定时业务
- JobDetail:用来定义业务Job的实例,我们可以称之为quartz job,很多时候我们谈到的job指的是JobDetail
- Trigger:触发器,用来定义一个指定的Job何时被执行
- JobBuilder:Job构建器,用来定义或创建JobDetail的实例;JobDetail限定了只能是Job的实例
- TriggerBuilder:触发器构建器,用来定义或创建触发器的实例
具体为什么要分这么细,大家可以去查阅下相关资料,你会发现很多东西。
工程实现
pom.xml
application.xml
这样,quartz就配置好了,应用里面直接用即可
JobController.java
JobServiceImpl.java
主要就是以上文件,详情请查看spring-boot-quartz
https://gitee.com/youzhibing/spring-boot-2.0.3/tree/master/spring-boot-quartz
工程里面数据源用的druid,springboot默认也会将该数据源应用到quartz,如果想给quartz单独配置数据源,可配合@QuartzDataSource来实现
最终效果如下
trigger状态
org.quartz.impl.jdbcjobstore.Constants中存放了一些列的常量,源代码如下
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package org.quartz.impl.jdbcjobstore;
/**
* <p>
* This interface can be implemented by any <code>{@link
* org.quartz.impl.jdbcjobstore.DriverDelegate}</code>
* class that needs to use the constants contained herein.
* </p>
*
* @author <a href="mailto:jeff@binaryfeed.org">Jeffrey Wescott</a>
* @author James House
*/
public interface Constants {
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constants.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// Table names
String TABLE_JOB_DETAILS = "JOB_DETAILS";
String TABLE_TRIGGERS = "TRIGGERS";
String TABLE_SIMPLE_TRIGGERS = "SIMPLE_TRIGGERS";
String TABLE_CRON_TRIGGERS = "CRON_TRIGGERS";
String TABLE_BLOB_TRIGGERS = "BLOB_TRIGGERS";
String TABLE_FIRED_TRIGGERS = "FIRED_TRIGGERS";
String TABLE_CALENDARS = "CALENDARS";
String TABLE_PAUSED_TRIGGERS = "PAUSED_TRIGGER_GRPS";
String TABLE_LOCKS = "LOCKS";
String TABLE_SCHEDULER_STATE = "SCHEDULER_STATE";
// TABLE_JOB_DETAILS columns names
String COL_SCHEDULER_NAME = "SCHED_NAME";
String COL_JOB_NAME = "JOB_NAME";
String COL_JOB_GROUP = "JOB_GROUP";
String COL_IS_DURABLE = "IS_DURABLE";
String COL_IS_VOLATILE = "IS_VOLATILE";
String COL_IS_NONCONCURRENT = "IS_NONCONCURRENT";
String COL_IS_UPDATE_DATA = "IS_UPDATE_DATA";
String COL_REQUESTS_RECOVERY = "REQUESTS_RECOVERY";
String COL_JOB_DATAMAP = "JOB_DATA";
String COL_JOB_CLASS = "JOB_CLASS_NAME";
String COL_DESCRIPTION = "DESCRIPTION";
// TABLE_TRIGGERS columns names
String COL_TRIGGER_NAME = "TRIGGER_NAME";
String COL_TRIGGER_GROUP = "TRIGGER_GROUP";
String COL_NEXT_FIRE_TIME = "NEXT_FIRE_TIME";
String COL_PREV_FIRE_TIME = "PREV_FIRE_TIME";
String COL_TRIGGER_STATE = "TRIGGER_STATE";
String COL_TRIGGER_TYPE = "TRIGGER_TYPE";
String COL_START_TIME = "START_TIME";
String COL_END_TIME = "END_TIME";
String COL_PRIORITY = "PRIORITY";
String COL_MISFIRE_INSTRUCTION = "MISFIRE_INSTR";
String ALIAS_COL_NEXT_FIRE_TIME = "ALIAS_NXT_FR_TM";
// TABLE_SIMPLE_TRIGGERS columns names
String COL_REPEAT_COUNT = "REPEAT_COUNT";
String COL_REPEAT_INTERVAL = "REPEAT_INTERVAL";
String COL_TIMES_TRIGGERED = "TIMES_TRIGGERED";
// TABLE_CRON_TRIGGERS columns names
String COL_CRON_EXPRESSION = "CRON_EXPRESSION";
// TABLE_BLOB_TRIGGERS columns names
String COL_BLOB = "BLOB_DATA";
String COL_TIME_ZONE_ID = "TIME_ZONE_ID";
// TABLE_FIRED_TRIGGERS columns names
String COL_INSTANCE_NAME = "INSTANCE_NAME";
String COL_FIRED_TIME = "FIRED_TIME";
String COL_SCHED_TIME = "SCHED_TIME";
String COL_ENTRY_ID = "ENTRY_ID";
String COL_ENTRY_STATE = "STATE";
// TABLE_CALENDARS columns names
String COL_CALENDAR_NAME = "CALENDAR_NAME";
String COL_CALENDAR = "CALENDAR";
// TABLE_LOCKS columns names
String COL_LOCK_NAME = "LOCK_NAME";
// TABLE_LOCKS columns names
String COL_LAST_CHECKIN_TIME = "LAST_CHECKIN_TIME";
String COL_CHECKIN_INTERVAL = "CHECKIN_INTERVAL";
// MISC CONSTANTS
String DEFAULT_TABLE_PREFIX = "QRTZ_";
// STATES
String STATE_WAITING = "WAITING";
String STATE_ACQUIRED = "ACQUIRED";
String STATE_EXECUTING = "EXECUTING";
String STATE_COMPLETE = "COMPLETE";
String STATE_BLOCKED = "BLOCKED";
String STATE_ERROR = "ERROR";
String STATE_PAUSED = "PAUSED";
String STATE_PAUSED_BLOCKED = "PAUSED_BLOCKED";
String STATE_DELETED = "DELETED";
/**
* @deprecated Whether a trigger has misfired is no longer a state, but
* rather now identified dynamically by whether the trigger's next fire
* time is more than the misfire threshold time in the past.
*/
String STATE_MISFIRED = "MISFIRED";
String ALL_GROUPS_PAUSED = "_$_ALL_GROUPS_PAUSED_$_";
// TRIGGER TYPES
/** Simple Trigger type. */
String TTYPE_SIMPLE = "SIMPLE";
/** Cron Trigger type. */
String TTYPE_CRON = "CRON";
/** Calendar Interval Trigger type. */
String TTYPE_CAL_INT = "CAL_INT";
/** Daily Time Interval Trigger type. */
String TTYPE_DAILY_TIME_INT = "DAILY_I";
/** A general blob Trigger type. */
String TTYPE_BLOB = "BLOB";
}
// EOF
里面有quartz的表名、各个表包含的列名、trigger状态、trigger类型等内容。
状态包括
- WAITING:等待中
- ACQUIRED:将触发,此时还未到trigger真正的触发时刻
- EXECUTING:触发,亦可理解成执行中,trigger真正的触发时刻
- COMPLETE:完成,不再触发
- BLOCKED:受阻,不允许并发执行job时会出现(@DisallowConcurrentExecution)
- ERROR:出错
- PAUSED:暂停中
- PAUSED_BLOCKED:暂停受阻,不允许并发执行job时会出现(@DisallowConcurrentExecution)
- DELETED:已删除
- MISFIRED:触发失败,已弃用,有另外的替代方式
状态变化流程图如下所示
trigger的初始状态是WAITING,处于WAITING状态的trigger等待被触发。调度线程会不停地扫triggers表,根据NEXT_FIRE_TIME提前拉取即将触发的trigger,如果这个trigger被该调度线程拉取到,它的状态就会变为ACQUIRED。
因为是提前拉取trigger,并未到达trigger真正的触发时刻,所以调度线程会等到真正触发的时刻,再将trigger状态由ACQUIRED改为EXECUTING。如果这个trigger不再执行,就将状态改为COMPLETE,否则为WAITING,开始新的周期。如果这个周期中的任何环节抛出异常,trigger的状态会变成ERROR。如果手动暂停这个trigger,状态会变成PAUSED。
总结
Quartz作为一个开源的作业调度框架,提供了巨大的灵活性而不牺牲简单性。我们能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征,如:数据库、集群、插件、JavaMail支持,EJB作业预构建,支持cron-like表达式等等;
springboot集成quartz非常简单,最简单的情况下只需要引入依赖我们就可以享受quartz提供的功能,springboot默认会帮我们配置好quartz;当然我们也可以自定义配置来实现quartz的定制;
猜你喜欢
- 2024-11-20 抖音 Android 性能优化系列:启动优化实践
- 2024-11-20 Restic设计原理
- 2024-11-20 前端必读榜——如何在JavaScript中用SpreadJS导入/导出Excel文件
- 2024-11-20 Axios API详细介绍及使用
- 2024-11-20 镜像仓库registry命令行启动,垃圾回收和服务监听我全都要
- 2024-11-20 ElasticSearch知识day04
- 2024-11-20 详解Oracle 11g如何快速定位到lobsegment、lobindex对应的表
- 2024-11-20 前端必读:如何在 JavaScript 中使用SpreadJS导入和导出 Excel 文件
- 2024-11-20 前端文件下载的几种方式
- 2024-11-20 JavaScript奇淫技巧:20行代码,实现屏幕录像
- 标签列表
-
- content-disposition (47)
- nth-child (56)
- math.pow (44)
- 原型和原型链 (63)
- canvas mdn (36)
- css @media (49)
- promise mdn (39)
- readasdataurl (52)
- if-modified-since (49)
- css ::after (50)
- border-image-slice (40)
- flex mdn (37)
- .join (41)
- function.apply (60)
- input type number (64)
- weakmap (62)
- js arguments (45)
- js delete方法 (61)
- blob type (44)
- math.max.apply (51)
- js (44)
- firefox 3 (47)
- cssbox-sizing (52)
- js删除 (49)
- js for continue (56)
- 最新留言
-