package com.jpsoft.shinestar.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import javax.annotation.PreDestroy;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Slf4j
@EnableScheduling
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    private ScheduledExecutorService threadPool;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        threadPool = Executors.newScheduledThreadPool(10);

        //当然了，这里设置的线程池是corePoolSize也是很关键了，自己根据业务需求设定
        taskRegistrar.setScheduler(threadPool);
    }

    @PreDestroy
    public void shutdown(){
        log.warn("关闭定时任务线程");

        if(threadPool!=null){
            threadPool.shutdown();
            log.warn("关闭定时任务线程成功!");
        }
    }
}