From 5ceccc62e076d54e46efb25bd3ddbd1357d278ba Mon Sep 17 00:00:00 2001 From: Sutra Zhou Date: Sat, 2 Jan 2021 20:31:53 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E6=8F=90=E4=BE=9B=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=88=B7=E6=96=B0httpClient=EF=BC=8C=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE=EF=BC=8C=E9=87=8D=E5=86=99getHttpCl?= =?UTF-8?q?ient=E4=BB=A3=E7=A0=81"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 19465089c3ad254e6f35b96cbe707bc6dd33ec62. --- .../downloader/HttpClientDownloader.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java b/webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java index 8cc52db..1d308bc 100644 --- a/webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java +++ b/webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java @@ -2,8 +2,8 @@ package us.codecraft.webmagic.downloader; import java.io.IOException; import java.nio.charset.Charset; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.Predicate; import org.apache.commons.io.IOUtils; @@ -24,7 +24,6 @@ import us.codecraft.webmagic.selector.PlainText; import us.codecraft.webmagic.utils.CharsetUtils; import us.codecraft.webmagic.utils.HttpClientUtils; - /** * The http downloader based on HttpClient. * @@ -33,7 +32,7 @@ import us.codecraft.webmagic.utils.HttpClientUtils; */ public class HttpClientDownloader extends AbstractDownloader { - private final Map httpClients = new ConcurrentHashMap<>(); + private final Map httpClients = new HashMap(); private final Logger logger = LoggerFactory.getLogger(getClass()); private final HttpClientGenerator httpClientGenerator = new HttpClientGenerator(); @@ -46,13 +45,6 @@ public class HttpClientDownloader extends AbstractDownloader { private Predicate refreshProxyOnError = t -> false; - - private Predicate refreshClientOnError = t -> false; - - - public void setRefreshClientOnError(Predicate clientOnError){ - this.refreshClientOnError = clientOnError; - } public void setRefreshProxyOnError(Predicate proxyOnError) { this.refreshProxyOnError = refreshProxyOnError; } @@ -70,8 +62,17 @@ public class HttpClientDownloader extends AbstractDownloader { return httpClientGenerator.getClient(null); } String domain = site.getDomain(); - return httpClients.computeIfAbsent(domain,k->httpClientGenerator.getClient(site)); - + CloseableHttpClient httpClient = httpClients.get(domain); + if (httpClient == null) { + synchronized (this) { + httpClient = httpClients.get(domain); + if (httpClient == null) { + httpClient = httpClientGenerator.getClient(site); + httpClients.put(domain, httpClient); + } + } + } + return httpClient; } @Override