Spring-boot scheduler works without @EnableScheduling annotation

I used this example to create a scheduled task in a project example: https://spring.io/guides/gs/scheduling-tasks

It says: @EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

But, by mistake, I did not use it. Why is it still working?

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <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-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

Other classes:

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
   SpringApplication.run(DemoApplication.class, args);
  }
 }

@Component
public class ScheduledTaskRunner {

    Logger log = LoggerFactory.getLogger(ScheduledTaskRunner.class);

    @Scheduled(cron = "0/1 * * * * *")
    public void run(){
        log.info("Hello");
    }
}

Is the scheduled task planned?

+4
source share
1 answer

Added to your code spring-boot-starter-actuator, when it is added, a scheduler is added. This scheduler is used by the drive to schedule automatic export of key figures.

The code for this is in MetricExportAutoConfigurationand added in Spring Boot 1.3.0.

, , .

+7

Source: https://habr.com/ru/post/1673973/


All Articles