Tag Archives: @Scheduled

Springboot starts with @ scheduled and reports an error: no qualifying bean

 

Error:

2018-06-12 14:02:24,709 [main] DEBUG[org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor] – Could not find default TaskScheduler bean

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:374)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:203)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:182)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:87)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)

at com.richfit.ruiche.RichfitCBMRest.main(RichfitCBMRest.java:39)

2018-06-12 14:02:24,711 [main] DEBUG [org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor] – Could not find default ScheduledExecutorService bean

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.util.concurrent.ScheduledExecutorService] is defined

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:374)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:224)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:182)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:87)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)

at com.richfit.ruiche.RichfitCBMRest.main(RichfitCBMRest.java:39)

Error message analysis

The error message has two characteristics

1. the error message is a debug message.

2. The project eventually started normally, and the automated tasks in the project ran normally and were not affected.

Solution 1 – This exception log level is DEBUG, which does not affect normal usage and can be set on the log4j log level

Just add this line to the configuration under log4j

log4j.logger.org.springframework.scheduling = INFO

Effects – really no debug anymore, just printed as info

2018-06-12 14:09:39,340 [main] INFO [org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor] – No TaskScheduler/ScheduledExecutorService bean found for scheduled processing

Solution 2 – Since the error reminds that there is no [TaskScheduler], just add TaskScheduler to the SpringBoot configuration class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter{    
    
	@Bean
	public TaskScheduler scheduledExecutorService() {
		ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
		scheduler.setPoolSize(8);
		scheduler.setThreadNamePrefix("scheduled-thread-");
		return scheduler;
	}

}

– what’s the matter? What’s the meaning of reporting an error?Maybe spring hopes that technicians can specify components themselves

Spring’s timed task scheduler will try to get a registered task scheduler to do task scheduling. It will try to get a registered scheduler bean through the beanfactory.getbean method. The steps are as follows:

1. Try to find a taskscheduler bean from the configuration

2. Find the scheduled executorservice bean

3. Use the default scheduler

In the first two steps, if you can’t find it, you will throw an exception in the form of debug

logger.debug(“Could not find default TaskScheduler bean”, ex);

logger.debug(“Could not find default ScheduledExecutorService bean”, ex);

Therefore, the two exceptions printed out in the log are no error messages at all, and will not affect the use of timers. They are just some information printed by spring

★ the final solution

Both solutions are OK, and my final choice is the one in solution 2