Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Scheduler does not work

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 <<<");     }  } 
like image 450
user219882 Avatar asked Jan 27 '11 14:01

user219882


People also ask

How do I enable scheduling in spring?

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.

How do I start and stop a scheduler in spring boot?

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.


2 Answers

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 ... **/     }  
like image 132
ahll Avatar answered Sep 30 '22 11:09

ahll


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="..." /> 
like image 41
Serkan Arıkuşu Avatar answered Sep 30 '22 11:09

Serkan Arıkuşu