时间:2021-05-02
quartz集群能力:
以上是个人的一些见解,下面开始本文的正文,本文主要介绍了关于spring boot + quartz集群搭建的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
步骤如下:
spring boot bean配置:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 @configuration public class quartzconfig { @value("${quartz.scheduler.instancename}") private string quartzinstancename; @value("${org.quartz.datasource.myds.driver}") private string mydsdriver; @value("${org.quartz.datasource.myds.url}") private string mydsurl; @value("${org.quartz.datasource.myds.user}") private string mydsuser; @value("${org.quartz.datasource.myds.password}") private string mydspassword; @value("${org.quartz.datasource.myds.maxconnections}") private string mydsmaxconnections; /** * 设置属性 * @return * @throws ioexception */ private properties quartzproperties() throws ioexception { properties prop = new properties(); prop.put("quartz.scheduler.instancename", quartzinstancename); prop.put("org.quartz.scheduler.instanceid", "auto"); prop.put("org.quartz.scheduler.skipupdatecheck", "true"); prop.put("org.quartz.scheduler.jmx.export", "true"); prop.put("org.quartz.jobstore.class", "org.quartz.impl.jdbcjobstore.jobstoretx"); prop.put("org.quartz.jobstore.driverdelegateclass", "org.quartz.impl.jdbcjobstore.stdjdbcdelegate"); prop.put("org.quartz.jobstore.datasource", "quartzdatasource"); prop.put("org.quartz.jobstore.tableprefix", "qrtz_"); prop.put("org.quartz.jobstore.isclustered", "true"); prop.put("org.quartz.jobstore.clustercheckininterval", "20000"); prop.put("org.quartz.jobstore.datasource", "myds"); prop.put("org.quartz.jobstore.maxmisfirestohandleatatime", "1"); prop.put("org.quartz.jobstore.misfirethreshold", "120000"); prop.put("org.quartz.jobstore.txisolationlevelserializable", "true"); prop.put("org.quartz.jobstore.selectwithlocksql", "select * from {0}locks where lock_name = ? for update"); prop.put("org.quartz.threadpool.class", "org.quartz.simpl.simplethreadpool"); prop.put("org.quartz.threadpool.threadcount", "10"); prop.put("org.quartz.threadpool.threadpriority", "5"); prop.put("org.quartz.threadpool.threadsinheritcontextclassloaderofinitializingthread", "true"); prop.put("org.quartz.datasource.myds.driver", mydsdriver); prop.put("org.quartz.datasource.myds.url", mydsurl); prop.put("org.quartz.datasource.myds.user", mydsuser); prop.put("org.quartz.datasource.myds.password", mydspassword); system.out.println("mydsmaxconnections:" + mydsmaxconnections); prop.put("org.quartz.datasource.myds.maxconnections", mydsmaxconnections); prop.put("org.quartz.plugin.trigghistory.class", "org.quartz.plugins.history.loggingjobhistoryplugin"); prop.put("org.quartz.plugin.shutdownhook.class", "org.quartz.plugins.management.shutdownhookplugin"); prop.put("org.quartz.plugin.shutdownhook.cleanshutdown", "true"); return prop; } @bean public schedulerfactorybean schedulerfactorybean(@qualifier("dialogjobtrigger") trigger cronjobtrigger) throws ioexception { schedulerfactorybean factory = new schedulerfactorybean(); // this allows to update triggers in db when updating settings in config file: //用于quartz集群,quartzscheduler 启动时更新己存在的job,这样就不用每次修改targetobject后删除qrtz_job_details表对应记录了 factory.setoverwriteexistingjobs(true); //用于quartz集群,加载quartz数据源 //factory.setdatasource(datasource); //quartzscheduler 延时启动,应用启动完10秒后 quartzscheduler 再启动 factory.setstartupdelay(10); //用于quartz集群,加载quartz数据源配置 factory.setquartzproperties(quartzproperties()); factory.setautostartup(true); factory.setapplicationcontextschedulercontextkey("applicationcontext"); //注册触发器 factory.settriggers(cronjobtrigger);<br data-filtered="filtered"> //直接使用配置文件 // factory.setconfiglocation(new filesystemresource(this.getclass().getresource("/quartz.properties").getpath())); return factory; } /** * 加载job * @return */ @bean public jobdetailfactorybean updatedialogstatusjobdetail() { return createjobdetail(invokingjobdetaildetailfactory.class, "updatedialogstatusgroup", "dialogjob"); } /** * 加载触发器 * @param jobdetail * @return */ @bean(name = "dialogjobtrigger") public crontriggerfactorybean dialogstatusjobtrigger(@qualifier("updatedialogstatusjobdetail") jobdetail jobdetail) { return dialogstatustrigger(jobdetail, "0 0 0/1 * * ?"); } /** * 创建job工厂 * @param jobclass * @param groupname * @param targetobject * @return */ private static jobdetailfactorybean createjobdetail(class<?> jobclass, string groupname, string targetobject) { jobdetailfactorybean factorybean = new jobdetailfactorybean(); factorybean.setjobclass(jobclass); factorybean.setdurability(true); factorybean.setrequestsrecovery(true); factorybean.setgroup(groupname); map<string, string> map = new hashmap<>(); map.put("targetobject", targetobject); map.put("targetmethod", "execute"); factorybean.setjobdataasmap(map); return factorybean; } /** * 创建触发器工厂 * @param jobdetail * @param cronexpression * @return */ private static crontriggerfactorybean dialogstatustrigger(jobdetail jobdetail, string cronexpression) { crontriggerfactorybean factorybean = new crontriggerfactorybean(); factorybean.setjobdetail(jobdetail); factorybean.setcronexpression (cronexpression); return factorybean; } }invokingjobdetaildetailfactory对象:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public class invokingjobdetaildetailfactory extends quartzjobbean{ // 计划任务所在类 private string targetobject; // 具体需要执行的计划任务 private string targetmethod; private applicationcontext ctx; @override protected void executeinternal(jobexecutioncontext context) throws jobexecutionexception { try { object otargetobject = ctx.getbean(targetobject); method m = null; try { m = otargetobject.getclass().getmethod(targetmethod); m.invoke(otargetobject); } catch (securityexception e) { e.printstacktrace(); } catch (nosuchmethodexception e) { e.printstacktrace(); } } catch (exception e) { throw new jobexecutionexception(e); } } public void setapplicationcontext(applicationcontext applicationcontext) { this.ctx = applicationcontext; } public void settargetobject(string targetobject) { this.targetobject = targetobject; } public void settargetmethod(string targetmethod) { this.targetmethod = targetmethod; } }备注:set方法不能少,setapplicationcontext中的applicationcontext与factory.setapplicationcontextschedulercontextkey("applicationcontext")填入的值有关,其原理由invokingjobdetaildetailfactory父类中的beanwrapper实现。
sql脚本:--
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 <em id="__mcedel">-- 表的结构 `qrtz_blob_triggers` -- create table if not exists `qrtz_blob_triggers` ( `sched_name` varchar(120) not null, `trigger_name` varchar(120) not null, `trigger_group` varchar(120) not null, `blob_data` blob ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_calendars` -- create table if not exists `qrtz_calendars` ( `sched_name` varchar(120) not null, `calendar_name` varchar(120) not null, `calendar` blob not null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_cron_triggers` -- create table if not exists `qrtz_cron_triggers` ( `sched_name` varchar(120) not null, `trigger_name` varchar(120) not null, `trigger_group` varchar(120) not null, `cron_expression` varchar(120) not null, `time_zone_id` varchar(80) default null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_fired_triggers` -- create table if not exists `qrtz_fired_triggers` ( `sched_name` varchar(120) not null, `entry_id` varchar(95) not null, `trigger_name` varchar(120) not null, `trigger_group` varchar(120) not null, `instance_name` varchar(120) not null, `fired_time` bigint(13) not null, `sched_time` bigint(13) not null, `priority` int(11) not null, `state` varchar(16) not null, `job_name` varchar(120) default null, `job_group` varchar(120) default null, `is_nonconcurrent` varchar(1) default null, `requests_recovery` varchar(1) default null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_job_details` -- create table if not exists `qrtz_job_details` ( `sched_name` varchar(120) not null, `job_name` varchar(120) not null, `job_group` varchar(120) not null, `description` varchar(250) default null, `job_class_name` varchar(250) not null, `is_durable` varchar(1) not null, `is_nonconcurrent` varchar(1) not null, `is_update_data` varchar(1) not null, `requests_recovery` varchar(1) not null, `job_data` blob ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_locks` -- create table if not exists `qrtz_locks` ( `sched_name` varchar(120) not null, `lock_name` varchar(40) not null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_paused_trigger_grps` -- create table if not exists `qrtz_paused_trigger_grps` ( `sched_name` varchar(120) not null, `trigger_group` varchar(120) not null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_scheduler_state` -- create table if not exists `qrtz_scheduler_state` ( `sched_name` varchar(120) not null, `instance_name` varchar(120) not null, `last_checkin_time` bigint(13) not null, `checkin_interval` bigint(13) not null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_simple_triggers` -- create table if not exists `qrtz_simple_triggers` ( `sched_name` varchar(120) not null, `trigger_name` varchar(120) not null, `trigger_group` varchar(120) not null, `repeat_count` bigint(7) not null, `repeat_interval` bigint(12) not null, `times_triggered` bigint(10) not null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_simprop_triggers` -- create table if not exists `qrtz_simprop_triggers` ( `sched_name` varchar(120) not null, `trigger_name` varchar(120) not null, `trigger_group` varchar(120) not null, `str_prop_1` varchar(512) default null, `str_prop_2` varchar(512) default null, `str_prop_3` varchar(512) 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) default null, `bool_prop_2` varchar(1) default null ) engine=innodb default charset=utf8mb4; -- -------------------------------------------------------- -- -- 表的结构 `qrtz_triggers` -- create table if not exists `qrtz_triggers` ( `sched_name` varchar(120) not null, `trigger_name` varchar(120) not null, `trigger_group` varchar(120) not null, `job_name` varchar(120) not null, `job_group` varchar(120) not null, `description` varchar(250) 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) not null, `trigger_type` varchar(8) not null, `start_time` bigint(13) not null, `end_time` bigint(13) default null, `calendar_name` varchar(200) default null, `misfire_instr` smallint(2) default null, `job_data` blob ) engine=innodb default charset=utf8mb4; -- -- indexes for dumped tables -- -- -- indexes for table `qrtz_blob_triggers` -- alter table `qrtz_blob_triggers` add primary key (`sched_name`,`trigger_name`,`trigger_group`), add key `sched_name` (`sched_name`,`trigger_name`,`trigger_group`); -- -- indexes for table `qrtz_calendars` -- alter table `qrtz_calendars` add primary key (`sched_name`,`calendar_name`); -- -- indexes for table `qrtz_cron_triggers` -- alter table `qrtz_cron_triggers` add primary key (`sched_name`,`trigger_name`,`trigger_group`); -- -- indexes for table `qrtz_fired_triggers` -- alter table `qrtz_fired_triggers` add primary key (`sched_name`,`entry_id`), add key `idx_qrtz_ft_trig_inst_name` (`sched_name`,`instance_name`), add key `idx_qrtz_ft_inst_job_req_rcvry` (`sched_name`,`instance_name`,`requests_recovery`), add key `idx_qrtz_ft_j_g` (`sched_name`,`job_name`,`job_group`), add key `idx_qrtz_ft_jg` (`sched_name`,`job_group`), add key `idx_qrtz_ft_t_g` (`sched_name`,`trigger_name`,`trigger_group`), add key `idx_qrtz_ft_tg` (`sched_name`,`trigger_group`); -- -- indexes for table `qrtz_job_details` -- alter table `qrtz_job_details` add primary key (`sched_name`,`job_name`,`job_group`), add key `idx_qrtz_j_req_recovery` (`sched_name`,`requests_recovery`), add key `idx_qrtz_j_grp` (`sched_name`,`job_group`); -- -- indexes for table `qrtz_locks` -- alter table `qrtz_locks` add primary key (`sched_name`,`lock_name`); -- -- indexes for table `qrtz_paused_trigger_grps` -- alter table `qrtz_paused_trigger_grps` add primary key (`sched_name`,`trigger_group`); -- -- indexes for table `qrtz_scheduler_state` -- alter table `qrtz_scheduler_state` add primary key (`sched_name`,`instance_name`); -- -- indexes for table `qrtz_simple_triggers` -- alter table `qrtz_simple_triggers` add primary key (`sched_name`,`trigger_name`,`trigger_group`); -- -- indexes for table `qrtz_simprop_triggers` -- alter table `qrtz_simprop_triggers` add primary key (`sched_name`,`trigger_name`,`trigger_group`); -- -- indexes for table `qrtz_triggers` -- alter table `qrtz_triggers` add primary key (`sched_name`,`trigger_name`,`trigger_group`), add key `idx_qrtz_t_j` (`sched_name`,`job_name`,`job_group`), add key `idx_qrtz_t_jg` (`sched_name`,`job_group`), add key `idx_qrtz_t_c` (`sched_name`,`calendar_name`(191)), add key `idx_qrtz_t_g` (`sched_name`,`trigger_group`), add key `idx_qrtz_t_state` (`sched_name`,`trigger_state`), add key `idx_qrtz_t_n_state` (`sched_name`,`trigger_name`,`trigger_group`,`trigger_state`), add key `idx_qrtz_t_n_g_state` (`sched_name`,`trigger_group`,`trigger_state`), add key `idx_qrtz_t_next_fire_time` (`sched_name`,`next_fire_time`), add key `idx_qrtz_t_nft_st` (`sched_name`,`trigger_state`,`next_fire_time`), add key `idx_qrtz_t_nft_misfire` (`sched_name`,`misfire_instr`,`next_fire_time`), add key `idx_qrtz_t_nft_st_misfire` (`sched_name`,`misfire_instr`,`next_fire_time`,`trigger_state`), add key `idx_qrtz_t_nft_st_misfire_grp` (`sched_name`,`misfire_instr`,`next_fire_time`,`trigger_group`,`trigger_state`); -- -- 限制导出的表 -- -- -- 限制表 `qrtz_blob_triggers` -- alter table `qrtz_blob_triggers` add constraint `qrtz_blob_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`); -- -- 限制表 `qrtz_cron_triggers` -- alter table `qrtz_cron_triggers` add constraint `qrtz_cron_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`); -- -- 限制表 `qrtz_simple_triggers` -- alter table `qrtz_simple_triggers` add constraint `qrtz_simple_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`); -- -- 限制表 `qrtz_simprop_triggers` -- alter table `qrtz_simprop_triggers` add constraint `qrtz_simprop_triggers_ibfk_1` foreign key (`sched_name`, `trigger_name`, `trigger_group`) references `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`); -- -- 限制表 `qrtz_triggers` -- alter table `qrtz_triggers` add constraint `qrtz_triggers_ibfk_1` foreign key (`sched_声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Spring中配置Quartz的过程:1.导入JAR包quartz需要的JAR包,已经包含在spring中,位置在spring解压后目录的\lib\quartz
SpringBoot集成activiti基础环境搭建添加依赖org.activitiactiviti-spring-boot-starter-basic6.0.
一、版本说明spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz2.x,不然会出错。原因:spring对于quartz
1.什么是spring-boot-devtoolsspring-boot-devtools是spring-boot项目开发时的一个热部署工具,安装了spring
一)spring-boot-starter命名规则自动配置模块命名规则:xxx-spring-boot,如:aspectlog-spring-boot启动器命名