我有一个非常简单的Web应用程序来测试@Scheduled注释。 RetrievePrices类中的方法 read()使用@Scheduled进行注释。在Tomcat上部署战争之后,我希望每隔5秒执行一次read方法,但Tomcat控制台中不会显示任何内容。
Main 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 ************** ");
}
}
答案 0 :(得分:2)
在SpringBoot中创建一个简单的调度程序(工作代码)
应用程序类
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);
}
}
控制器类
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");
}
}
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>
这个简单的测试代码对我有用。
答案 1 :(得分:1)
您的RetrievePrices
类没有任何类型的注释可以通过Spring扫描获取。例如,添加@Component
注释,它应该运行正常。
示例:
@SpringBootApplication(scanBasePackages = { "com.aaaa" })
@EnableScheduling
public class BootApplication {
//
}
答案 2 :(得分:1)
我遇到了类似的问题,我觉得我应该提一下对我有用的东西,它可能对别人有所帮助。
我添加了@EnableScheduling,组件扫描很好,来源检查不错,但有一个问题,该项目是一个继承的groovy项目,我不知道我需要将我的项目源文件夹标记为'sources root'在intellij中,这在intellij的java项目中是不必要的。因此,如果您已经完成了检查构建工具或IDE所需的所有操作,以确保您的源根目录已正确标记。
答案 3 :(得分:1)
如果使用springBoot 2.4.x(Spring 5版本),要使@Scheduled正常工作,则必须在与@SpringBootApplication文件注释类不同的文件类中定义其方法。 新类必须是一个组件,因此必须用@Component进行注释,还必须在同一类级别上添加@EnableScheduling注释,以使所有工作正常进行。 希望这会有所帮助。