国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 幫助中心 > 正文

spring boot封裝HttpClient的示例代碼

2024-07-09 22:40:42
字體:
供稿:網(wǎng)友

最近使用到了HttpClient,看了一下官方文檔:HttpClient implementations are expected to be thread safe. It is recommended that the same instance of this class is reused for multiple request executions,翻譯過來的意思就是:HttpClient的實(shí)現(xiàn)是線程安全的,可以重用相同的實(shí)例來執(zhí)行多次請(qǐng)求。遇到這種描述的話,我們就應(yīng)該想到,需要對(duì)HttpClient來進(jìn)行封裝了。由于是使用的spring boot,所以下面來結(jié)合spring boot來封裝HttpClient。

一、Request retry handler(請(qǐng)求重試處理)

為了使自定義異常機(jī)制生效,需要實(shí)現(xiàn)HttpRequestRetryHandler接口,代碼如下:

import java.io.IOException; import java.io.InterruptedIOException; import java.net.UnknownHostException; import javax.net.ssl.SSLException; import javax.net.ssl.SSLHandshakeException; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpRequest; import org.apache.http.NoHttpResponseException; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.protocol.HttpContext; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;  /**  * 描述:HttpClient的重試處理機(jī)制  */ @Configuration public class MyhttpRequestRetryHandler {    @Value("${httpclient.config.retryTime}")// 此處建議采用@ConfigurationProperties(prefix="httpclient.config")方式,方便復(fù)用   private int retryTime;      @Bean   public HttpRequestRetryHandler httpRequestRetryHandler() {     // 請(qǐng)求重試     final int retryTime = this.retryTime;     return new HttpRequestRetryHandler() {       public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {         // Do not retry if over max retry count,如果重試次數(shù)超過了retryTime,則不再重試請(qǐng)求         if (executionCount >= retryTime) {           return false;         }         // 服務(wù)端斷掉客戶端的連接異常         if (exception instanceof NoHttpResponseException) {           return true;         }         // time out 超時(shí)重試         if (exception instanceof InterruptedIOException) {           return true;         }         // Unknown host         if (exception instanceof UnknownHostException) {           return false;         }         // Connection refused         if (exception instanceof ConnectTimeoutException) {           return false;         }         // SSL handshake exception         if (exception instanceof SSLException) {           return false;         }         HttpClientContext clientContext = HttpClientContext.adapt(context);         HttpRequest request = clientContext.getRequest();         if (!(request instanceof HttpEntityEnclosingRequest)) {           return true;         }         return false;       }     };   } } 

二、Pooling connection manager(連接池管理)

PoolingHttpClientConnectionManager用來管理客戶端的連接池,并且可以為多個(gè)線程的請(qǐng)求提供服務(wù),代碼如下:

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新干县| 孟连| 西藏| 阳原县| 玛曲县| 钟祥市| 上栗县| 澄迈县| 乌拉特前旗| 德安县| 潮安县| 大安市| 化州市| 元朗区| 西乌珠穆沁旗| 井冈山市| 巴南区| 仁寿县| 鲁山县| 洮南市| 来凤县| 循化| 鄢陵县| 丽江市| 民权县| 定边县| 黄骅市| 淮北市| 会东县| 永善县| 会宁县| 霍山县| 成安县| 疏勒县| 平江县| 安图县| 绥宁县| 甘德县| 攀枝花市| 靖边县| 班戈县|