Shedlock:使用属性文件中的lockAtMostForString

时间:2019-02-20 12:25:28

标签: java spring spring-boot properties shedlock

我有这样的代码:

private static final String FOURTEEN_MIN = "PT14M";
...

@Scheduled(cron = "0 */15 * * * *")
@SchedulerLock(name = "scheduledTaskName", lockAtMostForString = FOURTEEN_MIN, lockAtLeastForString = FOURTEEN_MIN)
public void scheduledTask() {
   // do something
}

现在我为lockAtMostForString使用常量,但是我想从属性中获取此值。
   有办法吗?

P.S。

我知道我不能使用注释并像这样重写它:

LockingTaskExecutor executor = new DefaultLockingTaskExecutor(lockProvider);

...

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

但是我更喜欢使用注释。

1 个答案:

答案 0 :(得分:1)

您可以使用spring ${propertyName:defaultValue}中的注入语法将属性直接注入注释中。

注1:当不声明defaultValue时,如果缺少该属性,spring将抛出错误。

注2:几乎每个spring注释都可以使用此语法。