SpringBoot @Scheduled 不起作用

use*_*141 6 java spring tomcat schedule maven

我有一个非常简单的 Web 应用程序,用于测试 @Scheduled 注释。方法读()RetrievePrices 类中的使用 @Scheduled 进行注释。在Tomcat上部署war后,我原以为read方法应该每5秒执行一次,但是Tomcat控制台什么也没显示。

主要的 SpringBoot 类

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

带有@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) ");
}
Run Code Online (Sandbox Code Playgroud)

一个非常简单的Spring配置类

package com.aaaa.config;

@Configuration
@ComponentScan("com.aaaa")
@EnableScheduling
public class MyConfiguration {
}
Run Code Online (Sandbox Code Playgroud)

聚甲醛

<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>
Run Code Online (Sandbox Code Playgroud)

更新的类 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 ************** ");
    }
}
Run Code Online (Sandbox Code Playgroud)

Uro*_* T. 6

您的RetrievePrices类没有任何类型的注释可供 Spring 扫描获取。添加@Component例如注释,它应该运行良好。

例子:

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

//
} 
Run Code Online (Sandbox Code Playgroud)


小智 5

在SpringBoot中创建一个简单的调度程序(工作代码)

  1. 应用类

    package com.springboot.test;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @EnableScheduling
    @SpringBootApplication
    public class TestApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 控制器类

    package com.springboot.test;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class SchedulerTest {
    
        @Scheduled(fixedRate=2000)
        public void name() {
        System.out.println("HI");   
            }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.springboot</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>test</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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-web</artifactId>
        </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>
    </project>
    
    Run Code Online (Sandbox Code Playgroud)

这个简单的测试代码对我有用。


lyr*_*rio 5

如果您使用 springBoot 2.4.x(Spring 5 版本),要让 @Scheduled 工作,您必须在 @SpringBootApplication 文件注释类的单独文件类中定义其方法。新类必须是一个组件,因此必须使用@Component进行注释,并在同一类级别添加注释@EnableScheduling,以便所有事情都能正常工作。希望这有帮助。