Spring Scheduler does not work
Asked Answered
A

15

32

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

}
Artiodactyl answered 27/1, 2011 at 14:14 Comment(2)
I get no errors and I expect to log the >>> Scheduled test service <<< message which doesn't happen...Artiodactyl
is your logger configured properly, with the proper log level?Bushranger
P
27

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="..." />
Pecten answered 22/6, 2012 at 14:8 Comment(1)
Is there a spring-boot equivalent for the application.conf?Alumna
B
81

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 ... **/
    }

Blameless answered 25/9, 2014 at 9:43 Comment(1)
adding @EnableScheduling saved my time a lot. Thanks very muchProsthodontics
P
27

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="..." />
Pecten answered 22/6, 2012 at 14:8 Comment(1)
Is there a spring-boot equivalent for the application.conf?Alumna
A
15

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.

Antislavery answered 5/5, 2016 at 13:2 Comment(0)
R
11

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

Rabbet answered 20/3, 2019 at 7:36 Comment(0)
M
7

After configuring the Schedulers, add @EnableScheduling in your main class. **enter image description here**

Marras answered 15/2, 2020 at 15:26 Comment(0)
A
5

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.

Artiodactyl answered 27/1, 2011 at 14:29 Comment(1)
This surely works since you leaved the task:annotation-driven approach. You my look at the other answer for the missing line. CheersVegetate
M
3

if you have dispatcher-servlet.xml move your configuration there. it worked for me and i have left a comment in this article: https://mcmap.net/q/453779/-why-isn-39-t-spring-running-my-scheduled-method

Metalliferous answered 24/7, 2012 at 14:35 Comment(0)
V
3

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
Vierra answered 7/5, 2018 at 6:40 Comment(0)
S
1

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

That solved my problem.

Shot answered 10/6, 2014 at 11:40 Comment(0)
E
1

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>
Epa answered 31/7, 2018 at 17:7 Comment(0)
P
0

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!

Pigmy answered 3/11, 2016 at 17:35 Comment(0)
A
0

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

Alcove answered 2/4, 2019 at 9:18 Comment(0)
H
0

Maybe it will be useful for someone. I ran into similar issue when I had a bean that did a long processing job in PostConstruct method. Thus, Spring Boot application didn't start (because PostConstruct method was in progress) and that's why my scheduled jobs didn't run (they start running after application startup).

Health answered 4/12, 2020 at 16:29 Comment(0)
I
0

If you are using Grails with Spring Scheduler you will need to add to the top of your class.

static lazyInit = false

Source

Ingrained answered 15/4, 2021 at 16:56 Comment(0)
F
0

Add @EnableScheduling on top of @SpringBootApplication and you need to make sure your class with @Scheduled is marked with @Service or @Component annotation.

Frottage answered 6/12, 2023 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.