package com.face.global.config;

import org.springframework.boot.autoconfigure.task.TaskExecutionProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableConfigurationProperties(TaskExecutionProperties.class)
public class TaskExecutorConfig {

    @Primary
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(TaskExecutionProperties taskExecutionProperties) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(taskExecutionProperties.getPool().getCoreSize());
        threadPoolTaskExecutor.setMaxPoolSize(taskExecutionProperties.getPool().getMaxSize());
        threadPoolTaskExecutor.setKeepAliveSeconds((int) taskExecutionProperties.getPool().getKeepAlive().toSeconds());
        threadPoolTaskExecutor.setQueueCapacity(taskExecutionProperties.getPool().getQueueCapacity());
        threadPoolTaskExecutor.setAllowCoreThreadTimeOut(taskExecutionProperties.getPool().isAllowCoreThreadTimeout());
        return threadPoolTaskExecutor;
    }
}
