How to stop Spring @Scheduled from queuing up jobs

I have code running every two minutes using the @Scheduled annotation. However, our jobs tend to run for longer periods of time. As I understand it, the @Scheduled annotation queues up the new jobs and runs them as soon as the first job is complete. I do not want this to happen. I want there to be only 1 instance of the job running and no queued instances. How can I do this?

@Scheduled(cron = "0 */2 * * * ?")
public void twoMinMethod() {
    // code here
}

Upvotes: 5

Views: 1819

Answers (1)

Todd
Todd

Reputation: 31700

If you don't need to run the jobs exactly on the minute, you might want to switch to the other non-crontab syntax which will allow this sort of thing. The example below will execute the method, wait two minutes from when it finishes, and then execute it again.

@Scheduled(fixedDelay=120000)
public void twoMinMethod() { ... }

See more at the Spring Documentation.

Upvotes: 2

Related Questions