优化http 关闭连接

back_recharge
jiahuiwen 2021-11-01 18:39:03 +08:00
parent 61bb5e7e14
commit 449efd9de5
3 changed files with 15 additions and 29 deletions

View File

@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.ljsd.common.mogodb.util.MongoKeys;
import com.ljsd.jieling.config.json.CoreSettings;
import com.ljsd.jieling.core.CoreLogic;
import com.ljsd.jieling.db.mongo.AreaManager;
import com.ljsd.jieling.db.mongo.MongoUtil;
import com.ljsd.jieling.db.redis.RedisKey;
import com.ljsd.jieling.db.redis.RedisUtil;
@ -17,7 +16,6 @@ import com.ljsd.jieling.logic.activity.event.ServerOpenEvent;
import com.ljsd.jieling.logic.dao.GuilidManager;
import com.ljsd.jieling.logic.dao.MailingSystemManager;
import com.ljsd.jieling.logic.dao.root.User;
import com.ljsd.jieling.logic.hero.HeroLogic;
import com.ljsd.jieling.network.server.GlobalidHelper;
import com.ljsd.jieling.network.server.ProtocolsManager;
import com.ljsd.jieling.thread.ThreadManager;
@ -25,14 +23,11 @@ import com.ljsd.jieling.thread.task.MongoDataHandlerTask;
import com.ljsd.jieling.thrift.pool.ThriftPoolUtils;
import com.ljsd.jieling.util.ConfigurableApplicationContextManager;
import com.ljsd.jieling.util.KeyGenUtils;
import com.ljsd.jieling.util.SensitivewordFilter;
import com.ljsd.jieling.util.http.HttpPool;
import manager.STableManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URL;
/**
* Description:
* Author: zsx

View File

@ -1,23 +1,10 @@
package com.ljsd.jieling.config.reportData;
import com.google.gson.Gson;
import com.ljsd.GameApplication;
import com.ljsd.GameLogicService;
import com.ljsd.common.mogodb.util.BlockingUniqueQueue;
import com.ljsd.jieling.ktbeans.KTParam;
import com.ljsd.jieling.ktbeans.KTSendBody;
import com.ljsd.jieling.util.http.HttpPool;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import util.StringUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

View File

@ -36,7 +36,7 @@ public class HttpPool {
public static String sendPost(String url, String info,String content_type) {
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse;
HttpResponse httpResponse = null;
int sendTimes = 0;
boolean needBack = true;
do {
@ -44,17 +44,14 @@ public class HttpPool {
StringEntity stringEntity = new StringEntity(info, charset);
httpPost.setEntity(stringEntity);
httpPost.setConfig(getRequestConfig());
httpPost.setHeader("Content-Type",content_type);
httpPost.setHeader("Content-Type", content_type);
httpResponse = httpclient.execute(httpPost);
HttpEntity responseEntity = httpResponse.getEntity();
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
needBack = false;
String result = EntityUtils.toString(responseEntity,"UTF-8");
String result = EntityUtils.toString(responseEntity, "UTF-8");
LOGGER.debug("Http Send第" + (sendTimes + 1) + "次调用成功,返回码为:[" + statusCode + "]");
EntityUtils.consume(responseEntity);
return result;
// break;
} else {
@ -69,15 +66,22 @@ public class HttpPool {
} else {
LOGGER.debug("Http Send达到重发最大次数退出程序");
}
EntityUtils.consume(responseEntity);
}
} catch (IOException e) {
LOGGER.error("Http Send error",e);
}finally {
sendTimes ++ ;
LOGGER.error("Http Send error", e);
} finally {
sendTimes++;
if (httpResponse != null) {
HttpEntity responseEntity = httpResponse.getEntity();
try {
EntityUtils.consume(responseEntity);
} catch (IOException e) {
LOGGER.error("EntityUtils.consume(responseEntity)", e);
}
}
}
} while (sendTimes < maxSendTimes);
if(needBack){
if (needBack) {
BackErrorLog.info(info);
}
return null;