Spring Scheduler does not work

情到浓时终转凉″ 提交于 2019-12-06 17:02:49

问题


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 <<<");
    }

}

回答1:


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="..." />



回答2:


Spring @Configuration (non-xml configuration) for annotation-driven tasks

Just add @EnableScheduling on your WebMvcConfig class

    @Configuration
    @EnableWebMvc
    @EnableAsync
    @EnableScheduling
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
       /** Annotations config Stuff ... **/
    }




回答3:


I finally found a solution.

application-context.xml

<bean id="schedulingTest" class="...SchedulingTest" />

<task:scheduled-tasks>
    <task:scheduled ref="schedulingTest" method="test" cron="* * * * * ?"/>
</task:scheduled-tasks>

and the test() method without the annotation. This runs the method every second and works perfectly.




回答4:


if you have dispatcher-servlet.xml move your configuration there. it worked for me and i have left a comment in this article: https://stackoverflow.com/a/11632536/546130




回答5:


This is happening because by default Spring lazy initializes the beans.

Disable lazy initialization for the bean by placing this annotation

@Lazy(false)

on top of your @Component.




回答6:


You should also check lazy-init to be false for that bean or use default-lazy-init="false" in beans.

That solved my problem.




回答7:


The solution for me was to add in the applicationContext.xml:

<task:annotation-driven/>

with the following schemaLocation:

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd



回答8:


For me the solution that worked in Spring 5 was that I had to add @Component to the class having @Scheduled annotated methods.




回答9:


We had the following reason: Service needed an interface (due to Transaction annotation) - IDE added this tx annotation also to interface. But @Scheduled was in implementing service class - and Spring ignored it since it thought that only annotations exist on the interface. So be careful to only have annotations on implementing classes!




回答10:


I had to update my dispatcher-servlet.xml with

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-4.3.xsd"></beans>

Bean definition below:

<bean id="scheduledTasks" class="com.vish.services.scheduler.ScheduledTasks"></bean>



回答11:


Just add @EnableScheduling at any spring boot configuration class annotated with @Configuration and for the method that run the schedule job add @Scheduled annotation.



来源:https://stackoverflow.com/questions/4817414/spring-scheduler-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!