31

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

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user219882
  • 15,274
  • 23
  • 93
  • 138

14 Answers14

79

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

jasxir
  • 808
  • 9
  • 18
ahll
  • 2,329
  • 1
  • 19
  • 22
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="..." />
Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
13

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.

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
9

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

nabeel
  • 305
  • 2
  • 7
8

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

pasindupa
  • 689
  • 9
  • 10
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.

user219882
  • 15,274
  • 23
  • 93
  • 138
  • 1
    This surely works since you leaved the `task:annotation-driven` approach. You my look at the other answer for the missing line. Cheers – Serkan Arıkuşu Jun 22 '12 at 14:10
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://stackoverflow.com/a/11632536/546130

Community
  • 1
  • 1
Hazhir
  • 780
  • 1
  • 6
  • 18
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
Joao Gavazzi
  • 1,818
  • 18
  • 8
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.

Amir M
  • 354
  • 4
  • 16
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>
joce
  • 9,624
  • 19
  • 56
  • 74
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!

Strinder
  • 2,111
  • 2
  • 21
  • 34
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.

Hany Sakr
  • 2,591
  • 28
  • 27
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).

0

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

static lazyInit = false

Source

Dylan
  • 2,161
  • 2
  • 27
  • 51