Java網路爬蟲crawler4j學習筆記 IdleConnectionMonitorThread類
阿新 • • 發佈:2018-12-26
簡介
IdleConnectionMonitorThread類負責監控httpclient中的連線,進行清理操作。同時提供終止爬蟲的功能。
原始碼
package edu.uci.ics.crawler4j.fetcher;
import java.util.concurrent.TimeUnit;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
// 監控http connection的空閒執行緒
public class IdleConnectionMonitorThread extends Thread {
// PoolingHttpClientConnectionManager is a more complex implementation that manages a pool
// of client connections and is able to service connection requests from multiple execution threads.
private final PoolingHttpClientConnectionManager connMgr;
private volatile boolean shutdown;
public IdleConnectionMonitorThread(PoolingHttpClientConnectionManager connMgr) {
super("Connection Manager");
this.connMgr = connMgr;
}
@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(5000);
// Close expired connections,停止過期的連線
connMgr.closeExpiredConnections();
// Optionally, close connections that have been idle longer than 30 sec
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
} catch (InterruptedException ex) {
// terminate
}
}
public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll(); // 讓run方法不再wait
}
}
}