使用 java apache http client 增加重连 retry
来源:互联网 发布:物联网大数据的应用 编辑:程序博客网 时间:2024/06/05 16:37
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/78860325
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
1,使用apache client
一般使用http的时候都是短链接。所以失败的情况经常发生。
于是想retry 重连增加成功概率。但是又想找一个优美的解决办法。
而不是自己写代码 retry。
参考代码:
https://www.programcreek.com/java-api-examples/index.php?api=org.apache.http.impl.client.DefaultHttpRequestRetryHandler
2,使用retryHandler
全部重试代码:
import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.*;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import java.util.ArrayList;import java.util.List;public class TestHttp { public static void main(String[] args) { for (int i = 1; i < 1000; i++) { String str = test(); if (str.equals("")) { System.out.printf("########## error ########"); } else { System.out.println(str); } } } public static String test() { String str = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(5, true); httpClient.setHttpRequestRetryHandler(retryHandler); HttpPost httpPost = new HttpPost("http://www.baidu.com/"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); //httpPost.setEntity(new UrlEncodedFormEntity(nvps)); try { System.out.println("cout:" + retryHandler.getRetryCount()); HttpResponse response2 = httpClient.execute(httpPost); str = response2.getStatusLine().toString(); System.out.println(str); HttpEntity entity2 = response2.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } catch (Exception e) { e.printStackTrace(); } finally { httpPost.abort(); httpClient.getConnectionManager().shutdown(); } return str; }}
比如访问大百度,访问次数多了只会被拦截掉呢。
3,分析下
主要是使用了 DefaultHttpRequestRetryHandler
这个实现了
public interface HttpRequestRetryHandler { boolean retryRequest(IOException var1, int var2, HttpContext var3);}
重试接口。DefaultHttpRequestRetryHandler 实现了这个接口就可以了。
在初始化的时候
public MyDefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled) { this(retryCount, requestSentRetryEnabled, Arrays.asList(InterruptedIOException.class, UnknownHostException.class, ConnectException.class, SSLException.class)); }
这是个有意思的设计,放入了几个 异常,然后如果出现异常之后就会重试。
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
凡是发现了 IOException 就开始进行重试操作。
直到执行了 n次重试之后为止。
通过日志抓到 发现 java.net.SocketException 重试了一次。
SocketException 也是继承了 IOException。发出网络异常之后就进入重试了。
4,总结
使用 apache retry 非常方便,只需要两行代码就可以完成。
非常的方便。
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/78860325
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
- 使用 java apache http client 增加重连 retry
- socket client 重连
- java 使用httpclient出错org.apache.http.client.ClientProtocolException
- 利用Java NIO 实现client端,支持自动重连
- Rabbitmq-Java-Client重连和consumer恢复机制
- Java STOMP Client reconnect--WebSocketStompClient端实现自动重连
- Netty Client重连实现
- Netty Client重连实现
- java retry使用详解
- 使用HttpUrlConnection取代Apache Http Client
- 关于 Apache Http Client 的使用笔记
- apache http client
- Apache HTTP Client Removal
- apache.http.client.HttpClient
- 【cc3200】增加断线重连机制
- 2.使用org.apache.http.client.HttpClient访问网络
- java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
- java利用org.apache.http.client下载文件
- Easy--Animation
- 集合框架-Comparator和Comparable的区别
- win下官方BLVC版本caffe配置及anaconda2安装与python下配置
- Neutron Server 分层模型(5)
- 解决cmd下mysql中文乱码问题
- 使用 java apache http client 增加重连 retry
- Linux内核驱动之GPIO子系统(一)GPIO的使用
- 一、任意轴线性变换公式
- JS基础学习第十二天:String字符串方法
- android全透明状态栏
- 僵尸进程查找并杀掉
- 正则表达式
- vc2010中mfc/com菜单、控件等汉字显示为问号或乱码的解决方法
- linux 忘记密码