简体   繁体   English

使用@SnapLock 注释调用计划任务两次

[英]Schedule tasks are called twice with the @SnapLock annotation

I want only one schedule method to work for many instances.我只希望一种调度方法适用于许多实例。 I added the snap-scheduler to my project, and after that I deployed two different instances to the railway.我将snap-scheduler添加到我的项目中,然后我将两个不同的实例部署到铁路上。 And two entries about the execution of my method are added to the snap_lock table.并且将关于我的方法执行的两个条目添加到snap_lock表中。 Сan you explain where I went wrong?你能解释一下我哪里出错了吗?

It`s scheduler config:它的调度程序配置:

@Configuration
@Import(SnapAppConfig.class)
public class SchedulerConfiguration {

    private static final String SNAP_DB_POOL_NAME = "snap-pool";

    @Bean(name = "snapDataSource")
    public DataSource customDataSource(final DataSourceProperties properties,
                                       @Value("${spring.datasource.hikari.schema}") final String schema) {
        final HikariDataSource dataSource = properties.initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();

        dataSource.setPoolName(SNAP_DB_POOL_NAME);
        Optional.ofNullable(schema).ifPresent(dataSource::setSchema);

        return dataSource;
    }
}

It`s method with @SnapLock and @Scheduled annotations它是带有@SnapLock 和@Scheduled 注释的方法

@Slf4j
@Service
@RequiredArgsConstructor
public class SchedulerService {

    private static final String EUROPE_KIEV_ZONE = "Europe/Kiev";
    
    @SnapLock(key = "REPORT_CURRENT_TIME", time = 30)
    @Scheduled(cron = "0 */1 * * * *", zone = EUROPE_KIEV_ZONE)
    protected void test() {
        log.info("time now equals = " + Instant.now());
    }
}

It`s psql这是psql

CREATE TABLE snap_lock
(
    task_key    text                     NOT NULL,
    task_method text                     NOT NULL,
    lock_until  timestamp with time zone NOT NULL,
    lock_at     timestamp with time zone NOT NULL,
    lock_by     text                     NOT NULL,
    CONSTRAINT snap_lock_pkey PRIMARY KEY (task_key, task_method)
);

CREATE TABLE snap_task_audit
(
    id               integer GENERATED BY DEFAULT AS identity,
    task_key         text                     NOT NULL,
    task_method      text                     NOT NULL,
    run_on           text                     NOT NULL,
    start_run        timestamp with time zone NOT NULL,
    end_run          timestamp with time zone NOT NULL,
    run_time_seconds numeric                  NOT NULL,
    task_error       jsonb,
    CONSTRAINT snap_task_audit_pkey PRIMARY KEY (id)
);

CREATE TABLE snap_scheduler
(
    name            text                     NOT NULL,
    task_key        text                     NOT NULL,
    type            text                     NOT NULL,
    task_class      text                     NOT NULL,
    task_data       jsonb,
    task_data_class text,
    run_at          timestamp with time zone NOT NULL,
    recurrence      text,
    picked          boolean                  NOT NULL DEFAULT false,
    picked_by       text,
    end_run         timestamp with time zone,
    CONSTRAINT snap_scheduler_pkey PRIMARY KEY (task_key)
);

As an alternative, I solved my problem using a library https://github.com/lukas-krecan/ShedLock .作为替代方案,我使用库https://github.com/lukas-krecan/ShedLock解决了我的问题。 Everything is similar there, the method through the base is blocked.那里的一切都是相似的,通过基地的方法被封锁了。 But there were no problems with crowns.但是冠没有问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM