Reputation: 489
I'm trying to use @Scheduled
method to process some regular work (every second). Body of this method can process during more than one second and I see that if this happens next execution is not started. Does Spring support it or I should change it to any other concurrent solution?
I have tried to change Scheduler to ConcurrentTaskScheduler
, but looks like it helps only if we have few schedules methods.
@Service
public class MainService {
@Scheduled(cron = "* * * * * *")
public void doSomething() {
//some logic which can takes more than 1 second
}
}
@Configuration
public class SchedulingConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean
public Executor taskExecutor() {
return new ConcurrentTaskScheduler(
Executors.newScheduledThreadPool(100));
}
}
Once the first execution is taking extra time the second execution will not be started. Otherwise, all works fine. How can I set up parallel execution of one scheduled method?
Upvotes: 0
Views: 976
Reputation: 999
You can introduce an async component so it does not take 1 second https://www.baeldung.com/spring-async
@Service
public class MainService {
@Autowired
private SomethingService somethingService;
@Scheduled(cron = "* * * * * *")
public void doSomething() {
somethingService.doSomething();
}
}
//Introduce an async component so it does not take 1 second. runs doSomething() in a separate thread
@Component
public class SomethingService {
@Async
public void doSomething() {
//some logic which can takes more than 1 second
}
}
Upvotes: 1