Skip to content

Commit 5ceccc6

Browse files
committed
Revert "提供异常刷新httpClient,异常可配置,重写getHttpClient代码"
This reverts commit 1946508.
1 parent 33e3fcd commit 5ceccc6

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.IOException;
44
import java.nio.charset.Charset;
5+
import java.util.HashMap;
56
import java.util.Map;
6-
import java.util.concurrent.ConcurrentHashMap;
77
import java.util.function.Predicate;
88

99
import org.apache.commons.io.IOUtils;
@@ -24,7 +24,6 @@
2424
import us.codecraft.webmagic.utils.CharsetUtils;
2525
import us.codecraft.webmagic.utils.HttpClientUtils;
2626

27-
2827
/**
2928
* The http downloader based on HttpClient.
3029
*
@@ -33,7 +32,7 @@
3332
*/
3433
public class HttpClientDownloader extends AbstractDownloader {
3534

36-
private final Map<String, CloseableHttpClient> httpClients = new ConcurrentHashMap<>();
35+
private final Map<String, CloseableHttpClient> httpClients = new HashMap<String, CloseableHttpClient>();
3736
private final Logger logger = LoggerFactory.getLogger(getClass());
3837
private final HttpClientGenerator httpClientGenerator = new HttpClientGenerator();
3938

@@ -46,13 +45,6 @@ public class HttpClientDownloader extends AbstractDownloader {
4645

4746
private Predicate<Throwable> refreshProxyOnError = t -> false;
4847

49-
50-
private Predicate<Throwable> refreshClientOnError = t -> false;
51-
52-
53-
public void setRefreshClientOnError(Predicate<Throwable> clientOnError){
54-
this.refreshClientOnError = clientOnError;
55-
}
5648
public void setRefreshProxyOnError(Predicate<Throwable> proxyOnError) {
5749
this.refreshProxyOnError = refreshProxyOnError;
5850
}
@@ -70,8 +62,17 @@ private CloseableHttpClient getHttpClient(Site site) {
7062
return httpClientGenerator.getClient(null);
7163
}
7264
String domain = site.getDomain();
73-
return httpClients.computeIfAbsent(domain,k->httpClientGenerator.getClient(site));
74-
65+
CloseableHttpClient httpClient = httpClients.get(domain);
66+
if (httpClient == null) {
67+
synchronized (this) {
68+
httpClient = httpClients.get(domain);
69+
if (httpClient == null) {
70+
httpClient = httpClientGenerator.getClient(site);
71+
httpClients.put(domain, httpClient);
72+
}
73+
}
74+
}
75+
return httpClient;
7576
}
7677

7778
@Override

0 commit comments

Comments
 (0)