SpringBoot @Scheduled doesn't work

user2023141 picture user2023141 · Feb 10, 2018 · Viewed 12k times · Source

I have a very simple web application for testing the @Scheduled annotation. The method read() in the class RetrievePrices is annotated with @Scheduled . After deploying the war on Tomcat, I was expecting that the read method should be executed every 5 seconds, but nothing is displayed in the Tomcat console.

The Main SpringBoot class

package com.aaaa.main;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
public class BatchMain {

    public static void main(String[] args) {

        SpringApplication.run(BatchMain.class, args);
    }

}

The Class whith a method annotated with @Scheduled

package com.aaaa.schedule;

import org.springframework.scheduling.annotation.Scheduled;

public class RetrievePrices {

    @Scheduled(fixedRate = 5000)
    public void read() {

        System.out.println(" *************  Scheduled(fixedRate = 5000) ");
}

A very simple Spring Configuration Class

package com.aaaa.config;

@Configuration
@ComponentScan("com.aaaa")
@EnableScheduling
public class MyConfiguration {
}

The POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    <artifactId>batch</artifactId>
    <name>Batch processes</name>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
        <Postgres.version>9.4-1200-jdbc41</Postgres.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>    
</project>

The updated class RetrievePrices

package com.aaaa.schedule;

import javax.annotation.PostConstruct;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class RetrievePrices {

    @Scheduled(fixedRate = 5000)
    public void read() {

        System.out.println(" *************  into @Scheduled(fixedRate = 5000) ");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println(" *************  postConstruct ************** ");
    }
}

Answer

Urosh T. picture Urosh T. · Feb 10, 2018

Your RetrievePrices class does not have any sort of annotation to get picked up by the Spring scanning. Add @Component annotation for example and it should run fine.

Example:

@SpringBootApplication(scanBasePackages = { "com.aaaa" })
@EnableScheduling
public class BootApplication {

//
}