Search code examples
javaspringspring-bootpropertiesshedlock

Shedlock: use lockAtMostForString from property file


I have code like this:

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
}

Now I use constant for lockAtMostForString but I want to take this value from property for this.
Is there way to do it?

P.S.

I know that I can not use annotations and rewrite it like this:

LockingTaskExecutor executor = new DefaultLockingTaskExecutor(lockProvider);

...

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

But I prefer to use annotations.


Solution

  • You can directly inject your property into the annotation by using the injection syntax from spring ${propertyName:defaultValue}.

    Note 1: When not declaring a defaultValue then spring will throw an error if the property is missing.

    Note 2: This syntax can be used with almost every spring-annotation.