Search code examples
springspring-bootquartz-schedulerschedulerspring-boot-actuator

How the scheduler is running without @EnableScheduling annotation in the project


I have a Spring Boot application, which is referring/depends on other Jar. The Jar is basically a Spring application which as a scheduler method in it.

I don't have any @EnableScheduling annotation on the Spring Boot application and in the external Jar also.

But as soon as I run the Spring Boot, the scheduler kicks in.

I am not understanding without @EnableScheduling annotation what is causing the scheduler to start execution.

i gone through the below answer from M. Deinum, he says @EnableScheduling will be added as part of springboot actuator (version 1.3.0).

Spring-boot scheduler runs without @EnableScheduling annotation

Yes I have a Spring Boot actuator in my classpath and the version of Spring Boot I am using is 2.0.3.

How can I discover which component is causing the scheduling to kick in? And the possible way to avoid the Scheduling.


Solution

  • Because you are using Spring Boot, it will configure the project automatically according to your class path and configuration classes. Then you just happened to have spring-boot-starter-actuator dependency in your class path, one of the feature in spring-boot-starter-actuator is scheduled tasks. (Actuator @Configuration class had already add @EnableScheduling, so Spring Boot will detect it and configure it to your project, after that your project will also have this feature either).

    But as Spring Boot 2, actuator does not enable the scheduling by default, I cannot replicate this issue in my environment, I'm using 2.2.5.RELEASE, so maybe update your spring boot version will help to avoid this.


    I cannot reproduced your case in my environment with spring boot 2.0.3. Below is my code snippet.

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://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>2.0.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.ouyang</groupId>
        <artifactId>Testing</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>Store</name>
        <description>Testing</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    Main Class

    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
    
            SpringApplication.run(Application.class, args);
    
        }
    
        @Scheduled(fixedDelay = 1000)
        public void test() {
    
            System.out.println("HI");
    
        }
    
    }
    

    @Scheduled method test() is not invoked.