mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add config
This commit is contained in:
parent
f6dad69c7f
commit
ca93693a86
@ -41,11 +41,6 @@ public class ApacheHttpClientConfig extends ClientConfig {
|
|||||||
* 每个路由的最大连接数
|
* 每个路由的最大连接数
|
||||||
*/
|
*/
|
||||||
private int maxPerRoute;
|
private int maxPerRoute;
|
||||||
/**
|
|
||||||
* 重定向最大次数
|
|
||||||
*/
|
|
||||||
private int maxRedirects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取最大连接总数
|
* 获取最大连接总数
|
||||||
*
|
*
|
||||||
@ -85,24 +80,4 @@ public class ApacheHttpClientConfig extends ClientConfig {
|
|||||||
this.maxPerRoute = maxPerRoute;
|
this.maxPerRoute = maxPerRoute;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取重定向最大次数
|
|
||||||
*
|
|
||||||
* @return 重定向最大次数
|
|
||||||
*/
|
|
||||||
public int getMaxRedirects() {
|
|
||||||
return maxRedirects;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置重定向最大次数
|
|
||||||
*
|
|
||||||
* @param maxRedirects 重定向最大次数
|
|
||||||
* @return this
|
|
||||||
*/
|
|
||||||
public ApacheHttpClientConfig setMaxRedirects(final int maxRedirects) {
|
|
||||||
this.maxRedirects = maxRedirects;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,10 @@ public class ClientConfig {
|
|||||||
* 默认连接超时
|
* 默认连接超时
|
||||||
*/
|
*/
|
||||||
private int connectionTimeout;
|
private int connectionTimeout;
|
||||||
|
/**
|
||||||
|
* 默认连接保持时间(连接池中),单位:毫秒
|
||||||
|
*/
|
||||||
|
private long timeToLive;
|
||||||
/**
|
/**
|
||||||
* 默认读取超时
|
* 默认读取超时
|
||||||
*/
|
*/
|
||||||
@ -112,6 +116,26 @@ public class ClientConfig {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取连接保持时间(连接池中),单位:毫秒
|
||||||
|
*
|
||||||
|
* @return 连接保持时间(连接池中),单位:毫秒
|
||||||
|
*/
|
||||||
|
public long getTimeToLive() {
|
||||||
|
return timeToLive;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置连接保持时间(连接池中),单位:毫秒
|
||||||
|
*
|
||||||
|
* @param timeToLive 连接保持时间(连接池中),单位:毫秒
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public ClientConfig setTimeToLive(final long timeToLive) {
|
||||||
|
this.timeToLive = timeToLive;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取读取超时,单位:毫秒
|
* 获取读取超时,单位:毫秒
|
||||||
*
|
*
|
||||||
|
@ -50,6 +50,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apache HttpClient5的HTTP请求引擎
|
* Apache HttpClient5的HTTP请求引擎
|
||||||
@ -154,9 +155,18 @@ public class HttpClient4Engine extends AbstractClientEngine {
|
|||||||
* @return PoolingHttpClientConnectionManager
|
* @return PoolingHttpClientConnectionManager
|
||||||
*/
|
*/
|
||||||
private PoolingHttpClientConnectionManager buildConnectionManager(final ClientConfig config) {
|
private PoolingHttpClientConnectionManager buildConnectionManager(final ClientConfig config) {
|
||||||
|
long timeToLive = config.getTimeToLive();
|
||||||
|
if(timeToLive <= 0){
|
||||||
|
timeToLive = -1;
|
||||||
|
}
|
||||||
final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
|
final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
|
||||||
// SSL配置,当config.getSslInfo()为null时,使用默认配置
|
// SSL配置,当config.getSslInfo()为null时,使用默认配置
|
||||||
ConnectionSocketFactoryRegistryBuilder.build(config.getSslInfo())
|
ConnectionSocketFactoryRegistryBuilder.build(config.getSslInfo()),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
timeToLive,
|
||||||
|
TimeUnit.MILLISECONDS
|
||||||
);
|
);
|
||||||
|
|
||||||
// 连接池配置
|
// 连接池配置
|
||||||
@ -195,9 +205,6 @@ public class HttpClient4Engine extends AbstractClientEngine {
|
|||||||
if (readTimeout > 0) {
|
if (readTimeout > 0) {
|
||||||
requestConfigBuilder.setSocketTimeout(readTimeout);
|
requestConfigBuilder.setSocketTimeout(readTimeout);
|
||||||
}
|
}
|
||||||
if (config instanceof ApacheHttpClientConfig) {
|
|
||||||
requestConfigBuilder.setMaxRedirects(((ApacheHttpClientConfig) config).getMaxRedirects());
|
|
||||||
}
|
|
||||||
|
|
||||||
return requestConfigBuilder.build();
|
return requestConfigBuilder.build();
|
||||||
}
|
}
|
||||||
|
@ -173,13 +173,20 @@ public class HttpClient5Engine extends AbstractClientEngine {
|
|||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// 连接超时配置
|
|
||||||
|
// 默认连接配置
|
||||||
|
final ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom();
|
||||||
final int connectionTimeout = config.getConnectionTimeout();
|
final int connectionTimeout = config.getConnectionTimeout();
|
||||||
if (connectionTimeout > 0) {
|
if (connectionTimeout > 0) {
|
||||||
connectionManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom()
|
connectionConfigBuilder
|
||||||
.setSocketTimeout(connectionTimeout, TimeUnit.MILLISECONDS)
|
.setSocketTimeout(connectionTimeout, TimeUnit.MILLISECONDS)
|
||||||
.setConnectTimeout(connectionTimeout, TimeUnit.MILLISECONDS).build());
|
.setConnectTimeout(connectionTimeout, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
final long timeToLive = config.getTimeToLive();
|
||||||
|
if(timeToLive > 0){
|
||||||
|
connectionConfigBuilder.setTimeToLive(timeToLive, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
connectionManagerBuilder.setDefaultConnectionConfig(connectionConfigBuilder.build());
|
||||||
|
|
||||||
// 连接池配置
|
// 连接池配置
|
||||||
if (config instanceof ApacheHttpClientConfig) {
|
if (config instanceof ApacheHttpClientConfig) {
|
||||||
@ -215,9 +222,6 @@ public class HttpClient5Engine extends AbstractClientEngine {
|
|||||||
if (readTimeout > 0) {
|
if (readTimeout > 0) {
|
||||||
requestConfigBuilder.setResponseTimeout(readTimeout, TimeUnit.MILLISECONDS);
|
requestConfigBuilder.setResponseTimeout(readTimeout, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
if (config instanceof ApacheHttpClientConfig) {
|
|
||||||
requestConfigBuilder.setMaxRedirects(((ApacheHttpClientConfig) config).getMaxRedirects());
|
|
||||||
}
|
|
||||||
|
|
||||||
return requestConfigBuilder.build();
|
return requestConfigBuilder.build();
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.http.client.engine.okhttp;
|
package org.dromara.hutool.http.client.engine.okhttp;
|
||||||
|
|
||||||
import okhttp3.ConnectionPool;
|
|
||||||
import org.dromara.hutool.http.client.ClientConfig;
|
import org.dromara.hutool.http.client.ClientConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,25 +34,28 @@ public class OkHttpClientConfig extends ClientConfig {
|
|||||||
return new OkHttpClientConfig();
|
return new OkHttpClientConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConnectionPool connectionPool;
|
/**
|
||||||
|
* 默认最大空闲连接数
|
||||||
|
*/
|
||||||
|
private int maxIdle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取连接池
|
* 获取最大空闲连接数
|
||||||
*
|
*
|
||||||
* @return ConnectionPool
|
* @return 最大空闲连接数
|
||||||
*/
|
*/
|
||||||
public ConnectionPool getConnectionPool() {
|
public int getMaxIdle() {
|
||||||
return connectionPool;
|
return maxIdle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置连接池
|
* 设置最大空闲连接数
|
||||||
*
|
*
|
||||||
* @param connectionPool ConnectionPool
|
* @param maxIdle 最大空闲连接数
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public OkHttpClientConfig setConnectionPool(final ConnectionPool connectionPool) {
|
public ClientConfig setMaxIdle(final int maxIdle) {
|
||||||
this.connectionPool = connectionPool;
|
this.maxIdle = maxIdle;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.http.client.engine.okhttp;
|
package org.dromara.hutool.http.client.engine.okhttp;
|
||||||
|
|
||||||
|
import okhttp3.ConnectionPool;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
@ -120,9 +121,18 @@ public class OkHttpEngine extends AbstractClientEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 连接池
|
// 连接池
|
||||||
if (config instanceof OkHttpClientConfig) {
|
int maxIdle = 0;
|
||||||
builder.connectionPool(((OkHttpClientConfig) config).getConnectionPool());
|
if(config instanceof OkHttpClientConfig){
|
||||||
|
maxIdle = ((OkHttpClientConfig) config).getMaxIdle();
|
||||||
}
|
}
|
||||||
|
if(maxIdle <= 0){
|
||||||
|
maxIdle = 5;
|
||||||
|
}
|
||||||
|
long timeToLive = config.getTimeToLive();
|
||||||
|
if(timeToLive <= 0){
|
||||||
|
timeToLive = TimeUnit.MINUTES.toMillis(5);
|
||||||
|
}
|
||||||
|
builder.connectionPool(new ConnectionPool(maxIdle, timeToLive, TimeUnit.MILLISECONDS));
|
||||||
|
|
||||||
// 关闭自动重定向,手动实现
|
// 关闭自动重定向,手动实现
|
||||||
builder.followRedirects(false);
|
builder.followRedirects(false);
|
||||||
|
@ -52,6 +52,7 @@ public class DownloadTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Disabled
|
||||||
void downloadWithHeaderTest() {
|
void downloadWithHeaderTest() {
|
||||||
HttpDownloader.of("https://hutool.cn/")
|
HttpDownloader.of("https://hutool.cn/")
|
||||||
.header(MapUtil.of("Authorization", "token"))
|
.header(MapUtil.of("Authorization", "token"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user