Files
smartDriveEE/src/main/java/com/rj/config/AppSchedulingConfiguration.java

37 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.rj.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.time.Clock;
/**
* 显式扩大 {@code @Scheduled} 线程池,避免单个耗时任务(如长时间 HTTP占满默认单线程后拖死全部定时任务。
* <p>与 {@code spring.task.scheduling.pool.size} 配置互补;此处代码保证至少 8 个调度线程。
*/
@Configuration
public class AppSchedulingConfiguration implements SchedulingConfigurer {
private static final int SCHEDULER_POOL_SIZE = 8;
@Bean
public Clock applicationClock() {
return Clock.systemDefaultZone();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(SCHEDULER_POOL_SIZE);
scheduler.setThreadNamePrefix("app-scheduling-");
scheduler.setWaitForTasksToCompleteOnShutdown(true);
scheduler.setAwaitTerminationSeconds(120);
scheduler.setRemoveOnCancelPolicy(true);
scheduler.initialize();
taskRegistrar.setTaskScheduler(scheduler);
}
}