ServiceUnavailableRetryStrategy设置重试间隔时间
//工具类
public class PayHttpUtils {
private static ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy=null;
private static PoolingHttpClientConnectionManager cm=null;
private static HttpRequestRetryHandler httpRequestRetryHandler =null;
static {
log.addTrace("PayHttpUtils工具类静态代码块初始化!" );
httpRequestRetryHandler = new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int retryTimes, HttpContext context) {
log.addError("httpclient重试机制入参:exception:" + exception + " retryTimes:" + retryTimes + " context:" + context);
String retryNum = ConfigAlipay.getMessage("retryNum");
if(StringUtil.isEmpty(retryNum)){
retryNum = "3";
}
log.addError("重试次数为:" + retryNum);
if (retryTimes >= Integer.parseInt(retryNum)) {
return false;
}
if (exception instanceof ConnectTimeoutException || exception instanceof NoHttpResponseException || exception instanceof SocketTimeoutException
|| !(exception instanceof UnknownHostException) || !(exception instanceof InterruptedIOException) || !(exception instanceof SSLException)
|| !(exception instanceof SSLHandshakeException)) {
return true;
}else {
log.addError("未记录的请求异常:" + exception.getClass());
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// 如果请求被认为是幂等的,那么就重试。即重复执行不影响程序其他效果的
return true;
}
return false;
}
};
serviceUnavailableRetryStrategy = new ServiceUnavailableRetryStrategy() {
/**
* retry逻辑
*/
@Override
public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
log.addError("retryRequest次数为:"+executionCount);
String retryNum = ConfigAlipay.getMessage("retryNum");
if(StringUtil.isEmpty(retryNum)){
retryNum = "3";
}
//当返回状态码不为200(成功)的情况下重试,重试次数默认设为3次
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && executionCount <= Integer.parseInt(retryNum))
return true;
else
return false;
}
/**
* retry间隔时间
*/
@Override
public long getRetryInterval() {
log.addError("getRetryInterval");
String retryInterval = ConfigAlipay.getMessage("retryInterval");
if(StringUtil.isEmpty(retryInterval)){
retryInterval = "1000";
}
log.addError("重试时间间隔为:" + retryInterval);
return Long.parseLong(retryInterval);
}
};
cm = new PoolingHttpClientConnectionManager();
//连接池最大生成连接数200
cm.setMaxTotal(200);
//默认设置route最大连接数
cm.setDefaultMaxPerRoute(100);
}
public static CloseableHttpClient createSSLClientDefault(Boolean bool) {
try {
if (bool){
log.addTrace("此请求为https方式!");
return HttpsSSLClient.createSSLInsecureClient(cm,httpRequestRetryHandler,serviceUnavailableRetryStrategy);
}
} catch (Exception e) {
e.printStackTrace();
}
log.addTrace("此请求为http方式!");
return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy).build();
}