今天,排查了一个RuntimeException超时问题,简单记录分享下。
分析关键日志排查如下
查看关键代码
private static Client createClient(String wsdlUrl) {
JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
return jaxWsDynamicClientFactory.createClient(wsdlUrl);
}
发现,原来这是同事比较久,之前的代码,这相当于来一次就newInstance一次,于是改造下
private static final Map<String, Client> CLIENT_MAP = new ConcurrentHashMap<>();
/**
* 创建ws客户端
*
* @param wsdlUrl
* @return
*/
public static Client createClient(String wsdlUrl) {
Client client;
if (!CLIENT_MAP.containsKey(wsdlUrl)) {
synchronized (CLIENT_MAP) {
if (!CLIENT_MAP.containsKey(wsdlUrl)) {
JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
client = jaxWsDynamicClientFactory.createClient(wsdlUrl);
CLIENT_MAP.put(wsdlUrl, client);
} else {
client = CLIENT_MAP.get(wsdlUrl);
}
}
} else {
client = CLIENT_MAP.get(wsdlUrl);
}
return client;
}
用ConcurrentHashMap存储一下,每次都先从map里面拿,不难看出,其实这里和单例思想是一样的。