I'm a bit befuddled because I'm trying use use @Scheduled
annotations, but Spring doesn't seem to be finding my methods. The end result is that, none of my methods annotated with @Scheduled
are being executed.
I have invoked Spring's task magic with the following declarations:
<beans> <!-- XMLNS, XSD declarations omitted for brevity --> <context:component-scan base-package="com.mypackage"/> <task:executor id="executor" pool-size="5"/> <task:scheduler id="scheduler" pool-size="5"/> <task:annotation-driven scheduler="scheduler" executor="executor"/> </beans>
And I have an interface that looks something like this:
package com.mypackage; public interface MyInterface { public void executePeriodically(); }
With a corresponding impl like this:
package com.mypackage.impl; // imports omitted for brevity @Service public class MyInterfaceImpl implements MyInterface { @Scheduled(cron = "0/5 * * * * ?") public void executePeriodically() { System.out.println("The time is now : " + new Date()); } }
Now the expected result is that I have a very noisy little guy telling me what time it is every 5 seconds...but in reality I get absolutely nothing. I've tried with the annotation on the interface method and on the impl method, but that doesn't seem to change anything.
I know for sure that the executor and scheduler are being initialized because I have the following in my log:
INFO - ThreadPoolTaskExecutor - Initializing ExecutorService INFO - XmlWebApplicationContext - Bean 'executor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) INFO - XmlWebApplicationContext - Bean 'executor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) INFO - ThreadPoolTaskScheduler - Initializing ExecutorService 'scheduler' INFO - XmlWebApplicationContext - Bean 'scheduler' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
I'm not sure if that line about not being eligible is relevant or a red herring.
At the moment, I'm working around it by declaring my scheduled tasks like so:
<task:scheduled-tasks> <task:scheduled ref="sourceDocumentManagerImpl" method="deleteOldDocuments" cron="0 0 * * * ?"/> </task:scheduled-tasks>
While this works perfectly fine, I'd much rather use the annotations because it's so much more convenient to see directly in the code what the expectations are for that method. Anyone know what I could be doing wrong? For the record, I'm using Spring 3.0.4
Thanks a bunch!
The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.
There is no need to use @Async. Just use fixedRate attribute of @Scheduled instead of fixedDelay. Spring will make another invocation on the method after the given time regardless of any call is already being processed.
There is no limitation to call a scheduled method manually.
Spring Boot provides different scheduling functionalities in spring applications. You can schedule your tasks via @Scheduled annotation or via using a custom thread pool. @EnableScheduling annotation ensures that a background task executor is created with single thread.
add "task:annotation-driven" ?
<beans> <!-- XMLNS, XSD declarations omitted for brevity --> <context:component-scan base-package="com.mypackage"/> <task:annotation-driven/> <task:executor id="executor" pool-size="5"/> <task:scheduler id="scheduler" pool-size="5"/> <task:annotation-driven scheduler="scheduler" executor="executor"/> </beans>
reference http://howtodoinjava.com/2013/04/23/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/
or
Spring @Configuration (non-xml configuration) for annotation-driven tasks
Just add @EnableScheduling on you WebMvcConfig class
@Configuration @EnableWebMvc @EnableAsync @EnableScheduling public class WebMvcConfig extends WebMvcConfigurerAdapter { /** Annotations config Stuff ... **/ }
reference Spring Scheduler does not work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With