概述
雖然單個quartz實例能給予你很好的job調度能力,但它不能滿足典型的企業需求,如可伸縮性、高可靠性滿足。假如你需要故障轉移的能力并能運行日益增多的 job,quartz集群勢必成為你應用的一部分了。使用 quartz 的集群能力可以更好的支持你的業務需求,并且即使是其中一臺機器在最糟的時間崩潰了也能確保所有的 job 得到執行。
quartz 中集群如何工作
一個 quartz 集群中的每個節點是一個獨立的 quartz 應用,它又管理著其他的節點。意思是你必須對每個節點分別啟動或停止。不像許多應用服務器的集群,獨立的 quartz 節點并不與另一其的節點或是管理節點通信。quartz 應用是通過數據庫表來感知到另一應用的。
圖:表示了每個節點直接與數據庫通信,若離開數據庫將對其他節點一無所知
創建quartz數據庫表
因為quartz 集群依賴于數據庫,所以必須首先創建quartz數據庫表。quartz 包括了所有被支持的數據庫平臺的 sql 腳本。在 <quartz_home>/docs/dbtables 目錄下找到那些 sql 腳本,這里的 <quartz_home> 是解壓 quartz 分發包后的目錄。
這里采用的quartz 2.2.3版本,總共11張表,不同版本,表個數可能不同。數據庫為mysql,用tables_mysql_innodb.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
|
# # in your quartz properties file, you'll need to set # org.quartz.jobstore.driverdelegateclass = org.quartz.impl.jdbcjobstore.stdjdbcdelegate # # # by: ron cordell - roncordell # i didn 't see this anywhere, so i thought i' d post it here. this is the script from quartz to create the tables in a mysql database, modified to use innodb instead of myisam. drop table if exists qrtz_fired_triggers; drop table if exists qrtz_paused_trigger_grps; drop table if exists qrtz_scheduler_state; drop table if exists qrtz_locks; drop table if exists qrtz_simple_triggers; drop table if exists qrtz_simprop_triggers; drop table if exists qrtz_cron_triggers; drop table if exists qrtz_blob_triggers; drop table if exists qrtz_triggers; drop table if exists qrtz_job_details; drop table if exists qrtz_calendars; create table qrtz_job_details( sched_name varchar( 120 ) not null , job_name varchar( 200 ) not null , job_group varchar( 200 ) not null , description varchar( 250 ) 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 null , primary key (sched_name,job_name,job_group)) engine=innodb; create table qrtz_triggers ( sched_name varchar( 120 ) not null , trigger_name varchar( 200 ) not null , trigger_group varchar( 200 ) not null , job_name varchar( 200 ) not null , job_group varchar( 200 ) not null , description varchar( 250 ) null , next_fire_time bigint( 13 ) null , prev_fire_time bigint( 13 ) null , priority integer null , trigger_state varchar( 16 ) not null , trigger_type varchar( 8 ) not null , start_time bigint( 13 ) not null , end_time bigint( 13 ) null , calendar_name varchar( 200 ) null , misfire_instr smallint( 2 ) null , job_data blob null , primary key (sched_name,trigger_name,trigger_group), foreign key (sched_name,job_name,job_group) references qrtz_job_details(sched_name,job_name,job_group)) engine=innodb; create table qrtz_simple_triggers ( sched_name varchar( 120 ) not null , trigger_name varchar( 200 ) not null , trigger_group varchar( 200 ) 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), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group)) engine=innodb; create table qrtz_cron_triggers ( sched_name varchar( 120 ) not null , trigger_name varchar( 200 ) not null , trigger_group varchar( 200 ) not null , cron_expression varchar( 120 ) not null , time_zone_id varchar( 80 ), primary key (sched_name,trigger_name,trigger_group), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group)) engine=innodb; create table qrtz_simprop_triggers ( sched_name varchar( 120 ) not null , trigger_name varchar( 200 ) not null , trigger_group varchar( 200 ) not null , str_prop_1 varchar( 512 ) null , str_prop_2 varchar( 512 ) null , str_prop_3 varchar( 512 ) null , int_prop_1 int null , int_prop_2 int null , long_prop_1 bigint null , long_prop_2 bigint null , dec_prop_1 numeric( 13 , 4 ) null , dec_prop_2 numeric( 13 , 4 ) null , bool_prop_1 varchar( 1 ) null , bool_prop_2 varchar( 1 ) null , primary key (sched_name,trigger_name,trigger_group), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group)) engine=innodb; create table qrtz_blob_triggers ( sched_name varchar( 120 ) not null , trigger_name varchar( 200 ) not null , trigger_group varchar( 200 ) not null , blob_data blob null , primary key (sched_name,trigger_name,trigger_group), index (sched_name,trigger_name, trigger_group), foreign key (sched_name,trigger_name,trigger_group) references qrtz_triggers(sched_name,trigger_name,trigger_group)) engine=innodb; create table qrtz_calendars ( sched_name varchar( 120 ) not null , calendar_name varchar( 200 ) not null , calendar blob not null , primary key (sched_name,calendar_name)) engine=innodb; create table qrtz_paused_trigger_grps ( sched_name varchar( 120 ) not null , trigger_group varchar( 200 ) not null , primary key (sched_name,trigger_group)) engine=innodb; create table qrtz_fired_triggers ( sched_name varchar( 120 ) not null , entry_id varchar( 95 ) not null , trigger_name varchar( 200 ) not null , trigger_group varchar( 200 ) not null , instance_name varchar( 200 ) not null , fired_time bigint( 13 ) not null , sched_time bigint( 13 ) not null , priority integer not null , state varchar( 16 ) not null , job_name varchar( 200 ) null , job_group varchar( 200 ) null , is_nonconcurrent varchar( 1 ) null , requests_recovery varchar( 1 ) null , primary key (sched_name,entry_id)) engine=innodb; create table qrtz_scheduler_state ( sched_name varchar( 120 ) not null , instance_name varchar( 200 ) not null , last_checkin_time bigint( 13 ) not null , checkin_interval bigint( 13 ) not null , primary key (sched_name,instance_name)) engine=innodb; create table qrtz_locks ( sched_name varchar( 120 ) not null , lock_name varchar( 40 ) not null , primary key (sched_name,lock_name)) engine=innodb; create index idx_qrtz_j_req_recovery on qrtz_job_details(sched_name,requests_recovery); create index idx_qrtz_j_grp on qrtz_job_details(sched_name,job_group); create index idx_qrtz_t_j on qrtz_triggers(sched_name,job_name,job_group); create index idx_qrtz_t_jg on qrtz_triggers(sched_name,job_group); create index idx_qrtz_t_c on qrtz_triggers(sched_name,calendar_name); create index idx_qrtz_t_g on qrtz_triggers(sched_name,trigger_group); create index idx_qrtz_t_state on qrtz_triggers(sched_name,trigger_state); create index idx_qrtz_t_n_state on qrtz_triggers(sched_name,trigger_name,trigger_group,trigger_state); create index idx_qrtz_t_n_g_state on qrtz_triggers(sched_name,trigger_group,trigger_state); create index idx_qrtz_t_next_fire_time on qrtz_triggers(sched_name,next_fire_time); create index idx_qrtz_t_nft_st on qrtz_triggers(sched_name,trigger_state,next_fire_time); create index idx_qrtz_t_nft_misfire on qrtz_triggers(sched_name,misfire_instr,next_fire_time); create index idx_qrtz_t_nft_st_misfire on qrtz_triggers(sched_name,misfire_instr,next_fire_time,trigger_state); create index idx_qrtz_t_nft_st_misfire_grp on qrtz_triggers(sched_name,misfire_instr,next_fire_time,trigger_group,trigger_state); create index idx_qrtz_ft_trig_inst_name on qrtz_fired_triggers(sched_name,instance_name); create index idx_qrtz_ft_inst_job_req_rcvry on qrtz_fired_triggers(sched_name,instance_name,requests_recovery); create index idx_qrtz_ft_j_g on qrtz_fired_triggers(sched_name,job_name,job_group); create index idx_qrtz_ft_jg on qrtz_fired_triggers(sched_name,job_group); create index idx_qrtz_ft_t_g on qrtz_fired_triggers(sched_name,trigger_name,trigger_group); create index idx_qrtz_ft_tg on qrtz_fired_triggers(sched_name,trigger_group); commit; |
配置數據庫連接池
1.配置jdbc.properties文件
1
2
3
4
|
jdbc.url=jdbc\:mysql\: //192.168.1.132\:3306/jiafuwei?useunicode\=true&characterencoding\=utf8&autoreconnect\=true jdbc.username=root jdbc.password= 123456 jdbc.driverclassname=com.mysql.jdbc.driver |
2.配置applicationcontext.xml文件
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:jdbc= "http://www.springframework.org/schema/jdbc" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base- package = "com.sundoctor" /> <!-- 屬性文件讀入 --> <bean id= "propertyconfigurer" class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" > <property name= "locations" > <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 數據源定義,使用c3p0 連接池 --> <bean id= "datasource" class = "com.mchange.v2.c3p0.combopooleddatasource" destroy-method= "close" > <property name= "driverclass" value= "${jdbc.driverclassname}" /> <property name= "jdbcurl" value= "${jdbc.url}" /> <property name= "user" value= "${jdbc.username}" /> <property name= "password" value= "${jdbc.password}" /> <property name= "initialpoolsize" value= "2" /> <property name= "minpoolsize" value= "10" /> <property name= "maxpoolsize" value= "20" /> <property name= "acquireincrement" value= "2" /> <property name= "maxidletime" value= "1800" /> </bean> </beans> |
創建job測試服務類
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
|
package com.sundoctor.quartz.cluster.example; import java.io.serializable; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.stereotype.service; @service ( "simpleservice" ) public class simpleservice { private static final long serialversionuid = 122323233244334343l; private static final logger logger = loggerfactory.getlogger(simpleservice. class ); public void testmethod1(){ //這里執行定時調度業務 logger.info( "testmethod1.......1" ); system.out.println( "2--testmethod1......." +system.currenttimemillis()/ 1000 ); } public void testmethod2(){ logger.info( "testmethod2.......2" ); } } |
創建一個job類myquartzjobbean1
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
|
package com.sundoctor.quartz.cluster.example; import org.quartz.disallowconcurrentexecution; import org.quartz.jobexecutioncontext; import org.quartz.jobexecutionexception; import org.quartz.persistjobdataafterexecution; import org.quartz.schedulerexception; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.context.applicationcontext; import org.springframework.scheduling.quartz.quartzjobbean; @persistjobdataafterexecution @disallowconcurrentexecution // 不允許并發執行 public class myquartzjobbean1 extends quartzjobbean { private static final logger logger = loggerfactory.getlogger(myquartzjobbean1. class ); @override protected void executeinternal(jobexecutioncontext jobexecutioncontext) throws jobexecutionexception { simpleservice simpleservice = getapplicationcontext(jobexecutioncontext).getbean( "simpleservice" , simpleservice. class ); simpleservice.testmethod1(); } private applicationcontext getapplicationcontext( final jobexecutioncontext jobexecutioncontext) { try { return (applicationcontext) jobexecutioncontext.getscheduler().getcontext().get( "applicationcontextkey" ); } catch (schedulerexception e) { logger.error( "jobexecutioncontext.getscheduler().getcontext() error!" , e); throw new runtimeexception(e); } } } |
配置 quartz 使用集群
1.配置節點的 quartz.properties 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
org.quartz.scheduler.instancename = testscheduler1 org.quartz.scheduler.instanceid = auto org.quartz.threadpool. class = org.quartz.simpl.simplethreadpool org.quartz.threadpool.threadcount = 10 org.quartz.threadpool.threadpriority = 5 org.quartz.threadpool.threadsinheritcontextclassloaderofinitializingthread = true org.quartz.jobstore.misfirethreshold = 60000 org.quartz.jobstore. class = org.quartz.impl.jdbcjobstore.jobstoretx org.quartz.jobstore.driverdelegateclass=org.quartz.impl.jdbcjobstore.stdjdbcdelegate org.quartz.jobstore.tableprefix = qrtz_ org.quartz.jobstore.maxmisfirestohandleatatime= 10 org.quartz.jobstore.isclustered = true org.quartz.jobstore.clustercheckininterval = 20000 |
org.quartz.scheduler.instancename屬性可為任何值,用在 jdbc jobstore 中來唯一標識實例,但是所有集群節點中必須相同。
org.quartz.scheduler.instanceid 屬性為 auto即可,基于主機名和時間戳來產生實例 id。
org.quartz.jobstore.class屬性為 jobstoretx,將任務持久化到數據中。因為集群中節點依賴于數據庫來傳播 scheduler 實例的狀態,你只能在使用 jdbc jobstore 時應用 quartz 集群。這意味著你必須使用 jobstoretx 或是 jobstorecmt 作為 job 存儲;你不能在集群中使用 ramjobstore。
org.quartz.jobstore.isclustered 屬性為 true,你就告訴了 scheduler 實例要它參與到一個集群當中。這一屬性會貫穿于調度框架的始終,用于修改集群環境中操作的默認行為。
org.quartz.jobstore.clustercheckininterval 屬性定義了scheduler 實例檢入到數據庫中的頻率(單位:毫秒)。scheduler 檢查是否其他的實例到了它們應當檢入的時候未檢入;這能指出一個失敗的 scheduler 實例,且當前 scheduler 會以此來接管任何執行失敗并可恢復的 job。通過檢入操作,scheduler 也會更新自身的狀態記錄。clusterchedkininterval 越小,scheduler 節點檢查失敗的 scheduler 實例就越頻繁。默認值是 15000 (即15 秒)。
2.配置applicationcontext-quartz.xml文件
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean name= "quartzscheduler" class = "org.springframework.scheduling.quartz.schedulerfactorybean" > <property name= "datasource" > <ref bean= "datasource" /> </property> <property name= "applicationcontextschedulercontextkey" value= "applicationcontextkey" /> <property name= "configlocation" value= "classpath:quartz.properties" /> <property name= "triggers" > <list> <ref bean= "trigger1" /> </list> </property> </bean> <bean id= "jobdetail1" class = "org.springframework.scheduling.quartz.jobdetailfactorybean" > <property name= "jobclass" > <value>com.sundoctor.quartz.cluster.example.myquartzjobbean1</value> </property> <property name= "durability" value= "true" /> <property name= "requestsrecovery" value= "true" /> </bean> <bean id= "trigger1" class = "org.springframework.scheduling.quartz.crontriggerfactorybean" > <property name= "jobdetail" ref= "jobdetail1" /> <property name= "cronexpression" value= "0/10 * * * * ?" /> </bean> </beans> |
datasource:項目中用到的數據源,里面包含了quartz用到的11張數據庫表;
applicationcontextschedulercontextkey: 是org.springframework.scheduling.quartz.schedulerfactorybean這個類中把spring上下 文以key/value的方式存放在了schedulercontext中了,可以用applicationcontextschedulercontextkey所 定義的key得到對應spring 的applicationcontext;
configlocation:用于指明quartz的配置文件的位置
requestsrecovery
requestsrecovery屬性必須設置為 true,當quartz服務被中止后,再次啟動或集群中其他機器接手任務時會嘗試恢復執行之前未完成的所有任務。
運行quartz集群
在相同或不同的機器上運行com.sundoctor.quartz.cluster.example.test.maintest進行測試,在本例中只是簡單打印一下日志。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.sundoctor.quartz.cluster.example.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class maintest { /** * @param args */ public static void main(string[] args) { applicationcontext springcontext = new classpathxmlapplicationcontext( new string[]{ "classpath:applicationcontext.xml" , "classpath:applicationcontext-quartz.xml" }); } } |
quartz 實際并不關心你是在相同的還是不同的機器上運行節點。當集群是放置在不同的機器上時,通常稱之為水平集群。節點是跑在同一臺機器是,稱之為垂直集群。對于垂直集群,存在著單點故障的問題。這對高可用性的應用來說是個壞消息,因為一旦機器崩潰了,所有的節點也就被有效的終止了。
當你運行水平集群時,時鐘應當要同步,以免出現離奇且不可預知的行為。假如時鐘沒能夠同步,scheduler 實例將對其他節點的狀態產生混亂。有幾種簡單的方法來保證時鐘何持同步,而且也沒有理由不這么做。最簡單的同步計算機時鐘的方式是使用某一個 internet 時間服務器(internet time server its)。
沒什么會阻止你在相同環境中使用集群的和非集群的 quartz 應用。唯一要注意的是這兩個環境不要混用在相同的數據庫表。意思是非集群環境不要使用與集群應用相同的一套數據庫表;否則將得到希奇古怪的結果,集群和非集群的 job 都會遇到問題。
假如你讓一個非集群的 quartz 應用與集群節點并行著運行,設法使用 jobinitializationplugin和 ramjobstore。
項目下載地址,點擊下載。里面包含所需的jar。
在eclipse的兩個工作空間同時開啟這兩個項目,連接同一個mysql數據庫,發現只有一個定時任務在運行,停掉其中一個項目,另外一個項目的定時任務開啟了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/jiafuwei/p/6145280.html