@Scheduled(fixedDelay = 5000)
public void myJob() {
Thread.sleep(12000);
}
How can I prevent this spring job from running if the previous routine is not yet finished?
@Scheduled(fixedDelay = 5000)
public void myJob() {
Thread.sleep(12000);
}
How can I prevent this spring job from running if the previous routine is not yet finished?
With fixedDelay
, the period is measured after the completion of job, so no worries.
cron
(which is definitly intended)? –
Birthroot by default, spring uses a single-threaded Executor. so no two @Scheduled tasks will ever overlap. even two @Scheduled methods in completely unrelated classes will not overlap simply because there is only a single thread to execute all @Scheduled tasks.
furthermore, even if you replace the default Executor with a thread pool based executor, those Executors will typically delay the execution of a task instance until the previously scheduled instance completes. this is true for fixedDelay, fixedInterval, and cron based schedules. for example, this spring configuration will create a ScheduledThreadPoolExecutor that uses a threadpool, but does not allow concurrent instances of the same schedule just as you desire:
@Configuration
@EnableScheduling
...
public class MySpringJavaConfig {
@Bean(destroyMethod = "shutdown")
public Executor taskScheduler() {
return Executors.newScheduledThreadPool(5);
}
...
}
here is the javadoc for ScheduledThreadPoolExecutor::scheduleAtFixedRate which specifies:
If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.
note: this functionality does not hold true for @Async tasks. spring will create as many concurrent instances of those as needed (if there are sufficient threads in the pool).
@Scheduled foo(){Bar.baz();}} class Bar{ @Async baz(){/*work done here*/}}
keeping in mind that the async threadpool must be large enough for all your overlapping workers, and if you're workers never complete you will eventually exhaust even the largest thread pool. –
Demur @Scheduled
annotations like: @Scheduled(cron = "...") @Scheduled(initialDelayString = "PT10S", fixedDelayString = "PT1H")
? –
Intertexture Thread.sleep(60000)
. then add 2 @Scheduled
annotations to it (one that runs on the first second of every minute and one that runs on the 10th second). let us know if you see 1 timestamp per minute or 2 :) –
Demur spring.task.execution.pool.size=10
and spring.task.scheduling.pool.size=10
but it made no difference. –
Scumble With fixedDelay
, the period is measured after the completion of job, so no worries.
cron
(which is definitly intended)? –
Birthroot © 2022 - 2024 — McMap. All rights reserved.