package cn.reghao.tnb.content.app.config;

import cn.reghao.tnb.content.app.util.rabbit.RabbitAppender;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @author reghao
 * @date 2025-07-18 09:17:28
 */
@Configuration
public class ThreadPoolConfig {
    @Bean(name = "taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(10_000);
        //executor.setRejectedExecutionHandler();
        executor.setThreadNamePrefix("MyExecutor-");

        // Set the TaskDecorator
        executor.setTaskDecorator(new MdcTaskDecorator());
        executor.initialize();
        return executor;
    }

    @Bean
    public RabbitAppender rabbitAppender(Environment env, RabbitTemplate rabbitTemplate) {
        String app = env.getProperty("spring.application.name");
        String host = "127.0.0.1";
        return new RabbitAppender(app, host, rabbitTemplate);
    }
}
