我正在尝试使用CloseableHttpClient击中其余端点,我想使用ClosableHttpClient的keep-alive属性。即使我将keepalive设置为10000毫秒,每个请求后连接仍然关闭。我启用了调试日志,我可以在每个response.close()上看到连接已关闭。
@media print {
body {transform: scale(.5);}
table {page-break-inside: avoid;}
日志
public class RestClientTest {
static final Logger logger = Logger.getLogger(RestClientTest.class);
static {
System.setProperty("org.apache.http", "debug");
System.setProperty("org.apache.http.wire", "debug");
System.setProperty("org.apache.http.impl.conn", "debug");
System.setProperty("org.apache.http.impl.client", "debug");
System.setProperty("org.apache.http.client", "debug");
}
public static void main(String[] args) throws IOException {
ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse httpResponse, org.apache.http.protocol.HttpContext context) {
return 10*1000;
}
};
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
.setKeepAliveStrategy(myStrategy)
.setConnectionManager(connManager)
.build();
HttpGet httpGet;
CloseableHttpResponse response = null;
try {
httpGet = new HttpGet("http://localhost:8080/company/offlineTicket");
for (int i = 0; i <100; i++) {
long starTime = System.currentTimeMillis();
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException();
}
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
}