java - Spring Boot @Scheduled 不起作用

标签 java spring maven tomcat schedule

我有一个非常简单的 Web 应用程序来测试 @Scheduled 注释。 RetrievePrices 类中的方法 read() 用 @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);
    }

}

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

一个非常简单的Spring配置类

package com.aaaa.config;

@Configuration
@ComponentScan("com.aaaa")
@EnableScheduling
public class MyConfiguration {
}

POM

<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>

更新的类 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 ************** ");
    }
}

最佳答案

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

例子:

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

//
} 

关于java - Spring Boot @Scheduled 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720667/

相关文章:

java - StringBuilder 和 HashSet 中的 OutOfMemoryError

java - JAXB,公共(public)字段映射行为与带有 @XmlElement 注释的私有(private)字段不同

java - 有没有办法使用 Matcher 来 peek() 或返回

java - 使用 Hibernate SQLQuery 的 AVG 函数中的 NullPointerException

spring - AWS Elastic Beanstalk - 配置我的 nginx 设置以增加 Java Spring maven 应用程序的超时

java - 如何获取执行jar的绝对路径?

java - 好的做法 : valid property vs. isValid() 方法?

java - Spring 在手动类实例化期间注入(inject)@Autowired 字段

java - 在 Spring 中使用抽象类作为实例变量

maven - 如何在 Build.scala 中包含带有分类器的依赖项