Using @Scheduled and @EnableScheduling but gives NoSuchBeanDefinitionException

only no-arg methods may be annotated with @scheduled
spring task scheduler
enablescheduling xml
scheduled not working
spring boot scheduler shutdown
the annotation scheduled is disallowed for this location
unsupported scheduler type class=org springframework scheduling concurrent threadpooltaskexecutor
spring scheduler multiple threads

I have followed very simple examples online to set up a cron job in Spring yet I keep getting this error in my Tomcat startup log each and every time:

2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:191 - 
Could not find default TaskScheduler bean org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined

2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:202 - Could not    
find default ScheduledExecutorService bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying    
bean of type [org.springframework.scheduling.TaskScheduler] is defined

And the 2 java classes used to implement the cron:

1) The @Configuration class:

@Configuration
@EnableScheduling
public class ClearTokenStoreCronEnable {    
  final static Logger log =   
  LoggerFactory.getLogger(ClearTokenStoreCronEnable.class);
  private @Autowired TokenStoreRepository tokenStoreRepository; 
}

and Cron job class:

@Service
public class ClearTokenStoreWorkerService {

    final static Logger log = LoggerFactory.getLogger(ClearTokenStoreWorkerService.class);
    private @Autowired TokenStoreRepository tokenStoreRepository;

    //@Scheduled(fixedDelay=5000)
    //run daily at midnight
    @Scheduled(cron = "0 0 * * * *")
    public void tokenStoreTable() {
        log.debug("tokenstore table truncated - start");
        tokenStoreRepository.deleteAll();
        log.debug("tokenstore table truncated - end");
    }
}

As a side note, the cron job runs at midnight but it also seems to run randomly at other times. Not sure if this is a bug or my cron expression is wrong: @Scheduled(cron = "0 0 * * * *")

My main concern at this time is why am I getting ScheduledAnnotationBeanPostProcessor errors? It's looking for a TaskScheduler and ScheduledExectorService. I just need to fire this once a day. I am not doing any concurrent processing or where I need multiple threads. Ultimately are these errors harmful OR do I need to fix them?

according to exception Info "Could not find default TaskScheduler bean", the config should define "TaskScheduler" rather than "Executor"

@Configuration
public class AppContext extends WebMvcConfigurationSupport {
    @Bean
    public TaskScheduler taskScheduler() {
        return new ConcurrentTaskScheduler();
    }

    // Of course , you can define the Executor too
    @Bean
    public Executor taskExecutor() {
        return new SimpleAsyncTaskExecutor();
   }

}

Spring Framework, Spring Framework - @Scheduled and @EnableScheduling Examples. Spring Framework. @Scheduled marks a method to be scheduled per provided fixedDelay, method 'runTask': Only no-arg methods may be annotated with @​Scheduled Spring Scheduler Example Using @Scheduled And @EnableScheduling Annotations. By Yashwant Chavan, Views 33101, Last updated on 20-Feb-2019. In last article we have covered Spring Scheduler Using Xml Configuration,now we will implement same thing using @Scheduled And @EnableScheduling annotations.

EDIT: the best answer is here and it involves creating an Executor:

@Configuration
@EnableAsync
public class AppContext extends WebMvcConfigurationSupport {
    @Bean
    public Executor taskExecutor() {
        return new SimpleAsyncTaskExecutor();
    }
}

PREVIOUS (still valid though):

The NoSuchBeanDefinitionException is logged with a DEBUG severity and can be safely ignored. If you look at the source code for ScheduledAnnotationBeanPostProcessor, you see that it first tries to get a TaskScheduler, then a ScheduledExecutorService, then it keeps going on "falling back to default scheduler":

    if (this.registrar.hasTasks() && this.registrar.getScheduler() == null) {
        Assert.state(this.beanFactory != null, "BeanFactory must be set to find scheduler by type");
        try {
            // Search for TaskScheduler bean...
            this.registrar.setScheduler(this.beanFactory.getBean(TaskScheduler.class));
        }
        catch (NoUniqueBeanDefinitionException ex) {
            throw new IllegalStateException("More than one TaskScheduler exists within the context. " +
                    "Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " +
                    "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback.", ex);
        }
        catch (NoSuchBeanDefinitionException ex) {
            logger.debug("Could not find default TaskScheduler bean", ex);
            // Search for ScheduledExecutorService bean next...
            try {
                this.registrar.setScheduler(this.beanFactory.getBean(ScheduledExecutorService.class));
            }
            catch (NoUniqueBeanDefinitionException ex2) {
                throw new IllegalStateException("More than one ScheduledExecutorService exists within the context. " +
                        "Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " +
                        "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback.", ex);
            }
            catch (NoSuchBeanDefinitionException ex2) {
                logger.debug("Could not find default ScheduledExecutorService bean", ex);
                // Giving up -> falling back to default scheduler within the registrar...
            }
        }
    }

You can remove the exception by setting at least a INFO severity on org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor, like

<logger name="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" level="INFO"/>

when using logback.

