package com.multictrl.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * MQTT线程池配置 + Channel绑定
 */
@Configuration
public class MqttThreadConfig {

    @Bean("mqttExecutor1")
    public ExecutorService mqttExecutor1() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(200), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
        // JVM关闭时优雅关闭线程池
        Runtime.getRuntime().addShutdownHook(new Thread(executor::shutdown));
        return executor;
    }

    @Bean("mqttExecutor2")
    public ExecutorService mqttExecutor2() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(200), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
        // JVM关闭时优雅关闭线程池
        Runtime.getRuntime().addShutdownHook(new Thread(executor::shutdown));
        return executor;
    }
}
