How to enable TaskScheduler in spring-boot?

I use spring-boot to configure spring by default. I would like to use the @EnableScheduling mechanism and assign my tasks conditional.

Therefore, I have to implement SchedulingConfigurer and set TaskScheduler explicitly.

But when I enter TaskScheduler I get the following error. But why doesn't spring-boot automatically provide the Scheduler accordingly?

 @Configuration @EnableAutoConfiguration @EnableScheduling public class AppConfig { } @Service public class JobService implements SchedulingConfigurer { @Autowired private TaskScheduler taskScheduler; //schedule the task dynamically @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler(taskScheduler); //fails taskRegistrar.addTriggerTask(task, trigger); } } 

Mistake:

 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.scheduling.TaskScheduler; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 15 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ... 17 more 
+7
java spring spring-boot spring-scheduled
source share
1 answer

You do not need to install the scheduler on the ScheduledTaskRegistrar . If it has not been configured, the default is ConcurrentTaskScheduler , which wraps a single-threaded scheduled executor:

 if (this.taskScheduler == null) { this.localExecutor = Executors.newSingleThreadScheduledExecutor(); this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor); } 

If you are happy with this default scheduler, you can remove your autowiring TaskScheduler and call to set it to ScheduledTaskRegistrar . Also, as Marten suggested in the comments, you should make your SchedulingConfigurer a configuration class, not a service.

These changes leave your code similar to this:

 @Configuration static class TaskConfiguration implements SchedulingConfigurer { //schedule the task dynamically @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(task, trigger); } } 
+8
source share

All Articles