What to use instead of ScheduledLockConfiguration Bean , in shedlock-spring 3.0?

2.7k Views Asked by At

I have a ScheduledLockConfiguration bean configuration.

@Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
    return ScheduledLockConfigurationBuilder
        .withLockProvider(lockProvider)
        .withPoolSize(5)
        .withDefaultLockAtMostFor(Duration.ofMinutes(5))
        .build();
}

I just upgraded to shedlock-spring 3.0, and I don't know what to use instead of this Bean?

1

There are 1 best solutions below

0
On

We can configure like below.

@Component
class TaskScheduler {
    @Scheduled(cron = "0 0 10 * * ?")
    @SchedulerLock(name = "TaskScheduler_scheduledTask", lockAtLeastForString = "PT5M", lockAtMostForString = "PT14M")
        public void scheduledTask() {
            // ...
        }
    }

@Scheduled will support corn format.

@SchedulerLock, the name parameter has to be unique and ClassName_methodName is typically enough to achieve that. We don't want more than one run of this method happening at the same time, and ShedLock uses the unique name to achieve that.

First, we've added lockAtLeastForString so that we can put some distance between method invocations. Using “PT5M” means that this method will hold the lock for 5 minutes, at a minimum. In other words, that means that this method can be run by ShedLock no more often than every five minutes.

Next, we added lockAtMostForString to specify how long the lock should be kept in case the executing node dies. Using “PT14M” means that it will be locked for no longer than 14 minutes.

In normal situations, ShedLock releases the lock directly after the task finishes. Now, really, we didn't have to do that because there is a default provided in @EnableSchedulerLock, but we've chosen to override that here.