配置中增加关闭连接response.close()

This commit is contained in:
zhanghongqing
2018-11-21 15:14:28 +08:00
parent f5e3b17761
commit 1f0aafb4bf
3 changed files with 94 additions and 3 deletions

View File

@@ -134,13 +134,25 @@ public class ConfigServiceUtil {
if(!StringUtil.isEmpty(bean.getData())){ if(!StringUtil.isEmpty(bean.getData())){
list = bean.getData().getPzIdList(); list = bean.getData().getPzIdList();
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
} }
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
} }
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
long end=System.currentTimeMillis(); long end=System.currentTimeMillis();
logger.warn("get ids finish,cost:"+(end-start)); logger.warn("get ids finish,cost:"+(end-start));
@@ -170,7 +182,13 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result,config); JSONObject resObject = JSONObject.fromObject(result,config);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
return bean; return bean;
} }
@@ -195,7 +213,13 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result); JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
return bean; return bean;
@@ -221,6 +245,9 @@ public class ConfigServiceUtil {
try { try {
response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType())); response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
} catch (Exception e) { } catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
if(response != null && response.getStatus() == 200){ if(response != null && response.getStatus() == 200){
@@ -228,7 +255,13 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result); JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
if (response != null) {
response.close();
} }
return bean; return bean;
@@ -257,6 +290,9 @@ public class ConfigServiceUtil {
try { try {
response= header.delete(); response= header.delete();
} catch (Exception e) { } catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
if(response != null && response.getStatus() == 200){ if(response != null && response.getStatus() == 200){
@@ -264,7 +300,13 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result); JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
return bean; return bean;
@@ -295,6 +337,9 @@ public class ConfigServiceUtil {
try { try {
response= header.put(Entity.entity(params, MediaType.APPLICATION_JSON)); response= header.put(Entity.entity(params, MediaType.APPLICATION_JSON));
} catch (Exception e) { } catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
if(response !=null && response.getStatus() == 200){ if(response !=null && response.getStatus() == 200){
@@ -302,7 +347,13 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result); JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
return bean; return bean;
} }
@@ -360,6 +411,9 @@ public class ConfigServiceUtil {
try { try {
response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType())); response= header.post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()));
} catch (Exception e) { } catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
if(response !=null && response.getStatus() == 200){ if(response !=null && response.getStatus() == 200){
@@ -368,7 +422,13 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result); JSONObject resObject = JSONObject.fromObject(result);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
return bean; return bean;
} }
@@ -393,6 +453,9 @@ public class ConfigServiceUtil {
try { try {
response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON)); response= header.post(Entity.entity(params, MediaType.APPLICATION_JSON));
} catch (Exception e) { } catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
if(response !=null && response.getStatus() == 200){ if(response !=null && response.getStatus() == 200){
@@ -402,7 +465,13 @@ public class ConfigServiceUtil {
JSONObject resObject = JSONObject.fromObject(result,config); JSONObject resObject = JSONObject.fromObject(result,config);
bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class); bean = (ToMaatResult) JSONObject.toBean(resObject,ToMaatResult.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
long end=System.currentTimeMillis(); long end=System.currentTimeMillis();
logger.warn("postGroupReuseSources end,cost:"+(end-start)); logger.warn("postGroupReuseSources end,cost:"+(end-start));
@@ -449,12 +518,21 @@ public class ConfigServiceUtil {
try { try {
response= header.get(); response= header.get();
} catch (Exception e) { } catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
if(response !=null && response.getStatus() == 200){ if(response !=null && response.getStatus() == 200){
result= response.readEntity(String.class); result= response.readEntity(String.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
return result; return result;
} }
@@ -503,12 +581,21 @@ public class ConfigServiceUtil {
try { try {
response= header.get(); response= header.get();
} catch (Exception e) { } catch (Exception e) {
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
} }
if(response !=null && response.getStatus() == 200){ if(response !=null && response.getStatus() == 200){
result= response.readEntity(String.class); result= response.readEntity(String.class);
}else{ }else{
if (response != null) {
response.close();
}
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class)); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+response.readEntity(String.class));
}
if (response != null) {
response.close();
} }
return result; return result;
} }

View File

@@ -247,7 +247,7 @@ public class HttpClientUtil {
* @param params 参数列表 * @param params 参数列表
* @return 查询结果数据json * @return 查询结果数据json
*/ */
public static String getMsg(String destUrl, Map<String, Object> params, HttpServletRequest req) { public static String getMsg(String destUrl, Map<String, Object> params, HttpServletRequest req) throws IOException {
// RequestContext requestContext = new RequestContext(req); // RequestContext requestContext = new RequestContext(req);
// CloseableHttpResponse response = null; // CloseableHttpResponse response = null;
@@ -296,6 +296,10 @@ public class HttpClientUtil {
logger.error("获取消息失败,相应内容如下: " + result); logger.error("获取消息失败,相应内容如下: " + result);
logger.error("获取消息失败 ", e); logger.error("获取消息失败 ", e);
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"); throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:");
}finally {
if (response != null) {
response.close();
}
} }
return result; return result;
} }

View File

@@ -24,7 +24,7 @@ import com.nis.domain.configuration.ComplexkeywordCfg;
import com.nis.domain.configuration.IpPortCfg; import com.nis.domain.configuration.IpPortCfg;
import com.nis.domain.configuration.NtcBgpAsCfg; import com.nis.domain.configuration.NtcBgpAsCfg;
import com.nis.domain.configuration.NtcSubscribeIdCfg; import com.nis.domain.configuration.NtcSubscribeIdCfg;
import com.nis.exception.MaatConvertException; import com.nis.exceptions.MaatConvertException;
import com.nis.util.Constants; import com.nis.util.Constants;
import com.nis.util.StringUtils; import com.nis.util.StringUtils;
import com.nis.web.controller.BaseController; import com.nis.web.controller.BaseController;