Skip to content

Commit 6edbed9

Browse files
committed
更新quartz的使用方式
1 parent 21e8574 commit 6edbed9

File tree

8 files changed

+41
-37
lines changed

8 files changed

+41
-37
lines changed

Spring-Boot-Quartz/src/main/java/com/example/quartz/config/SchedulerConfig.java

-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,5 @@ public QuartzInitializerListener initializerListener() {
4848
@Bean("scheduler")
4949
public Scheduler scheduler() throws IOException {
5050
return schedulerFactoryBean().getScheduler();
51-
5251
}
5352
}

Spring-Boot-Quartz/src/main/java/com/example/quartz/entity/JobAndTrigger.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void setTriggerGroup(String triggerGroup) {
6767
}
6868

6969
public Date getStartTime() {
70-
return startTime;
70+
return startTime != null ? startTime : new Date();
7171
}
7272

7373
public void setStartTime(Date startTime) {
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package com.example.quartz.job;
22

3-
import org.quartz.Job;
4-
import org.quartz.JobExecutionContext;
5-
import org.quartz.JobExecutionException;
3+
import org.springframework.scheduling.quartz.QuartzJobBean;
64

75
/**
86
* Created by dengzhiming on 2019/8/1
97
*/
10-
public interface BaseJob extends Job {
11-
@Override
12-
void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException;
8+
public abstract class BaseJob extends QuartzJobBean {
9+
1310
}

Spring-Boot-Quartz/src/main/java/com/example/quartz/job/HelloJob.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
/**
1111
* Created by dengzhiming on 2019/8/1
1212
*/
13-
public class HelloJob implements BaseJob {
13+
public class HelloJob extends BaseJob {
1414

1515
private static Logger logger = LoggerFactory.getLogger(HelloJob.class);
16-
17-
public HelloJob() {
18-
}
19-
2016
@Override
21-
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
17+
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
2218
logger.info("HelloJob执行时间: " + new Date());
2319
}
2420
}

Spring-Boot-Quartz/src/main/java/com/example/quartz/job/NewJob.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
/**
1111
* Created by dengzhiming on 2019/8/1
1212
*/
13-
public class NewJob implements BaseJob {
13+
public class NewJob extends BaseJob {
1414

1515
private static Logger logger = LoggerFactory.getLogger(NewJob.class);
16-
17-
public NewJob() {
18-
}
19-
2016
@Override
21-
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
17+
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
2218
logger.info("NewJob执行的时间: " + new Date());
2319
}
2420
}

Spring-Boot-Quartz/src/main/resources/application.properties

-1
This file was deleted.

Spring-Boot-Quartz/src/main/resources/application.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ server:
22
port: 8080
33

44
spring:
5+
application:
6+
name: springboot-quartz
57
datasource:
68
druid:
79
type: com.alibaba.druid.pool.DruidDataSource
@@ -62,7 +64,18 @@ spring:
6264
wall:
6365
config:
6466
multi-statement-allow: true
65-
67+
quartz:
68+
#数据库方式
69+
job-store-type: jdbc
70+
#scheduler执行器的名称
71+
scheduler-name: ${spring.application.name}
72+
#是否覆盖已存在的任务
73+
overwrite-existing-jobs: off
74+
#延迟启动时间
75+
startup-delay: 500ms
76+
#初始化表结构
77+
jdbc:
78+
initialize-schema: never
6679

6780
mybatis:
6881
# mapper xml实现扫描路径
@@ -74,4 +87,4 @@ mybatis:
7487
# 1.logging.level.yourdaoclasspackagename=debug (指定某个包下的SQL打印出来)
7588
# 2.mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
7689
# (所有包下的SQL都会打印出来)
77-
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
90+
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
# ThreadPool实现的名称(Quartz附带的线程池是"org.quartz.simpl.SimpleThreadPool")
1+
# ThreadPool实现的名称(Quartz自带线程池是"org.quartz.simpl.SimpleThreadPool")
22
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
33
# threadCount和threadPriority将以setter的形式注入ThreadPool实例
44
# 可用于并发执行作业的线程数(只有1到100之间的数字是非常实用的)
55
org.quartz.threadPool.threadCount=5
66
# 线程的优先级,默认值为5
77
org.quartz.threadPool.threadPriority=5
8-
#是否继承初始化线程的上下文加载器类,默认为false
8+
#是否继承初始化线程的上下文加载器类,默认为false
99
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
10-
#调度程序在被认为是"misfired"之前将"容忍"触发器到下一次触发时间的毫秒数(默认60000)
11-
org.quartz.jobStore.misfireThreshold=5000
10+
1211
# 默认存储在内存中,RAMJobStore快速轻便,但是当进程终止时,所有调度信息都会丢失
1312
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
14-
#持久化
13+
# 持久化
1514
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
16-
#您需要为JobStore选择一个DriverDelegate才能使用。DriverDelegate负责执行特定数据库可能需要的任何JDBC工作
17-
# StdJDBCDelegate是一个使用“vanilla”JDBC代码和SQL语句来执行其工作的委托,用于完全符合JDBC的驱动程序
15+
# DriverDelegate负责执行特定数据库可能需要的任何JDBC工作,需为JobStore选择一个DriverDelegate才能使用
16+
# StdJDBCDelegate是一个使用“vanilla”JDBC代码(和SQL语句)来执行其工作的委托,用于完全符合JDBC的驱动程序
1817
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
19-
#可以将“org.quartz.jobStore.useProperties”配置参数设置为“true”(默认为false)以指示JDBCJobStore将JobDataMaps中的所有值都作为字符串,
20-
# 因此可以作为名称 - 值对存储而不是在BLOB列中以其序列化形式存储更多复杂的对象
18+
# 调度程序在被认为是"misfired"之前将"容忍"触发器到下一次触发时间的毫秒数(默认60000)
19+
org.quartz.jobStore.misfireThreshold=5000
20+
# 将该配置参数设置为“true”(默认为false)用以指示JDBCJobStore将JobDataMaps中的所有值都作为字符串
2121
org.quartz.jobStore.useProperties=true
22-
#表前缀
22+
# 表前缀
2323
org.quartz.jobStore.tablePrefix=QRTZ_
24-
#设置JobStore使用哪个DataSource
24+
# 设置为"true"以打开群集功能;若有多个Quartz实例使用同一组数据库表,则此属性必须设置为"true"
25+
#org.quartz.jobStore.isClustered=false
26+
#org.quartz.jobStore.clusterCheckinInterval:10000
27+
28+
# 设置JobStore使用哪个DataSource
2529
org.quartz.jobStore.dataSource=qzDS
2630
org.quartz.dataSource.qzDS.driver=com.mysql.cj.jdbc.Driver
2731
org.quartz.dataSource.qzDS.URL=jdbc:mysql://localhost:3306/quartz?serverTimezone=GMT%2B8
2832
org.quartz.dataSource.qzDS.user=root
2933
org.quartz.dataSource.qzDS.password=root
3034
org.quartz.dataSource.qzDS.maxConnections=10
31-
#设置为“true”以打开群集功能;如果您有多个Quartz实例使用同一组数据库表,则此属性必须设置为“true”
32-
#org.quartz.jobStore.isClustered=false
33-
#配置集群参见: https://www.w3cschool.cn/quartz_doc/quartz_doc-3x7u2doc.html
35+
36+
37+
#配置集群参见: https://www.w3cschool.cn/quartz_doc/quartz_doc-3x7u2doc.html

0 commit comments

Comments
 (0)