【问题标题】:How to replace @SchedulerLock with programmatic style如何用编程风格替换@SchedulerLock
【发布时间】:2019-07-04 20:28:03
【问题描述】:

我使用 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");
    }

有没有办法通过程序化样式替换它?

【问题讨论】:

  • 在执行 pullTask​​sFromRemote 之前获取锁然后释放锁

标签: java spring-boot scheduled-tasks shedlock


【解决方案1】:

您似乎可以在没有 Spring Annotations 的情况下做到这一点,如文档中所述: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” 没有提到注释
  • 他正在使用 Shedlock。他的问题被标记了。
  • @gstackoverflow 你是什么意思?在这种情况下,“没有 Spring”和“没有注释”是完全一样的。
  • @Michael,我不理解语义上的差异,只是在词汇差异中指出。如果它是相同的 - 很酷。
  • @Michael , 默认 lockProvider 是什么?
猜你喜欢
  • 2013-02-26
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多