首页
App
&
Coffee
文档
项目
分享
反馈
俱乐部
登录
注册
多节点部署下,用quartz跑定时任务的问题
sbw
2025-04-15 15:17
多节点时,各个节点重复跑定时任务,怎么处理比较好
项目:
JFinal
评论区
杜福忠
2025-04-15 23:48
可以使用分布式锁来处理。
任务不多的情况下,可以使用数据库行锁来解决。任务一般都有任务表,每个任务上面增加一个锁状态,谁 update 修改成功就谁执行。
任务量大的情况下,可以用Redis 锁来处理。
或者把任务业务单拆出去,独立JVM来运行处理。
回复
zzutligang
2025-04-17 14:48
quartz可以通过配置,适配多节点调度。
#
#============================================================================
# Configure Main Scheduler Properties 调度器属性
#============================================================================
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount= 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
#============================================================================
# Configure JobStore
#============================================================================
#存储方式使用JobStoreTX,也就是数据库
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#使用自己的配置文件
org.quartz.jobStore.useProperties:true
#数据库中quartz表的表名前缀
org.quartz.jobStore.tablePrefix:QRTZ_
org.quartz.jobStore.dataSource:QuartzDS
#是否使用集群(如果项目只部署到 一台服务器,就不用了)
org.quartz.jobStore.isClustered = false
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.clusterCheckinInterval: 10000
#============================================================================
# Configure Datasources
#============================================================================
#配置数据库源
org.quartz.dataSource.QuartzDS.connectionProvider.class: cn.york.common.quartz.util.DruidConnectionProvider
org.quartz.dataSource.QuartzDS.driver: com.mysql.cj.jdbc.Driver
org.quartz.dataSource.QuartzDS.url: jdbc:mysql://192.168.0.46:3306/pt?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true
org.quartz.dataSource.QuartzDS.user: root
org.quartz.dataSource.QuartzDS.password: ***********
org.quartz.dataSource.QuartzDS.validationQuery: select 0 from dual
org.quartz.dataSource.QuartzDS.maxConnection: 10
然后你就需要实现cn.york.common.quartz.util.DruidConnectionProvider这个数据源就行了
回复
发送
我要反馈
热门反馈
扫码入社
任务不多的情况下,可以使用数据库行锁来解决。任务一般都有任务表,每个任务上面增加一个锁状态,谁 update 修改成功就谁执行。
任务量大的情况下,可以用Redis 锁来处理。
或者把任务业务单拆出去,独立JVM来运行处理。