The cron expression has six fields:

second (0-59), minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (1-7, 1 = Sunday)

The syntax can be found in the quartz docs. I'm not sure about the "?" character because, although the page says

The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify "no specific value". This is useful when you need to specify something in one of the two fields, but not the other.

the examples on that page actually use ? even when the other field is *. IMHO all should work with just *, so in order to execute every midnight, the expression should be

0 0 0 * * *

EnableScheduling (Spring Framework 5.2.6.RELEASE API), @Configuration @EnableScheduling public class AppConfig { // various @Bean Methods annotated with @Scheduled may even be declared directly within This is but one demonstration how the code-based approach allows for maximum​  To schedule job in spring boot application to run periodically, spring boot provides @EnableScheduling and @Scheduled annotations. Lets learn to use Spring boot @Scheduled annotation. Let’s say you want to run job at every 10 seconds interval. You can achieve this job scheduling in below steps: 1. Add @EnableScheduling to Spring Boot

For solving this problem just create task scheduler bean in config.

@Bean
    public TaskScheduler taskScheduler() {
        return new ConcurrentTaskScheduler();
    }

java - @Scheduled y @EnableScheduling pero da , El NoSuchBeanDefinitionException se registra con una severidad de depuración y se + "Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " + bean", ex); // Giving up -> falling back to default scheduler within the registrar. Licensed under cc by-sa 3.0 with attribution required. Spring Framework - @Scheduled and @EnableScheduling Examples Spring Framework @Scheduled marks a method to be scheduled per provided fixedDelay, fixedRate or cron parameters.

I agree you can ignore it but just changing the severity will not fix it. I had the same issue but I am using xml instead of annotations, and in my case it happened because I did not included the executor on my bean definition. So adding this fixed it:

<task:annotation-driven executor="myExecutor"
    scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />

I hope it helps.

Regards.

java, Usando @Scheduled y @EnableScheduling pero da NoSuchBeanDefinitionException: No qualifying bean of type javacron​scheduled-tasksspring-4 + "Remove all but one of the beans; or implement the SchedulingConfigurer interface bean", ex); // Giving up -> falling back to default scheduler within the registrar. Because the intervals are specified using the @Scheduled annotation, the intervals are typically static and cannot change over the life of an application. In this tutorial, we'll look at various ways to conditionally enable Spring scheduled jobs.

With Spring Boot 2.0.5, I keep getting:

2018-11-20 11:35:48.046  INFO 64418 --- [  restartedMain] s.a.ScheduledAnnotationBeanPostProcessor : 
No TaskScheduler/ScheduledExecutorService bean found for scheduled processing

The only way to get rid of it seems to be using SchedulingConfigurer interface in your @Configuration class like this:

@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
    private final int POOL_SIZE = 10;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();

        threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
        threadPoolTaskScheduler.setThreadNamePrefix("my-scheduled-task-pool-");
        threadPoolTaskScheduler.initialize();

        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}

Note: This was taken from https://www.callicoder.com/spring-boot-task-scheduling-with-scheduled-annotation/

Spring Boot Batch Job + Scheduler Example, This post explains how to run the batch job through spring scheduler using EnableScheduling; @Configuration @EnableScheduling public  The beans which need to be scheduled must have @Scheduled annotaion. We need to configure schedule time. It is configured as @Scheduled(fixedRate=2000). This configuration means a thread will run the task every after two second. Find the link for task scheduler using XML configuration. Find the example for @EnableScheduling below. Task.java

org.springframework > spring-context > 5.1.6.RELEASE > org , package org.springframework.scheduling.annotation; import java.lang.reflect. NoSuchBeanDefinitionException; import org.springframework.beans.factory. See the @{@link EnableScheduling} javadocs for complete usage details. giving other ContextRefreshedEvent listeners a chance to perform // their work at the  We can run a scheduled task using Spring's @Scheduled annotation but based on the properties fixedDelay and fixedRate the nature of execution changes. The fixedDelay property makes sure that there is a delay of n millisecond between the finish time of an execution of a task and the start time of the next execution of the task.

Using @Scheduled and @EnableScheduling but gives NoSuchBeanDefinitionException: No qualifying bean of type  @Configuration @EnableScheduling public class AppConfig { @Scheduled(fixedRate=1000) public void work() { // task execution logic } } By default, will be searching for an associated scheduler definition: either a unique TaskScheduler bean in the context, or a TaskScheduler bean named "taskScheduler" otherwise; the same lookup will also be

Spring provides excellent support for both task scheduling and asynchronous method execution based on cron expression using @Scheduled annotation. The @Scheduled annotation can be added to a method along with trigger metadata. In this post, I will show the means to use @Scheduled feature in 4 different ways. Read More : Spring timer tasks Table […]

Comments
  • The initial cron expression above I was using I found was not right. The right expression for firing once a day at midnight is: @Scheduled(cron = "0 0 0 * * ?")
  • Strangely, POOL_SIZE = 1; do not seem to work, though poolSize can be 1 or higher.