javaspring-bootscheduled-tasksshedlock

How to replace @SchedulerLock with programmatic style


I use spring boot and somewhere in code I have following code:

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

Is there way to replace it via programmatic style?


Solution

  • You seem to be able to do it without Spring Annotations like said in the documentation: 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));