I have a problem with Spring's annotation based task scheduler - I can't get it working, I don't see any problem here...
application-context.xml
<task:scheduler id="taskScheduler" /> <task:executor id="taskExecutor" pool-size="1" /> <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />
bean
@Service public final class SchedulingTest { private static final Logger logger = Logger.getLogger(SchedulingTest.class); @Scheduled(fixedRate = 1000) public void test() { logger.debug(">>> Scheduled test service <<<"); } }
Java Cron ExpressionThe @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. The @Scheduled annotation is used to trigger the scheduler for a specific time period.
The schedulers do not start or stop. In the real world, it is necessary to stop and restart the scheduler without restarting the spring boot application. The ScheduledAnnotationBeanPostProcessor class allows you to programmatically start and stop the scheduler without having to restart the spring boot application.
Spring @Configuration (non-xml configuration) for annotation-driven tasks
Just add @EnableScheduling on your WebMvcConfig class
import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc @EnableAsync @EnableScheduling public class WebMvcConfig implements WebMvcConfigurer { /** Annotations config Stuff ... **/ }
If you want to use task:annotation-driven
approach and your @Scheduled annotation is not working, then you most probably missed context:component-scan
in your context xml. Without this line, spring cannot guess where to search for your annotations.
<context:component-scan base-package="..." />
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