调度程序如何在项目中没有@EnableScheduling批注的情况下运行

问题描述 投票:0回答:1

我有一个Spring Boot应用程序,它正在引用/依赖于其他Jar。Jar基本上是一个Spring应用程序,作为其中的调度程序方法。

我在SpringBoot应用程序和外部Jar中也没有任何@EnableScheduling注释。

但是一旦我运行Spring Boot,调度程序就会启动。

没有@EnableScheduling注释,我不明白是什么导致调度程序开始执行。

我听了M. Deinum的以下回答,他说@EnableScheduling将作为springboot执行器(版本1.3.0)的一部分添加。

Spring-boot scheduler runs without @EnableScheduling annotation

是的,我在我的类路径中有一个springboot执行器,我正在使用的Spring Boot版本是2.0.3。

任何人都可以帮助我以某种方式了解引起调度的因素。

以及避免计划的可能方法。

spring spring-boot quartz-scheduler scheduler spring-boot-actuator
1个回答
0
投票

因为您使用的是Spring Boot,它将根据您的类路径和配置类自动配置项目。然后,您碰巧在类路径中具有spring-boot-starter-actuator依赖项,spring-boot-starter-actuator的功能之一是计划任务。 (执行器@Configuration类已经添加了@EnableScheduling,因此Spring Boot将检测到它并将其配置到您的项目中,之后您的项目也将具有此功能)。

但是,由于Spring Boot 2执行器默认情况下不启用调度,因此我无法在我的环境中复制此问题,我使用的是2.2.5.RELEASE,因此也许更新Spring Boot版本将有助于避免这种情况。 >


我无法在我的环境中使用Spring Boot 2.0.3复制您的案例。下面是我的代码段。

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>

主类

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

    }

}

@计划方法test()未调用。

© www.soinside.com 2019 - 2024. All rights reserved.