quartz-cluster
扩展quartz动态分布式定时任务 常规使用quartz需要每一个任务都实现org.quartz.Job接口,每次添加,删除或者修改任务配置都需要停机修改代码再重新部署,即使quartz提供API可以运行时删除或修改,但是机器重启后,只要代码不修改,还是会恢复成初始的配置。 所以针对以上的问题,最好是能做成动态可配置的才能更方便地管理我们的定时任务。最好的结果是可以直接使用spring管理的任意Bean对象,通过Bean的名称,指定的方法名,就能按照一定的调度策略执行定时任务。
使用方式:
1.先导入依赖:
<dependency>
<groupId>com.github.zw201913</groupId>
<artifactId>quartz-cluster</artifactId>
<version>1.0.5.RELEASE</version>
</dependency>
需要至少JDK8版本
2.扫描指定包名
@ComponentScan("com.github.zw201913.quartzcluster")
@EnableQuartzCluster
3.添加quartz配置 在resources里面添加quartz.properties
spring:
quartz:
#相关属性配置
properties:
org:
quartz:
scheduler:
instanceName: clusteredScheduler
instanceId: AUTO
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_
isClustered: true
clusterCheckinInterval: 10000
useProperties: false
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 10
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
#数据库方式
job-store-type: jdbc
4.创建数据库表单
-- ----------------------------
-- Table structure for QRTZ_JOB_DETAILS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_JOB_DETAILS`;
CREATE TABLE `QRTZ_JOB_DETAILS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`JOB_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`JOB_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`DESCRIPTION` varchar(250) COLLATE utf8mb4_bin DEFAULT NULL,
`JOB_CLASS_NAME` varchar(250) COLLATE utf8mb4_bin NOT NULL,
`IS_DURABLE` varchar(1) COLLATE utf8mb4_bin NOT NULL,
`IS_NONCONCURRENT` varchar(1) COLLATE utf8mb4_bin NOT NULL,
`IS_UPDATE_DATA` varchar(1) COLLATE utf8mb4_bin NOT NULL,
`REQUESTS_RECOVERY` varchar(1) COLLATE utf8mb4_bin NOT NULL,
`JOB_DATA` blob,
PRIMARY KEY (`SCHED_NAME`,`JOB_NAME`,`JOB_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_TRIGGERS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_TRIGGERS`;
CREATE TABLE `QRTZ_TRIGGERS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`JOB_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`JOB_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`DESCRIPTION` varchar(250) COLLATE utf8mb4_bin DEFAULT NULL,
`NEXT_FIRE_TIME` bigint(13) DEFAULT NULL,
`PREV_FIRE_TIME` bigint(13) DEFAULT NULL,
`PRIORITY` int(11) DEFAULT NULL,
`TRIGGER_STATE` varchar(16) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_TYPE` varchar(8) COLLATE utf8mb4_bin NOT NULL,
`START_TIME` bigint(13) NOT NULL,
`END_TIME` bigint(13) DEFAULT NULL,
`CALENDAR_NAME` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
`MISFIRE_INSTR` smallint(2) DEFAULT NULL,
`JOB_DATA` blob,
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
KEY `SCHED_NAME` (`SCHED_NAME`,`JOB_NAME`,`JOB_GROUP`),
CONSTRAINT `QRTZ_TRIGGERS_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `JOB_NAME`, `JOB_GROUP`) REFERENCES `QRTZ_JOB_DETAILS` (`SCHED_NAME`, `JOB_NAME`, `JOB_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
DROP TABLE IF EXISTS `QRTZ_BLOB_TRIGGERS`;
CREATE TABLE `QRTZ_BLOB_TRIGGERS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`BLOB_DATA` blob,
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `QRTZ_BLOB_TRIGGERS_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_CALENDARS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_CALENDARS`;
CREATE TABLE `QRTZ_CALENDARS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`CALENDAR_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`CALENDAR` blob NOT NULL,
PRIMARY KEY (`SCHED_NAME`,`CALENDAR_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_CRON_TRIGGERS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_CRON_TRIGGERS`;
CREATE TABLE `QRTZ_CRON_TRIGGERS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`CRON_EXPRESSION` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`TIME_ZONE_ID` varchar(80) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `QRTZ_CRON_TRIGGERS_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_FIRED_TRIGGERS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_FIRED_TRIGGERS`;
CREATE TABLE `QRTZ_FIRED_TRIGGERS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`ENTRY_ID` varchar(95) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`INSTANCE_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`FIRED_TIME` bigint(13) NOT NULL,
`SCHED_TIME` bigint(13) NOT NULL,
`PRIORITY` int(11) NOT NULL,
`STATE` varchar(16) COLLATE utf8mb4_bin NOT NULL,
`JOB_NAME` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
`JOB_GROUP` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
`IS_NONCONCURRENT` varchar(1) COLLATE utf8mb4_bin DEFAULT NULL,
`REQUESTS_RECOVERY` varchar(1) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`SCHED_NAME`,`ENTRY_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_LOCKS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_LOCKS`;
CREATE TABLE `QRTZ_LOCKS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`LOCK_NAME` varchar(40) COLLATE utf8mb4_bin NOT NULL,
PRIMARY KEY (`SCHED_NAME`,`LOCK_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_PAUSED_TRIGGER_GRPS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_PAUSED_TRIGGER_GRPS`;
CREATE TABLE `QRTZ_PAUSED_TRIGGER_GRPS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_SCHEDULER_STATE
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_SCHEDULER_STATE`;
CREATE TABLE `QRTZ_SCHEDULER_STATE` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`INSTANCE_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`LAST_CHECKIN_TIME` bigint(13) NOT NULL,
`CHECKIN_INTERVAL` bigint(13) NOT NULL,
PRIMARY KEY (`SCHED_NAME`,`INSTANCE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_SIMPLE_TRIGGERS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_SIMPLE_TRIGGERS`;
CREATE TABLE `QRTZ_SIMPLE_TRIGGERS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`REPEAT_COUNT` bigint(7) NOT NULL,
`REPEAT_INTERVAL` bigint(12) NOT NULL,
`TIMES_TRIGGERED` bigint(10) NOT NULL,
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `QRTZ_SIMPLE_TRIGGERS_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- ----------------------------
-- Table structure for QRTZ_SIMPROP_TRIGGERS
-- ----------------------------
DROP TABLE IF EXISTS `QRTZ_SIMPROP_TRIGGERS`;
CREATE TABLE `QRTZ_SIMPROP_TRIGGERS` (
`SCHED_NAME` varchar(120) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_NAME` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`TRIGGER_GROUP` varchar(200) COLLATE utf8mb4_bin NOT NULL,
`STR_PROP_1` varchar(512) COLLATE utf8mb4_bin DEFAULT NULL,
`STR_PROP_2` varchar(512) COLLATE utf8mb4_bin DEFAULT NULL,
`STR_PROP_3` varchar(512) COLLATE utf8mb4_bin DEFAULT NULL,
`INT_PROP_1` int(11) DEFAULT NULL,
`INT_PROP_2` int(11) DEFAULT NULL,
`LONG_PROP_1` bigint(20) DEFAULT NULL,
`LONG_PROP_2` bigint(20) DEFAULT NULL,
`DEC_PROP_1` decimal(13,4) DEFAULT NULL,
`DEC_PROP_2` decimal(13,4) DEFAULT NULL,
`BOOL_PROP_1` varchar(1) COLLATE utf8mb4_bin DEFAULT NULL,
`BOOL_PROP_2` varchar(1) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`SCHED_NAME`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
CONSTRAINT `QRTZ_SIMPROP_TRIGGERS_ibfk_1` FOREIGN KEY (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `QRTZ_TRIGGERS` (`SCHED_NAME`, `TRIGGER_NAME`, `TRIGGER_GROUP`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
5.自定义Controller使用com.github.zw201913.quartzcluster.service.IScheduleService 示例:
@RestController
@RequestMapping("/job")
@Slf4j
public class ScheduleController {
@Autowired private IScheduleService scheduleService;
/**
* 添加cron定时任务
*
* @param cronJobDefinition
*/
@PostMapping("/addCronJob")
public void schedule(@RequestPart("cronJobDefinition") CronJobDefinition cronJobDefinition) {
scheduleService.schedule(cronJobDefinition);
}
/**
* 添加simple定时任务
*
* @param simpleJobDefinition
*/
@PostMapping("/addSimpleJob")
public void schedule(
@RequestPart("simpleJobDefinition") SimpleJobDefinition simpleJobDefinition) {
scheduleService.schedule(simpleJobDefinition);
}
}
6.如果有需要的,可以自定义SchedulerListener,JobListener和TriggerListener,并且交由spring来管理
@Service
public class ScheduleListenerService extends SchedulerListenerSupport {
}
@Service
public class JobListenerService extends JobListenerSupport {
}
@Service
public class TriggerListenerService extends TriggerListenerSupport {
}