英文:
Spring Boot Scheduler fixedRate has deviation with expected time period
问题
I am working on a sample spring boot app which calls a downstream API from scheduler method execution. I have used the fixedRate
variable under @Scheduled
with 3000ms value. While I run the application, the follow up executions are sometimes lesser than 3 seconds (like 2.99 or 2.84 sec). Can I know what needs to be changed in the code in order to have the method executed properly. Please find below the code:
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>
Configuration
class:
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
Console Output:
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
I have tried the same code in another machine and the deviation was only of 30ms, and the scheduler method was executing at 2.997 or 2.996 sec on average. So I am guessing that the deviation is dependent on hardware.
英文:
I am working on a sample spring boot app which calls a downstream API from scheduler method execution. I have used the fixedRate
variable under @Scheduled
with 3000ms value. While I run the application, the follow up executions are sometimes lesser than 3 seconds (like 2.99 or 2.84 sec). Can I know what needs to be changed in the code in order to have the method executed properly. Please find below the code:
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>
Configuration
class:
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
Console Output:
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
I have tried the same code in another machine and the deviation was only of 30ms and the scheduler method was executing at 2.997 or 2.996 sec on average. So I am guessing that the deviation is dependent on hardware.
答案1
得分: 2
我建议使用ScheduledThreadPoolExecutor
,它可以同时运行定时和多线程任务。
英文:
As you obviously want to run your tasks both scheduled and multi threaded I suggest to use ScheduledThreadPoolExecutor
which combine both concepts.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论