gpt4 book ai didi

Spring-boot 调度程序在没有 @EnableScheduling 注释的情况下运行

转载 作者:行者123 更新时间:2023-12-03 12:44:58 24 4
gpt4 key购买 nike

我正在按照此示例在示例项目中创建计划任务:https://spring.io/guides/gs/scheduling-tasks

它说,@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
但是,我错误地没有使用它。怎么还能用?

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>

其他类(class):
@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");
}
}

计划任务不是应该不运行吗?

最佳答案

您的代码添加了 spring-boot-starter-actuator添加时,还会添加一个调度程序。执行器使用此调度程序来调度指标的自动导出。

此代码在 MetricExportAutoConfiguration 中并已在 Spring Boot 1.3.0 中添加。

因此,如果您删除执行器调度将不再起作用。

关于Spring-boot 调度程序在没有 @EnableScheduling 注释的情况下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43204377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com