Newer
Older
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.cesgroup.common.utils.http;
import java.util.concurrent.TimeUnit;
import javax.annotation.PreDestroy;
import org.apache.http.conn.HttpClientConnectionManager;
/**
* 定期清理无效的http连接
* @author chuwanshun
*
*/
public class IdleConnectionEvictor extends Thread {
private final HttpClientConnectionManager manager;
private Integer waitTime;
private Integer idleConTime;
private volatile boolean shutdown = true;
public IdleConnectionEvictor(HttpClientConnectionManager manager, Integer waitTime, Integer idleConTime) {
this.manager = manager;
this.waitTime = waitTime;
this.idleConTime = idleConTime;
this.start();
}
@Override
public void run() {
try {
if (shutdown) {
synchronized (this) {
wait(waitTime);
manager.closeIdleConnections(idleConTime, TimeUnit.SECONDS);
// 关闭失效的连接
manager.closeExpiredConnections();
}
}
} catch (Exception e) {
}
}
@PreDestroy
public void shutdown() {
shutdown = false;
synchronized (this) {
notifyAll();
}
}
}