Java Spring的ShedLock失败

huangapple go评论84阅读模式
英文:

Java Spring shedlock fails

问题

我有一个Spring Boot应用程序,在生产环境中运行2个实例,导致计划任务运行两次。为了避免这种情况,我尝试使用ShedLock,如这里这里所解释的,但没有任何效果。
我已经在使用MySQL数据库,根据上述示例在数据库中添加了一个shedlock表。

我的项目结构如下(部分内容)-

在我的application.properties文件中,我有以下设置-

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/b4ad?autoReconnect=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
db.username=*****
db.password=*****

AppConfig类中,我添加了此注释-

@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")

以及此bean-

@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource, "b4ad.shedlock");
}

ScheduledManager类中,我有这个定时任务-

 @Scheduled(cron = "0 0/1 * * * ?")
@SchedulerLock(name = "deactivateExpiredOrganizations", lockAtLeastFor = "PT5M")
public void deactivateExpiredOrganizations() {
    // 我的代码...
}

当我在本地运行应用程序时(在application.properties文件中使用2个不同的端口),通过查看日志,我可以看到任务在两个实例上运行,并且同时shedlock表保持为空。
根据故障排除部分中的第二项,似乎我配置有误,但我无法确定是什么问题。

任何帮助将不胜感激。

英文:

i have a spring-boot app, which on production environment runs in 2 instances, resulting in scheduled tasks running twice. to avoid that i'm trying use shedlock, as explained here and here, but it has no affect.
i'm already using a MySql db, to which i added a shedlock table as explained on both of the examples above.

my project's structure is as follows (partially) -

Java Spring的ShedLock失败

in my application.properties file i have this settings -

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/b4ad?autoReconnect=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
db.username=*****
db.password=*****

to the AppConfig class i added this annotation -

@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")

and this bean -

@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource, "b4ad.shedlock");
}

and in the ScheduledManager class i have this scheduled task -

 @Scheduled(cron = "0 0/1 * * * ?")
@SchedulerLock(name = "deactivateExpiredOrganizations", lockAtLeastFor = "PT5M")
public void deactivateExpiredOrganizations() {
    // my code...
}

when i run my application locally on 2 instances (using 2 different ports in the application.properties file), looking at the log i can see that the task runs on both instances, and at the same time the shedlock table remains empty.
according to the second item in the troubleshooting section, it seems that i miss-configured something, but i can't tell what.

any help will be appreciated.

答案1

得分: 0

好的,以下是翻译好的内容:

好像在我的大项目中除了 AppConfig 之外还有另一个配置类,名为 SqlSpringConfig,位于 db 模块内。一旦我在那里放置了 LockProvider 这个 Bean,一切都运行得很顺利。

英文:

well, apparently in my big project there was another config class (besides the AppConfig) - one called SqlSpringConfig, inside the db module.
once i put the LockProvider bean there, all works like a charm

huangapple
  • 本文由 发表于 2020年10月1日 16:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64152064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定