Skip to content

Commit cedc583

Browse files
authored
Optimized RpcMessageHandler to reduce thread pool usage (#15163)
1 parent d4c1fb3 commit cedc583

File tree

1 file changed

+13
-7
lines changed
  • dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4

1 file changed

+13
-7
lines changed

dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/RpcMessageHandler.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,32 @@
1818

1919
import org.apache.dubbo.common.bytecode.NoSuchMethodException;
2020
import org.apache.dubbo.common.bytecode.Wrapper;
21+
import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
2122
import org.apache.dubbo.remoting.RemotingException;
2223
import org.apache.dubbo.remoting.exchange.ExchangeChannel;
2324
import org.apache.dubbo.remoting.exchange.support.Replier;
2425

2526
import java.lang.reflect.InvocationTargetException;
27+
import java.util.concurrent.ConcurrentHashMap;
28+
import java.util.concurrent.ConcurrentMap;
2629

2730
/**
2831
* RpcMessageHandler.
2932
*/
3033
public class RpcMessageHandler implements Replier<RpcMessage> {
34+
private static final ConcurrentMap<String, Object> IMPLEMENT_MAP = new ConcurrentHashMap<>();
3135
private static final ServiceProvider DEFAULT_PROVIDER = new ServiceProvider() {
3236
public Object getImplementation(String service) {
3337
String impl = service + "Impl";
34-
try {
35-
Class<?> cl = Thread.currentThread().getContextClassLoader().loadClass(impl);
36-
return cl.getDeclaredConstructor().newInstance();
37-
} catch (Exception e) {
38-
e.printStackTrace();
39-
}
40-
return null;
38+
return ConcurrentHashMapUtils.computeIfAbsent(IMPLEMENT_MAP, impl, (s) -> {
39+
try {
40+
Class<?> cl = Thread.currentThread().getContextClassLoader().loadClass(s);
41+
return cl.getDeclaredConstructor().newInstance();
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
return null;
46+
});
4147
}
4248
};
4349
private ServiceProvider mProvider;

0 commit comments

Comments
 (0)