https://www.jianshu.com/p/572322b36383 一篇不错的帖子
1:定时任务传递参数
JobDataMap jobDataMap = new JobDataMap();
// 设置参数
jobDataMap.put("executeTime", scheduleInfo.getExecuteTime());
scheduler.triggerJob(JobKey.jobKey(jobName, jobGroup), jobDataMap);
接收:
@Override
public void execute(JobExecutionContext context) {
JobDataMap dataMap = context.getMergedJobDataMap();
String executeTime = dataMap.getString("executeTime");
}
注意错误写法:
下面这种写法会将参数持久化到数据库中
JobDetail job = JobBuilder.newJob(DailyReportJob.class)
.withIdentity(jobName, jobGroup)
.storeDurably(true)
.build();
// 设置参数
job.getJobDataMap().put("executeTime", scheduleInfo.getExecuteTime());
scheduler.addJob(job, true);
scheduler.triggerJob(JobKey.jobKey(jobName, jobGroup));
2:定时任务大姨妈问题
问题描述:冒烟环境经常会发生定时任务每隔几天就有几小时不执行的情况
经过排查,发现日志报了misfired问题,最后通过修改参数解决了问题
misfireThreshold: 120000
threadCount: 15
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
dataSource: qzDS
misfireThreshold: 120000
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 15
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
#数据库方式
job-store-type: jdbc
#初始化表结构
jdbc:
initialize-schema: never
dataSource:
qzDS:
driver: com.mysql.jdbc.Driver
URL: jdbc:mysql://xxxx:3306/xxx_quartz?characterEncoding=UTF-8
user: '{cipher}'
password: '{cipher}'
maxConnections: 10
provider: hikaricp
datasource:
url: jdbc:mysql://xxxx:3306/xxx?allowMultiQueries=true
kafka:
producer:
properties:
security:
protocol: SASL_PLAINTEXT
sasl:
mechanism: PLAIN
3: