分布式 Quartz 的配置与 Spring 的集成使用。
初始化数据库
运行下面的(MySQL)数据库脚本,创建数据库表(先参照 quartz.properties 文件中
org.quartz.dataSource.myDS.URL
属性配置的数据库名称创建数据库,这里我的数据库名称即为 quartz_scheduler)其他数据库脚本见官网源代码(http://www.quartz-scheduler.org/downloads/)中 quartz-xxx-distribution\quartz-xxx\docs\dbTables 文件夹。
mysql.sql
1 | # |
配置 quartz.properties 文件
quartz.properties 文件位置
1 | #============================================================================ |
主要依赖
Maven:
1 | <dependency> |
Gradle:
1 | compile("org.quartz-scheduler:quartz:2.2.3") |
Server 端示例代码
SchedulerJob.java
1 | import org.springframework.scheduling.quartz.QuartzJobBean; |
让每个Job实例都继承SchedulerJob
Jdbc.java
1 | @Getter |
WebConfig.java
1 | @Configuration |
SchedulerJobServiceImpl.java
1 | @Service |
SchedulerCreateController.java
1 | @Controller |
对Job实例的其他操作
略
Client 端示例代码
ValueObject.java
1 | public interface ValueObject<T> extends Serializable { |
Jobable.java
1 | public interface Jobable { |
SchedulerJob.java
1 | @Data |
Jdbc.java
1 | @Getter |
RemoteSchedulerJobServiceImpl.java
1 | @Service |
JdbcJobCreateController.java
1 | @Controller |
将执行结果输入到日志中
数据库表的结构
FAQ
如果启动了远程 quartz ,那么本地如果同时启动了 quartz 且使用了远程 quartz 这个数据库,这时会出现 quartz 时间戳不统一的问题。(QRTZ_SCHEDULER_STATE 表)
org.quartz.JobPersistenceException: Couldn’t acquire next trigger: Field ‘SCHED_TIME’ doesn’t have a default value.
详细信息:
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
4710:25:58.428 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] ERROR org.quartz.core.ErrorLogger - An error occurred while scanning for the next triggers to fire.
org.quartz.JobPersistenceException: Couldn't acquire next trigger: Field 'SCHED_TIME' doesn't have a default value
at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTrigger(JobStoreSupport.java:2822) ~[main/:?]
at org.quartz.impl.jdbcjobstore.JobStoreSupport$41.execute(JobStoreSupport.java:2749) ~[main/:?]
at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3795) ~[main/:?]
at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTriggers(JobStoreSupport.java:2745) ~[main/:?]
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:264) [quartz-2.1.7.jar:?]
Caused by: java.sql.SQLException: Field 'SCHED_TIME' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994) ~[mysql-connector-java-5.1.39.jar:5.1.39]
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105) ~[c3p0-0.9.1.1.jar:0.9.1.1]
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.insertFiredTrigger(StdJDBCDelegate.java:2624) ~[quartz-2.1.7.jar:?]
at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTrigger(JobStoreSupport.java:2805) ~[main/:?]
... 4 more
10:27:56.133 [QuartzScheduler_org.springframework.scheduling.quartz.SchedulerFactoryBean#0-DESKTOP-DGF5RBI1489717255135_MisfireHandler] INFO org.quartz.impl.jdbcjobstore.JobStoreTX - Handling 1 trigger(s) that missed their scheduled fire-time.原因:使用的 jar 包版本与执行的数据库脚本不一致。
如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理