ShedLock:运行多个实例多次运行调度程序任务 [英] ShedLock: Running multiple instances runs scheduler tasks multiple times

查看:792
本文介绍了ShedLock:运行多个实例多次运行调度程序任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果服务的多个实例正在运行,我只使用 Shedlock 来运行我的调度程序任务一次。

I am using Shedlock to run my scheduler task only once if multiple instances of the service are running.

我按照文档进行了操作就是我做的。

I followed the documentation and this is what I did.

这是需要定期运行的功能

This is the function that needs to run periodically

@Scheduled(fixedDelayString = "300000")
@SchedulerLock(name = "onlineIngestionTask", lockAtMostFor = 240000, lockAtLeastFor = 240000)
public void pullTasksFromRemote() {
        //Code
}

在我的配置类中,我有以下bean

In my config class I have the following beans

@Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
    return ScheduledLockConfigurationBuilder
        .withLockProvider(lockProvider)
        .withPoolSize(10)
        .withDefaultLockAtMostFor(Duration.ofMinutes(10))
        .build();
}



@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource);
}

pom包括

<dependency>
    <groupId>net.javacrumbs.shedlock</groupId>
    <artifactId>shedlock-spring</artifactId>
    <version>0.14.0</version>
</dependency>

<dependency>
    <groupId>net.javacrumbs.shedlock</groupId>
    <artifactId>shedlock-provider-jdbc-template</artifactId>
    <version>0.14.0</version>
</dependency>

我在我的数据库中添加了一个表,jdbc连接的表。

I added a table to my db, the one to which jdbc connects.

CREATE TABLE shedlock(
    name VARCHAR(64), 
    lock_until TIMESTAMP(3) NULL, 
    locked_at TIMESTAMP(3) NULL, 
    locked_by  VARCHAR(255), 
    PRIMARY KEY (name)
) 

在此之后,我尝试通过首先在端口8080上运行tha pp来测试功能。然后我使用 server.port = 9000 再次运行它端口9000.但这两个实例都开始运行任务。我错过了什么。实施中出了什么问题?有人可以提出任何想法。谢谢!!

After this I tried to test the functionality by running tha pp first on port 8080. Then I use server.port=9000 to run it again on port 9000. But both these instances start running the task. Am I missing something. Is something wrong in the implementation? Can someone give any ideas. Thanks !!

推荐答案

dlock 通过使用数据库索引和约束来保证随时拥有一个锁。您可以简单地执行以下操作。

dlock guarantees to have one lock at any time by using database indexes and constraints. You can simply do something like below.

@Scheduled(cron = "30 30 3 * * *")
@TryLock(name = "onlineIngestionTask", owner = SERVER_NAME, lockFor = 240000)
public void pullTasksFromRemote() {

}

请参阅文章关于使用它。

这篇关于ShedLock:运行多个实例多次运行调度程序任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