Dynamically adjust thread pool core parameters

project

The apollo configuration is used to monitor whether the apollo configuration is modified. If there is a change, the modify parameter method of the thread pool is called.

key code

ThreadPoolTaskExecutor.setCorePoolSize(cpuCoreSize);
ThreadPoolTaskExecutor.setMaxPoolSize(maxCoreSize);

public static final String CPU_CORE_SIZE = "cpuCoreSize";
public static final String MAX_CORE_SIZE = "maxCoreSize";

@ApolloConfigChangeListener("")
public void doThreadChangeHandler(ConfigChangeEvent changeEvent) throws Exception {
    if(changeEvent.isChanged(CPU_CORE_SIZE) || changeEvent.isChanged(MAX_CORE_SIZE)) {
        ThreadPoolTaskExecutor asyncPool = ThreadPoolUtil.getAsyncPool();
        log.info("doThreadChangeHandler start : old cpuCoreSize : {},old maxPoolSize : {}", asyncPool.getCorePoolSize(), asyncPool.getMaxPoolSize());
        int cpuCoreSize = Integer.valueOf(changeEvent.getChange(CPU_CORE_SIZE).getNewValue());
        int maxCoreSize = Integer.valueOf(changeEvent.getChange(MAX_CORE_SIZE).getNewValue());
        ThreadPoolUtil.getAsyncPool().setCorePoolSize(cpuCoreSize);
        ThreadPoolUtil.getAsyncPool().setMaxPoolSize(maxCoreSize);
        log.info("doThreadChangeHandler start : new cpuCoreSize : {},new maxPoolSize : {}", asyncPool.getCorePoolSize(), asyncPool.getMaxPoolSize());
    }

}

Guess you like

Origin blog.csdn.net/qq_44961149/article/details/132448818