Spring Boot Scheduler fixerate与预期时间段有偏差



我正在研究一个示例spring启动应用程序,该应用程序从调度程序方法执行调用下游API。我在@Scheduled下使用了具有3000ms值的fixedRate变量。当我运行应用程序时,后续执行有时少于3秒(如2.99或2.84秒)。我可以知道什么需要改变的代码,以便有正确执行的方法。请在代码下面查找:

SchedulerDemoApplication

package edu.demo.scheduler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class SchedulerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulerDemoApplication.class, args);
}
}

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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>edu.demo.scheduler</groupId>
<artifactId>SchedulerDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SchedulerDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

Configurationclass:

package edu.demo.scheduler.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@Configuration
public class SchedulerConfiguration {
@Value("${spring.task.scheduling.pool.size}")
int poolSize;

@Bean("scheduler")
public TaskScheduler taskScheduler() {

ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(poolSize);
scheduler.setThreadNamePrefix("PoolScheduler");
return scheduler;
}
}

SchedulerService .

package edu.demo.scheduler.component;
import java.time.LocalDateTime;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@NoArgsConstructor
public class SchedulerService {
@Scheduled(fixedRate = 3000)
@Async("scheduler")
public void taskScheduler() {

System.out.println(Thread.currentThread().getName() + " - " + " Starting: " + LocalDateTime.now());
try { Thread.sleep(5000); }catch (Exception e) {}   //  Dummy downstream call
System.out.println(Thread.currentThread().getName() + " - " + " Running: " + LocalDateTime.now());
}

}

application.properties

logging.level.web=DEBUG
spring.task.scheduling.pool.size=5

控制台输出:

PoolScheduler1 -  Starting: 2023-04-06T17:16:26.900489100
PoolScheduler3 -  Starting: 2023-04-06T17:16:29.901791600
PoolScheduler1 -  Running: 2023-04-06T17:16:31.905599700
PoolScheduler2 -  Starting: 2023-04-06T17:16:32.899475300
PoolScheduler3 -  Running: 2023-04-06T17:16:34.911002700
PoolScheduler4 -  Starting: 2023-04-06T17:16:35.891940200
PoolScheduler2 -  Running: 2023-04-06T17:16:37.911668800
PoolScheduler4 -  Running: 2023-04-06T17:16:40.900844100

我在另一台机器上尝试了相同的代码,偏差只有30毫秒,调度器方法平均执行2.997或2.996秒。所以我猜偏差取决于硬件。

由于您显然希望同时运行计划和多线程任务,我建议使用结合这两个概念的ScheduledThreadPoolExecutor

最新更新