使用Apache HttpClient 4.x进行异常重试
2021-07-20 07:07
标签:text finally prot body tom socket lang span context 在进行http请求时,难免会遇到请求失败的情况,失败后需要重新请求,尝试再次获取数据。 Apache的HttpClient提供了异常重试机制,在该机制中,我们可以很灵活的定义在哪些异常情况下进行重试。 被请求的方法必须是幂等的:就是多次请求服务端结果应该是准确且一致的。 转载自:http://www.cnblogs.com/wuxiaofeng/p/6879292.html,感谢分享。 使用Apache HttpClient 4.x进行异常重试 标签:text finally prot body tom socket lang span context 原文地址:http://www.cnblogs.com/zhouzhilong/p/7056200.html重试前提
实现方式
1 import java.io.IOException;
2 import java.io.InterruptedIOException;
3 import java.net.ConnectException;
4 import java.net.UnknownHostException;
5
6 import javax.net.ssl.SSLException;
7
8 import org.apache.commons.lang3.ObjectUtils;
9 import org.apache.commons.lang3.StringUtils;
10 import org.apache.http.Consts;
11 import org.apache.http.HttpEntityEnclosingRequest;
12 import org.apache.http.HttpRequest;
13 import org.apache.http.HttpStatus;
14 import org.apache.http.ParseException;
15 import org.apache.http.client.HttpRequestRetryHandler;
16 import org.apache.http.client.config.RequestConfig;
17 import org.apache.http.client.methods.CloseableHttpResponse;
18 import org.apache.http.client.methods.HttpPost;
19 import org.apache.http.client.protocol.HttpClientContext;
20 import org.apache.http.entity.ContentType;
21 import org.apache.http.entity.StringEntity;
22 import org.apache.http.impl.client.CloseableHttpClient;
23 import org.apache.http.impl.client.HttpClients;
24 import org.apache.http.protocol.HttpContext;
25 import org.apache.http.util.EntityUtils;
26
27 /**
28 * @author 29 *
29 * @date 2017年5月18日 上午9:17:30
30 *
31 * @Description
32 */
33 public class HttpPostUtils {
34 /**
35 *
36 * @param uri
37 * the request address
38 * @param json
39 * the request data that must be a JSON string
40 * @param retryCount
41 * the number of times this method has been unsuccessfully
42 * executed
43 * @param connectTimeout
44 * the timeout in milliseconds until a connection is established
45 * @param connectionRequestTimeout
46 * the timeout in milliseconds used when requesting a connection
47 * from the connection manager
48 * @param socketTimeout
49 * the socket timeout in milliseconds, which is the timeout for
50 * waiting for data or, put differently, a maximum period
51 * inactivity between two consecutive data packets
52 * @return null when method parameter is null, "", " "
53 * @throws IOException
54 * if HTTP connection can not opened or closed successfully
55 * @throws ParseException
56 * if response data can not be parsed successfully
57 */
58 public String retryPostJson(String uri, String json, int retryCount, int connectTimeout,
59 int connectionRequestTimeout, int socketTimeout) throws IOException, ParseException {
60 if (StringUtils.isAnyBlank(uri, json)) {
61 return null;
62 }
63
64 HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
65
66 @Override
67 public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
68 if (executionCount > retryCount) {
69 // Do not retry if over max retry count
70 return false;
71 }
72 if (exception instanceof InterruptedIOException) {
73 // An input or output transfer has been terminated
74 return false;
75 }
76 if (exception instanceof UnknownHostException) {
77 // Unknown host 修改代码让不识别主机时重试,实际业务当不识别的时候不应该重试,再次为了演示重试过程,执行会显示retryCount次下面的输出
78 System.out.println("不识别主机重试"); return true;
79 }
80 if (exception instanceof ConnectException) {
81 // Connection refused
82 return false;
83 }
84 if (exception instanceof SSLException) {
85 // SSL handshake exception
86 return false;
87 }
88 HttpClientContext clientContext = HttpClientContext.adapt(context);
89 HttpRequest request = clientContext.getRequest();
90 boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
91 if (idempotent) {
92 // Retry if the request is considered idempotent
93 return true;
94 }
95 return false;
96 }
97 };
98
99 CloseableHttpClient client = HttpClients.custom().setRetryHandler(httpRequestRetryHandler).build();
100 HttpPost post = new HttpPost(uri);
101 // Create request data
102 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
103 // Set request body
104 post.setEntity(entity);
105
106 RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeout)
107 .setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build();
108 post.setConfig(config);
109 // Response content
110 String responseContent = null;
111 CloseableHttpResponse response = null;
112 try {
113 response = client.execute(post, HttpClientContext.create());
114 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
115 responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8.name());
116 }
117 } finally {
118 if (ObjectUtils.anyNotNull(response)) {
119 response.close();
120 }
121 if (ObjectUtils.anyNotNull(client)) {
122 client.close();
123 }
124 }
125 return responseContent;
126 }
上一篇:js三大弹出消息框
文章标题:使用Apache HttpClient 4.x进行异常重试
文章链接:http://soscw.com/index.php/essay/106516.html