From 00ece645d0b321e01789f4b458508230df9bf761 Mon Sep 17 00:00:00 2001 From: zrlw Date: Thu, 20 Feb 2025 16:10:55 +0800 Subject: [PATCH] Optimized RpcMessageHandler to reduce thread pool usage --- .../transport/netty4/RpcMessageHandler.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java index 89099f8d6bff..5300ccfac233 100755 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java @@ -18,26 +18,32 @@ import org.apache.dubbo.common.bytecode.NoSuchMethodException; import org.apache.dubbo.common.bytecode.Wrapper; +import org.apache.dubbo.common.utils.ConcurrentHashMapUtils; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.exchange.ExchangeChannel; import org.apache.dubbo.remoting.exchange.support.Replier; import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; /** * RpcMessageHandler. */ public class RpcMessageHandler implements Replier { + private static final ConcurrentMap IMPLEMENT_MAP = new ConcurrentHashMap<>(); private static final ServiceProvider DEFAULT_PROVIDER = new ServiceProvider() { public Object getImplementation(String service) { String impl = service + "Impl"; - try { - Class cl = Thread.currentThread().getContextClassLoader().loadClass(impl); - return cl.getDeclaredConstructor().newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - return null; + return ConcurrentHashMapUtils.computeIfAbsent(IMPLEMENT_MAP, impl, (s) -> { + try { + Class cl = Thread.currentThread().getContextClassLoader().loadClass(s); + return cl.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + }); } }; private ServiceProvider mProvider;