如何用编程风格替换@SchedulerLock

How to replace @SchedulerLock with programmatic style

我使用 spring 引导,在代码的某处我有以下代码:

@SchedulerLock(name = "onlineIngestionTask", lockAtMostFor = 900, lockAtLeastFor = 900)
    public void pullTasksFromRemote() throws InterruptedException {
        logger.info("task-started");
        Thread.sleep(500);
        logger.info("task-stopped");
    }

有没有办法通过编程方式替换它?

您似乎可以在没有 Spring 注释的情况下做到这一点,如文档中所述:https://github.com/lukas-krecan/ShedLock#running-without-spring

LockingTaskExecutor executor = new DefaultLockingTaskExecutor(lockProvider);

...

Instant lockAtMostUntil = Instant.now().plusSeconds(600);
executor.executeWithLock(runnable, new LockConfiguration("lockName", lockAtMostUntil));