-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigExample.java
More file actions
32 lines (27 loc) · 1.47 KB
/
ConfigExample.java
File metadata and controls
32 lines (27 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.github.mrbox.easyhttp.example;
import com.github.mrbox.easyhttp.config.HttpConfig;
import com.github.mrbox.easyhttp.client.apache.PoolingHttpClientExecutor;
import java.time.Duration;
public class ConfigExample {
public static void main(String[] args) {
HttpConfig customConfig = HttpConfig.builder()
.connectTimeout(Duration.ofSeconds(5)) // 连接超时 5 秒
.readTimeout(Duration.ofSeconds(10)) // 读取超时 10 秒
.connectionRequestTimeout(Duration.ofSeconds(2)) // 从连接池获取连接的超时时间 2 秒
.maxTotal(100) // 最大连接数 100
.maxPerRoute(20) // 每个路由的最大连接数 20
.validateAfterInactivity(Duration.ofSeconds(2)) // 连接空闲 2 秒后验证有效性
.keepAliveDuration(Duration.ofMinutes(1)) // 连接在连接池中保持活跃 1 分钟
.retryCount(3) // 请求失败重试 3 次
.proxyConfig(new HttpConfig.ProxyConfig("127.0.0.1", 7897, "username", "password"))
.build();
// 使用自定义配置创建执行器
try (PoolingHttpClientExecutor executor = new PoolingHttpClientExecutor(customConfig)) {
// 在这里使用 executor 发送请求
System.out.println("配置信息:\n" + customConfig);
// executor.execute(request);
} catch (Exception e) {
e.printStackTrace();
}
}
}