From 6b6066fba17e147b46807611acefba6ae106c5ef Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 20 May 2022 21:04:07 +0800 Subject: [PATCH] Dubbo thread pool adaptation logic optimization --- .../adapter/dubbo/DubboThreadPoolAdapter.java | 50 ++++++++----------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/hippo4j-adapter/hippo4j-adapter-dubbo/src/main/java/cn/hippo4j/adapter/dubbo/DubboThreadPoolAdapter.java b/hippo4j-adapter/hippo4j-adapter-dubbo/src/main/java/cn/hippo4j/adapter/dubbo/DubboThreadPoolAdapter.java index a8c1d73a..f8088f8a 100644 --- a/hippo4j-adapter/hippo4j-adapter-dubbo/src/main/java/cn/hippo4j/adapter/dubbo/DubboThreadPoolAdapter.java +++ b/hippo4j-adapter/hippo4j-adapter-dubbo/src/main/java/cn/hippo4j/adapter/dubbo/DubboThreadPoolAdapter.java @@ -53,61 +53,55 @@ public class DubboThreadPoolAdapter implements ThreadPoolAdapter, ApplicationLis @Override public ThreadPoolAdapterState getThreadPoolState(String identify) { ThreadPoolAdapterState threadPoolAdapterState = new ThreadPoolAdapterState(); - final ThreadPoolExecutor tp = DUBBO_PROTOCOL_EXECUTOR.get(identify); - if (tp == null) { + final ThreadPoolExecutor executor = DUBBO_PROTOCOL_EXECUTOR.get(identify); + if (executor == null) { return threadPoolAdapterState; } threadPoolAdapterState.setThreadPoolKey(identify); - threadPoolAdapterState.setActive(tp.getActiveCount() + ""); - threadPoolAdapterState.setCoreSize(tp.getCorePoolSize()); - threadPoolAdapterState.setMaximumSize(tp.getMaximumPoolSize()); + threadPoolAdapterState.setActive(executor.getActiveCount() + ""); + threadPoolAdapterState.setCoreSize(executor.getCorePoolSize()); + threadPoolAdapterState.setMaximumSize(executor.getMaximumPoolSize()); return threadPoolAdapterState; } @Override public List getThreadPoolStates() { List threadPoolAdapterStates = new ArrayList<>(); - DUBBO_PROTOCOL_EXECUTOR.forEach((k, v) -> threadPoolAdapterStates.add(getThreadPoolState(String.valueOf(k)))); + DUBBO_PROTOCOL_EXECUTOR.forEach((kel, val) -> threadPoolAdapterStates.add(getThreadPoolState(String.valueOf(val)))); return threadPoolAdapterStates; } @Override public boolean updateThreadPool(ThreadPoolAdapterParameter threadPoolAdapterParameter) { - final ThreadPoolExecutor tp = DUBBO_PROTOCOL_EXECUTOR.get(threadPoolAdapterParameter.getThreadPoolKey()); - if (tp == null) { + final ThreadPoolExecutor executor = DUBBO_PROTOCOL_EXECUTOR.get(threadPoolAdapterParameter.getThreadPoolKey()); + if (executor == null) { return false; } - tp.setCorePoolSize(threadPoolAdapterParameter.getCorePoolSize()); - tp.setMaximumPoolSize(threadPoolAdapterParameter.getMaximumPoolSize()); + executor.setCorePoolSize(threadPoolAdapterParameter.getCorePoolSize()); + executor.setMaximumPoolSize(threadPoolAdapterParameter.getMaximumPoolSize()); return true; } @Override public void onApplicationEvent(ApplicationStartedEvent event) { - boolean is2x = false; - String poolKey = "java.util.concurrent.ExecutorService"; + boolean is2xVersion = false; + String poolKey = ExecutorService.class.getName(); if (Version.getIntVersion(Version.getVersion()) < 3000000) { - is2x = true; + is2xVersion = true; } - - if (is2x) { - try { + try { + if (is2xVersion) { DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension(); Map executors = dataStore.get(poolKey); executors.forEach((key, value) -> DUBBO_PROTOCOL_EXECUTOR.put(key, (ThreadPoolExecutor) value)); - } catch (Exception e) { - log.error("Failed to get Dubbo 2.X protocol thread pool", e); - } - } else { - try { - final ExecutorRepository executorRepository = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); - final ConcurrentMap> data = (ConcurrentMap>) ReflectUtil.getFieldValue(executorRepository, "data"); - final ConcurrentMap executorServiceMap = data.get(poolKey); - executorServiceMap.forEach((key, value) -> DUBBO_PROTOCOL_EXECUTOR.put(String.valueOf(key), (ThreadPoolExecutor) value)); - } catch (Exception e) { - log.error("Failed to get Dubbo 3.X protocol thread pool", e); + return; } + ExecutorRepository executorRepository = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); + ConcurrentMap> data = (ConcurrentMap>) ReflectUtil.getFieldValue(executorRepository, "data"); + ConcurrentMap executorServiceMap = data.get(poolKey); + executorServiceMap.forEach((key, value) -> DUBBO_PROTOCOL_EXECUTOR.put(String.valueOf(key), (ThreadPoolExecutor) value)); + } catch (Exception ex) { + log.error("Failed to get Dubbo {}.X protocol thread pool", is2xVersion ? "2" : "3", ex); } - } }