package org.xyzh.crontab.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * @description 定时任务配置
 * @filename SchedulerConfig.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-25
 */
@Configuration
@EnableScheduling
public class SchedulerConfig {

    /**
     * @description 配置任务调度线程池
     * @return TaskScheduler
     * @author yslg
     * @since 2025-10-25
     */
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 设置线程池大小
        scheduler.setPoolSize(10);
        // 设置线程名称前缀
        scheduler.setThreadNamePrefix("crontab-task-");
        // 设置是否等待所有任务完成后再关闭
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        // 设置等待时间（秒）
        scheduler.setAwaitTerminationSeconds(60);
        // 设置拒绝策略
        scheduler.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        scheduler.initialize();
        
        return scheduler;
    }
}

