简体   繁体   English

带有 shedlock 的 Spring 启动计划任务不会因延迟而运行

[英]Spring boot schedule task with shedlock not running by its delay

I am using spring boot with shedlock to ensure my schedule task only run in an instance at the same time.我使用带有 shedlock 的 spring boot 来确保我的计划任务只在同一时间在一个实例中运行。

Here is my config这是我的配置

@Configuration
@EnableScheduling
@EnableSchedulerLock(mode = 
EnableSchedulerLock.InterceptMode.PROXY_METHOD, 
defaultLockAtMostFor = "PT15M", 
defaultLockAtLeastFor = "PT2M")
public class SchedulerConfig implements SchedulingConfigurer {
@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource);
}
}

Here my task这是我的任务

@Scheduled(fixedDelayString = "2000")
@SchedulerLock(name = "ms.demo.scheduleTaskJob1")
public void scheduleTaskJob1() {
    logger.info("Fixed delay task called every 2 seconds after latest finished Execution Time - {}",dateTimeFormatter.format(LocalDateTime.now()));
}

Here is shedlock table NAME LOCK_UNTIL LOCKED_AT LOCKED_BY这是 shedlock 表 NAME LOCK_UNTIL LOCKED_AT LOCKED_BY

Problem here is, when my task run the first time at 03-JUL-19 06.37.55.858685000 PM这里的问题是,当我的任务在 03-JUL-19 06.37.55.858685000 PM 首次运行时

Shedlock will add 1 row to DB like this: Shedlock 会像这样向 DB 添加 1 行:

ms.demo.scheduleTaskJob1    03-JUL-19 06.52.37.178573000 PM 03-JUL-19 06.37.55.858685000 PM MB0001

Because defaultLockAtMostFor is 15 minutes因为 defaultLockAtMostFor 是 15 分钟

When the task finished, it will update this record to unlock, and the row right now is:当任务完成后,它会更新这条记录来解锁,现在的行是:

ms.demo.scheduleTaskJob1    03-JUL-19 06.39.37.178573000 PM 03-JUL-19 06.37.55.858685000 PM MB0001

Because defaultLockAtLeastFor is 2 minutes.因为 defaultLockAtLeastFor 是 2 分钟。

when to the next run of the task (2019-07-03T18:37:58.434650900Z), it will updateRecord to get lock by sql command下一次运行任务时(2019-07-03T18:37:58.434650900Z),它会通过sql命令updateRecord获取锁

update shedlock set LOCK_UNTIL = '', lock_at = '' , locked_by = 'MB0001' where name = 'MB0001' and LOCK_UNTIL <= '2019-07-03T18:37:58.434650900Z';

Cannot update, so the task will not running.无法更新,因此任务不会运行。

Here I have to wait about 2 minutes until the current time is over LOCK_UNTIL (03-JUL-19 06.39.37.178573000 PM) while my task is configured with 2s delay So my task not run every 2s as expected, instead of that it will running every 2 minutes.在这里,我必须等待大约 2 分钟,直到当前时间结束 LOCK_UNTIL (03-JUL-19 06.39.37.178573000 PM) 而我的任务配置了 2 秒延迟 所以我的任务没有按预期每 2 秒运行一次,而不是它会运行每 2 分钟。

It is expected behaviour.这是预期的行为。 lockAtLeastFor is parameter that sets minimal interval between runs. lockAtLeastFor 是设置运行之间最小间隔的参数。 It means that you can't run jobs more frequently then specified value.这意味着您不能比指定值更频繁地运行作业。 It helps to fix time synchronization problems between nodes.它有助于解决节点之间的时间同步问题。

In you your particular case when job is complete lock is keeping hold until specified 2 minutes interval is expired ( defaultLockAtLeastFor = "PT2M" ).在您的特殊情况下,当作业完成时,锁定一直保持到指定的 2 分钟间隔到期( defaultLockAtLeastFor = "PT2M" )。 While this time Spring is trying to run job again (as job interval is smaller then lock interval) and Shedlock see that job is locked and preventing new run.虽然这次 Spring 尝试再次运行作业(因为作业间隔小于锁定间隔)并且 Shedlock 看到该作业被锁定并阻止了新的运行。 Source: https://github.com/lukas-krecan/ShedLock ("Annotate your scheduled tasks" section).来源: https : //github.com/lukas-krecan/ShedLock (“注释您的计划任务”部分)。

To fix this you need to change parameter defaultLockAtLeastFor to value less or equal 2 seconds as you specified this value in fixedDelayString = "2000" ("PT2S")要解决此问题,您需要将参数 defaultLockAtLeastFor 更改为小于或等于 2 秒的值,因为您在fixedDelayString = "2000" ("PT2S") 中指定了此值

您可以尝试@SchedulerLock(name = "ms.demo.scheduleTaskJob1", lockAtLeastFor = "PT2S")使用非默认选项或更改defaultLockAtLeastFor ,在@EnableSchedulerLock注释中

确保在 spring 2.x 之后使用 @EnableSchedulerLock。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring Boot中安排任务并增加延迟? - How to schedule a task in spring boot with increasing delay? 使用计划在Azure WebJob上运行Spring Boot应用程序 - Running a Spring Boot App on Azure WebJob with a Schedule spring-boot和shedlock-如何追踪依赖冲突? - spring-boot and shedlock - how to track down dependency conflicts? 在 Spring Boot 中实现 Shedlock 使用 Redis 缓存(Jedis 资源池) - Implementing Shedlock in Spring Boot using Redis Cache (Jedis resource Pool) 如何使用多个cron设置时间表以使用Spring Boot完成一项任务 - How to set a schedule with multiple cron to do one task with spring boot 在 Spring Boot 应用程序中安排任务的最佳方法是什么 - What is best way to schedule task in spring boot application 如何在Spring Boot应用程序中修复Websockets和Shedlock兼容性 - How to fix Websockets and Shedlock compatibility in Spring Boot application 如何在 Spring Boot 中根据 WebSocket 事件 @Schedule 任务 - How to @Schedule a task according to a WebSocket Event in Spring Boot 如何在Spring Boot中在当前运行的线程上运行@schedule - how to run @schedule on the current running thread in spring boot Spring Boot运行正常,但不是它的Jar? - Spring boot running fine, but not its Jar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM