From 7467ea634ccaa201dd00f922a75cb6fd30fcf7c9 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 19 Aug 2022 22:59:41 +0800 Subject: [PATCH] Code formatting optimization --- .../rabbitmq/RabbitMQThreadPoolAdapter.java | 150 +++++++++--------- 1 file changed, 74 insertions(+), 76 deletions(-) diff --git a/hippo4j-adapter/hippo4j-adapter-rabbitmq/src/main/java/cn/hippo4j/adapter/rabbitmq/RabbitMQThreadPoolAdapter.java b/hippo4j-adapter/hippo4j-adapter-rabbitmq/src/main/java/cn/hippo4j/adapter/rabbitmq/RabbitMQThreadPoolAdapter.java index 63957034..ba7521a8 100644 --- a/hippo4j-adapter/hippo4j-adapter-rabbitmq/src/main/java/cn/hippo4j/adapter/rabbitmq/RabbitMQThreadPoolAdapter.java +++ b/hippo4j-adapter/hippo4j-adapter-rabbitmq/src/main/java/cn/hippo4j/adapter/rabbitmq/RabbitMQThreadPoolAdapter.java @@ -17,12 +17,6 @@ package cn.hippo4j.adapter.rabbitmq; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.ThreadPoolExecutor; - import cn.hippo4j.adapter.base.ThreadPoolAdapter; import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; import cn.hippo4j.adapter.base.ThreadPoolAdapterState; @@ -31,11 +25,16 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; - import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ThreadPoolExecutor; + import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_DELIMITER; /** @@ -45,73 +44,72 @@ import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_DELIMI @RequiredArgsConstructor public class RabbitMQThreadPoolAdapter implements ThreadPoolAdapter, ApplicationListener { - private static final String RABBITMQ = "RabbitMQ"; - - private static final String FiledName = "executorService"; - - private final Map abstractConnectionFactoryMap; - - private final Map RABBITMQ_THREAD_POOL_TASK_EXECUTOR = Maps.newHashMap(); - - @Override - public String mark() { - return RABBITMQ; - } - - @Override - public ThreadPoolAdapterState getThreadPoolState(String identify) { - ThreadPoolAdapterState threadPoolAdapterState = new ThreadPoolAdapterState(); - ThreadPoolExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(identify); - threadPoolAdapterState.setThreadPoolKey(identify); - if (Objects.nonNull(threadPoolTaskExecutor)) { - threadPoolAdapterState.setCoreSize(threadPoolTaskExecutor.getCorePoolSize()); - threadPoolAdapterState.setMaximumSize(threadPoolTaskExecutor.getMaximumPoolSize()); - } - return threadPoolAdapterState; - } - - @Override - public List getThreadPoolStates() { - List adapterStateList = Lists.newArrayList(); - RABBITMQ_THREAD_POOL_TASK_EXECUTOR.forEach( - (key, val) -> adapterStateList.add(getThreadPoolState(key))); - return adapterStateList; - } - - @Override - public boolean updateThreadPool(ThreadPoolAdapterParameter threadPoolAdapterParameter) { - String threadPoolKey = threadPoolAdapterParameter.getThreadPoolKey(); - ThreadPoolExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(threadPoolKey); - if (Objects.nonNull(threadPoolTaskExecutor)) { - int originalCoreSize = threadPoolTaskExecutor.getCorePoolSize(); - int originalMaximumPoolSize = threadPoolTaskExecutor.getMaximumPoolSize(); - threadPoolTaskExecutor.setMaximumPoolSize(threadPoolAdapterParameter.getMaximumPoolSize()); - threadPoolTaskExecutor.setCorePoolSize(threadPoolAdapterParameter.getCorePoolSize()); - log.info("[{}] Rabbitmq consumption thread pool parameter change. coreSize: {}, maximumSize: {}", - threadPoolKey, - String.format(CHANGE_DELIMITER, originalCoreSize, threadPoolAdapterParameter.getCorePoolSize()), - String.format(CHANGE_DELIMITER, originalMaximumPoolSize, threadPoolAdapterParameter.getMaximumPoolSize())); - return true; - } - log.warn("[{}] Rabbitmq consuming thread pool not found.", threadPoolKey); - return false; - } - - @Override - public void onApplicationEvent(ApplicationStartedEvent event) { - abstractConnectionFactoryMap.forEach((beanName, abstractConnectionFactor) -> { - ExecutorService executor = (ExecutorService) ReflectUtil.getFieldValue(abstractConnectionFactor, FiledName); - if (Objects.nonNull(executor)) { - if (executor instanceof ThreadPoolExecutor) { - ThreadPoolExecutor threadPoolTaskExecutor = (ThreadPoolExecutor) executor; - RABBITMQ_THREAD_POOL_TASK_EXECUTOR.put(beanName, threadPoolTaskExecutor); - log.info("Rabbitmq executor name {}", beanName); - } - else { - log.warn("Custom thread pools only support ThreadPoolExecutor"); - } - } - - }); - } + private static final String RABBITMQ = "RabbitMQ"; + + private static final String FiledName = "executorService"; + + private final Map abstractConnectionFactoryMap; + + private final Map RABBITMQ_THREAD_POOL_TASK_EXECUTOR = Maps.newHashMap(); + + @Override + public String mark() { + return RABBITMQ; + } + + @Override + public ThreadPoolAdapterState getThreadPoolState(String identify) { + ThreadPoolAdapterState threadPoolAdapterState = new ThreadPoolAdapterState(); + ThreadPoolExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(identify); + threadPoolAdapterState.setThreadPoolKey(identify); + if (Objects.nonNull(threadPoolTaskExecutor)) { + threadPoolAdapterState.setCoreSize(threadPoolTaskExecutor.getCorePoolSize()); + threadPoolAdapterState.setMaximumSize(threadPoolTaskExecutor.getMaximumPoolSize()); + } + return threadPoolAdapterState; + } + + @Override + public List getThreadPoolStates() { + List adapterStateList = Lists.newArrayList(); + RABBITMQ_THREAD_POOL_TASK_EXECUTOR.forEach( + (key, val) -> adapterStateList.add(getThreadPoolState(key))); + return adapterStateList; + } + + @Override + public boolean updateThreadPool(ThreadPoolAdapterParameter threadPoolAdapterParameter) { + String threadPoolKey = threadPoolAdapterParameter.getThreadPoolKey(); + ThreadPoolExecutor threadPoolTaskExecutor = RABBITMQ_THREAD_POOL_TASK_EXECUTOR.get(threadPoolKey); + if (Objects.nonNull(threadPoolTaskExecutor)) { + int originalCoreSize = threadPoolTaskExecutor.getCorePoolSize(); + int originalMaximumPoolSize = threadPoolTaskExecutor.getMaximumPoolSize(); + threadPoolTaskExecutor.setMaximumPoolSize(threadPoolAdapterParameter.getMaximumPoolSize()); + threadPoolTaskExecutor.setCorePoolSize(threadPoolAdapterParameter.getCorePoolSize()); + log.info("[{}] Rabbitmq consumption thread pool parameter change. coreSize: {}, maximumSize: {}", + threadPoolKey, + String.format(CHANGE_DELIMITER, originalCoreSize, threadPoolAdapterParameter.getCorePoolSize()), + String.format(CHANGE_DELIMITER, originalMaximumPoolSize, threadPoolAdapterParameter.getMaximumPoolSize())); + return true; + } + log.warn("[{}] Rabbitmq consuming thread pool not found.", threadPoolKey); + return false; + } + + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + abstractConnectionFactoryMap.forEach((beanName, abstractConnectionFactor) -> { + ExecutorService executor = (ExecutorService) ReflectUtil.getFieldValue(abstractConnectionFactor, FiledName); + if (Objects.nonNull(executor)) { + if (executor instanceof ThreadPoolExecutor) { + ThreadPoolExecutor threadPoolTaskExecutor = (ThreadPoolExecutor) executor; + RABBITMQ_THREAD_POOL_TASK_EXECUTOR.put(beanName, threadPoolTaskExecutor); + log.info("Rabbitmq executor name {}", beanName); + } else { + log.warn("Custom thread pools only support ThreadPoolExecutor"); + } + } + + }); + } }