添加代码注释
This commit is contained in:
parent
74b19a833b
commit
e898d012d2
|
@ -33,17 +33,30 @@ public class HttpClient {
|
|||
@Value("${http.staleConnectionCheckEnabled}")
|
||||
private boolean staleConnectionCheckEnabled;
|
||||
|
||||
/**
|
||||
* 首先实例化一个连接池管理器,设置最大连接数、并发连接数
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = "httpClientConnectionManager")
|
||||
public PoolingHttpClientConnectionManager getHttpClientConnectionManager(){
|
||||
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
|
||||
//最大连接数
|
||||
httpClientConnectionManager.setMaxTotal(maxTotal);
|
||||
//并发数
|
||||
httpClientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
|
||||
return httpClientConnectionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实例化连接池,设置连接池管理器。
|
||||
* 这里需要以参数形式注入上面实例化的连接池管理器
|
||||
* @param httpClientConnectionManager
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = "httpClientBuilder")
|
||||
public HttpClientBuilder getHttpClientBuilder(@Qualifier("httpClientConnectionManager")PoolingHttpClientConnectionManager httpClientConnectionManager){
|
||||
|
||||
//HttpClientBuilder中的构造方法被protected修饰,所以这里不能直接使用new来实例化一个HttpClientBuilder,可以使用HttpClientBuilder提供的静态方法create()来获取HttpClientBuilder对象
|
||||
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
|
||||
|
||||
httpClientBuilder.setConnectionManager(httpClientConnectionManager);
|
||||
|
@ -51,12 +64,23 @@ public class HttpClient {
|
|||
return httpClientBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入连接池,用于获取httpClient
|
||||
* @param httpClientBuilder
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public CloseableHttpClient getCloseableHttpClient(@Qualifier("httpClientBuilder") HttpClientBuilder httpClientBuilder){
|
||||
return httpClientBuilder.build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builder是RequestConfig的一个内部类
|
||||
* 通过RequestConfig的custom方法来获取到一个Builder对象
|
||||
* 设置builder的连接信息
|
||||
* 这里还可以设置proxy,cookieSpec等属性。有需要的话可以在此设置
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = "builder")
|
||||
public RequestConfig.Builder getBuilder(){
|
||||
RequestConfig.Builder builder = RequestConfig.custom();
|
||||
|
@ -66,6 +90,11 @@ public class HttpClient {
|
|||
.setStaleConnectionCheckEnabled(staleConnectionCheckEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用builder构建一个RequestConfig对象
|
||||
* @param builder
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RequestConfig getRequestConfig(@Qualifier("builder") RequestConfig.Builder builder){
|
||||
return builder.build();
|
||||
|
|
|
@ -96,7 +96,7 @@ public class HttpAPIService {
|
|||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
|
||||
}
|
||||
// 构造佛from表单对象
|
||||
// 构造from表单对象
|
||||
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
|
||||
|
||||
// 把表单放到post里
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.lxg.springboot.http;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.http.conn.HttpClientConnectionManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
|
Loading…
Reference in New Issue