如何以编程方式替换@SchedulerLock

8

我使用Spring Boot,在代码中有以下代码:

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

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

在执行pullTasksFromRemote之前获取锁,然后释放锁。 - TongChen
1个回答

12

根据文档所说,您似乎可以不使用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));

在文档中提到“无Spring”,没有提及注解。 - gstackoverflow
@gstackoverflow 你的意思是什么?在这种情况下,“Without Spring”和“without annotations”是完全相同的东西。 - Michael
@Michael,我不理解语义上的差异,只是指出了词汇上的差异。如果它们是相同的 - 很好。 - gstackoverflow
@Michael ,默认的lockProvider是什么? - gstackoverflow
@gstackoverflow 所有的提供者都在这里(https://github.com/lukas-krecan/ShedLock/tree/master/providers)。我猜Spring的默认值可能是jdbc。 - Michael
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接