'How to reset scheduled (@Scheduled) in spring boot

I have spring boot application, where I need to refresh every 20 seconds so I created this:

@Scheduled(cron="*/20 * * * * *")
@GetMapping("/refresh")
public void refresh(){
    letterServiceI.refreshQueue();
}

And it works perfectly, but when I am adding row to database and before it was empty I want to reset timer to 20 seconds again, is there a way to do it?

@Override
public Letter add(String name, String content, String pin) {

    if (letterRepositoryI.checkIfNotEmpty().isEmpty()){
        //if empty -> reset scheduler to 20 again
        
    }


Solution 1:[1]

You could just call the method annotated with @Scheduled(cron="*/20 * * * * *") whenever you add a new row to the database.

If you wanted to make your Scheduled expression more concise, you could as well use @Scheduled(fixedDelay = 20, timeUnit = TimeUnit.SECONDS).

I acknowledge that this might lead to the scheduled reset taking place shortly after the manual call, but I could not find a way to reset this timer manually in the documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1