目前的项目接口都是http,因此在java项目中使用apache httpclient进行数据传输、访问。
目前程序中涉及到需要callback操作,product需要被动的接收consume的处理状态,为了最大程度的能够callback成功因此consume在http调用出现问题(如:服务不可用、异常、超时)情况下需要进行重试(retry request),在这里我列举出我找到的retry方案,有些成功有些不成功。
我是用的httpclient版本是4.5.2
。关于retry功能我在网上也找了不少的资料,但是都不是我对应的httpclient版本,大多是过时的。
在httpclient版本4.5.2
提供了以下几种retry方案:
这种方案没有测试通过,StandardHttpRequestRetryHandler
实际上是DefaultHttpRequestRetryHandler
的子类,这是官方提供的一个标准的retry方案,为了保证幂等性约定resetful接口必须是GET, HEAD, PUT, DELETE, OPTIONS, and TRACE
中的一种,如下,是我定义的httpclient pool,
public static CloseableHttpClient getHttpClient() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAX_TOTAL);
cm.setDefaultMaxPerRoute(MAX_PERROUTE);
CloseableHttpClient httpClient = HttpClients
.custom()
.setRetryHandler(new StandardHttpRequestRetryHandler())
.setConnectionManager(cm)
.build();
return httpClient;
}
如下是我的测试代码,
@Test
public void test6(){
HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1");
try {
rsp=httpClient.execute(httpPost);
log.info(">> {}",rsp.getStatusLine().getStatusCode());
} catch (Exception e) {
log.error(e.getMessage(),e);
}finally{
HttpUtil.close(rsp);
}
}
运行测试,当url错误、后台报错、后台超时等情况的时候不能进行retry,因此放弃了此方案。
这种方案没有测试通过,和上面的StandardHttpRequestRetryHandler
类似,它提供了一种默认的retry方案,并没有像StandardHttpRequestRetryHandler
一样约定接口必须是冥等的,如下,是我定义的httpclient pool,
public static CloseableHttpClient getHttpClient() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAX_TOTAL);
cm.setDefaultMaxPerRoute(MAX_PERROUTE);
CloseableHttpClient httpClient = HttpClients
.custom()
.setRetryHandler(new DefaultHttpRequestRetryHandler())
.setConnectionManager(cm)
.build();
return httpClient;
}
如下是我的测试代码,
@Test
public void test6(){
HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1");
try {
rsp=httpClient.execute(httpPost);
log.info(">> {}",rsp.getStatusLine().getStatusCode());
} catch (Exception e) {
log.error(e.getMessage(),e);
}finally{
HttpUtil.close(rsp);
}
}
依然没有达到希望的效果。
可以实现,但是不够完美。在官方文档有这么一段,如下,
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(
IOException exception,
int executionCount,
HttpContext context) {
if (executionCount >= 5) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
return false;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof ConnectTimeoutException) {
// Connection refused
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
return true;
}
return false;
}
};
CloseableHttpClient httpclient = HttpClients.custom()
.setRetryHandler(myRetryHandler)
.build();
自定义retry实现,这比较灵活,可以根据异常自定义retry机制以及重试次数,并且可以拿到返回信息,如下,是我定义的httpclient pool,
public static CloseableHttpClient getHttpClient() {
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(
IOException exception,
int executionCount,
HttpContext context) {
if (executionCount >= 5) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
return false;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof ConnectTimeoutException) {
// Connection refused
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
return true;
}
return false;
}
};
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAX_TOTAL);
cm.setDefaultMaxPerRoute(MAX_PERROUTE);
CloseableHttpClient httpClient = HttpClients
.custom()
.setRetryHandler(retryHandler)
.setConnectionManager(cm)
.build();
return httpClient;
}
如下是我的测试代码,
@Test
public void test6(){
HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1");
try {
rsp=httpClient.execute(httpPost);
log.info(">> {}",rsp.getStatusLine().getStatusCode());
} catch (Exception e) {
log.error(e.getMessage(),e);
}finally{
HttpUtil.close(rsp);
}
}
这种方案,可以实现retry,并且可以根据我的需求进行retry,如:retry count,但是就不能控制retry时间的间隔,也只好放弃了,继续寻找找到了下面这个ServiceUnavailableRetryStrategy
。
可以实现,满足需求,这具有HttpRequestRetryHandler
的所有有点,并且可以自定义retry时间的间隔,如下,是我定义的httpclient pool,
public static CloseableHttpClient getHttpClient() {
ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new ServiceUnavailableRetryStrategy() {
/** * retry逻辑 */
@Override
public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
if (executionCount <= 3)
return true;
else
return false;
}
/** * retry间隔时间 */
@Override
public long getRetryInterval() {
return 2000;
}
};
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(MAX_TOTAL);
cm.setDefaultMaxPerRoute(MAX_PERROUTE);
CloseableHttpClient httpClient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler())
.setConnectionManager(cm).build();
return httpClient;
}
如下是我的测试代码,
@Test
public void test6(){
HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1");
try {
rsp=httpClient.execute(httpPost);
log.info(">> {}",rsp.getStatusLine().getStatusCode());
} catch (Exception e) {
log.error(e.getMessage(),e);
}finally{
HttpUtil.close(rsp);
}
}
==手机QQ扫描下方二维码,快速加入Java架构师交流群==