httppool fix
parent
3a73c47823
commit
788c92b0e8
|
|
@ -2,7 +2,9 @@ package com.ljsd.jieling.util.http;
|
|||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
|
|
@ -17,7 +19,8 @@ public class HttpPool {
|
|||
private static CloseableHttpClient httpclient;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HttpPool.class);
|
||||
private static int maxSendTimes = 2;
|
||||
private static int sendIntervalTime = 200;
|
||||
private static int sendIntervalTime = 1000;
|
||||
private static String charset = "UTF-8";
|
||||
public static void init(){
|
||||
poolConnection.setMaxTotal(100);
|
||||
poolConnection.setDefaultMaxPerRoute(100);
|
||||
|
|
@ -25,37 +28,50 @@ public class HttpPool {
|
|||
httpclient = HttpClients.custom().setConnectionManager(poolConnection).build();
|
||||
}
|
||||
|
||||
public static RequestConfig getRequestConfig() {
|
||||
return RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(50000).setConnectionRequestTimeout(5000).build();
|
||||
|
||||
public static String sendPost (String url ,String info){
|
||||
}
|
||||
public static void sendPost(String url, String info) {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
HttpResponse httpResponse;
|
||||
String retStr = "";
|
||||
int sendTimes = 0;
|
||||
try {
|
||||
httpResponse = httpclient.execute(httpPost);
|
||||
HttpEntity responseEntity = httpResponse.getEntity();
|
||||
retStr = EntityUtils.toString(responseEntity, info);
|
||||
EntityUtils.consume(responseEntity);
|
||||
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
||||
if (statusCode >= 200 && statusCode <300){
|
||||
LOGGER.debug("Http Send,第" + (sendTimes + 1) + "次调用成功,返回码为:[" + statusCode + "]");
|
||||
}else{
|
||||
LOGGER.error("Http Send,第" + (sendTimes + 1) + "次调用出错,返回码为:[" + statusCode + "]");
|
||||
if (sendTimes + 1 <= maxSendTimes) {
|
||||
LOGGER.debug("Http Send," + (sendIntervalTime / 1000) + "秒后,重新发起请求...");
|
||||
try {
|
||||
Thread.sleep(sendIntervalTime);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
do {
|
||||
try {
|
||||
StringEntity stringEntity = new StringEntity(info, charset);
|
||||
httpPost.setEntity(stringEntity);
|
||||
httpPost.setConfig(getRequestConfig());
|
||||
httpResponse = httpclient.execute(httpPost);
|
||||
// retStr = EntityUtils.toString(responseEntity, charset);
|
||||
HttpEntity responseEntity = httpResponse.getEntity();
|
||||
EntityUtils.consume(responseEntity);
|
||||
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
||||
if (statusCode >= 200 && statusCode < 300) {
|
||||
LOGGER.debug("Http Send,第" + (sendTimes + 1) + "次调用成功,返回码为:[" + statusCode + "]");
|
||||
break;
|
||||
} else {
|
||||
LOGGER.debug("Http Send,达到重发最大次数,退出程序!");
|
||||
LOGGER.error("Http Send,第" + (sendTimes + 1) + "次调用出错,返回码为:[" + statusCode + "]");
|
||||
if (sendTimes + 1 <= maxSendTimes) {
|
||||
LOGGER.debug("Http Send," + (sendIntervalTime / 1000) + "秒后,重新发起请求...");
|
||||
try {
|
||||
Thread.sleep(sendIntervalTime);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
LOGGER.debug("Http Send,达到重发最大次数,退出程序!");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
sendTimes ++ ;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} while (sendTimes < maxSendTimes);
|
||||
}
|
||||
|
||||
return retStr;
|
||||
public static void main (String [] args){
|
||||
init();
|
||||
sendPost("http://60.1.1.212:8080","111111111");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue