diff --git a/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingHttpRequestFactory.java b/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingHttpRequestFactory.java index 099ad2e8c..737633d62 100644 --- a/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingHttpRequestFactory.java +++ b/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingHttpRequestFactory.java @@ -14,7 +14,7 @@ * This request factory will attempt to load resources using {@link * org.mapfish.print.config.Configuration#loadFile(String)} and {@link * org.mapfish.print.config.Configuration#isAccessible(String)} to load the resources if the http - * method is GET and will fallback to the normal/wrapped factory to make http requests. + * method is GET and will fall back to the normal/wrapped factory to make http requests. */ public final class ConfigFileResolvingHttpRequestFactory implements MfClientHttpRequestFactory { private final Configuration config; @@ -22,6 +22,7 @@ public final class ConfigFileResolvingHttpRequestFactory implements MfClientHttp private final MfClientHttpRequestFactoryImpl httpRequestFactory; private final List callbacks = new CopyOnWriteArrayList<>(); + /** Maximum number of attempts to try to fetch the same http request in case it is failing. */ @Value("${httpRequest.fetchRetry.maxNumber}") private int httpRequestMaxNumberFetchRetry; diff --git a/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingRequest.java b/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingRequest.java index 9754e009b..a1ae107db 100644 --- a/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingRequest.java +++ b/core/src/main/java/org/mapfish/print/http/ConfigFileResolvingRequest.java @@ -205,7 +205,10 @@ private boolean sleepIfPossible(final Exception e, final AtomicInteger counter) LOGGER.debug("Retry fetching {} following exception", this.getURI(), e); return true; } else { - LOGGER.debug("Has reached maximum number of retry for {}", getURI()); + LOGGER.debug( + "Reached maximum number of {} allowed requests attempts for {}", + getHttpRequestMaxNumberFetchRetry(), + getURI()); return false; } } @@ -222,7 +225,7 @@ private void sleepWithExceptionHandling() { } private boolean canRetry(final AtomicInteger counter) { - return counter.incrementAndGet() <= getHttpRequestMaxNumberFetchRetry(); + return counter.incrementAndGet() < getHttpRequestMaxNumberFetchRetry(); } private int getHttpRequestMaxNumberFetchRetry() { diff --git a/core/src/main/java/org/mapfish/print/http/MfClientHttpRequestFactoryImpl.java b/core/src/main/java/org/mapfish/print/http/MfClientHttpRequestFactoryImpl.java index bb865d816..0cbafd288 100644 --- a/core/src/main/java/org/mapfish/print/http/MfClientHttpRequestFactoryImpl.java +++ b/core/src/main/java/org/mapfish/print/http/MfClientHttpRequestFactoryImpl.java @@ -12,6 +12,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -31,6 +32,7 @@ import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.mapfish.print.config.Configuration; +import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; @@ -52,8 +54,11 @@ public class MfClientHttpRequestFactoryImpl extends HttpComponentsClientHttpRequ * @param maxConnTotal Maximum total connections. * @param maxConnPerRoute Maximum connections per route. */ - public MfClientHttpRequestFactoryImpl(final int maxConnTotal, final int maxConnPerRoute) { - super(createHttpClient(maxConnTotal, maxConnPerRoute)); + public MfClientHttpRequestFactoryImpl( + final int maxConnTotal, + final int maxConnPerRoute, + final ThreadPoolJobManager threadPoolJobManager) { + super(createHttpClient(maxConnTotal, maxConnPerRoute, threadPoolJobManager)); } @Nullable @@ -61,21 +66,41 @@ static Configuration getCurrentConfiguration() { return CURRENT_CONFIGURATION.get(); } - private static int getIntProperty(final String name) { + /** + * Return the number of milliseconds until the timeout Use the Automatic cancellation timeout if + * not defined. + * + * @param name timeout idemtifier + * @return the number of milliseconds until the timeout + */ + private static int getTimeoutValue( + final String name, final ThreadPoolJobManager threadPoolJobManager) { final String value = System.getProperty(name); if (value == null) { - return -1; + long millis = TimeUnit.SECONDS.toMillis(threadPoolJobManager.getTimeout()); + if (millis > Integer.MAX_VALUE) { + LOGGER.warn( + "The value of {} is too large. The timeout will be set to the maximum value of {}", + name, + Integer.MAX_VALUE); + return Integer.MAX_VALUE; + } else { + return Integer.parseInt(Long.toString(millis)); + } } return Integer.parseInt(value); } private static CloseableHttpClient createHttpClient( - final int maxConnTotal, final int maxConnPerRoute) { + final int maxConnTotal, + final int maxConnPerRoute, + final ThreadPoolJobManager threadPoolJobManager) { final RequestConfig requestConfig = RequestConfig.custom() - .setConnectionRequestTimeout(getIntProperty("http.connectionRequestTimeout")) - .setConnectTimeout(getIntProperty("http.connectTimeout")) - .setSocketTimeout(getIntProperty("http.socketTimeout")) + .setConnectionRequestTimeout( + getTimeoutValue("http.connectionRequestTimeout", threadPoolJobManager)) + .setConnectTimeout(getTimeoutValue("http.connectTimeout", threadPoolJobManager)) + .setSocketTimeout(getTimeoutValue("http.socketTimeout", threadPoolJobManager)) .build(); final HttpClientBuilder httpClientBuilder = @@ -93,9 +118,9 @@ private static CloseableHttpClient createHttpClient( LOGGER.debug( "Created CloseableHttpClient using connectionRequestTimeout: {} connectTimeout: {}" + " socketTimeout: {}", - getIntProperty("http.connectionRequestTimeout"), - getIntProperty("http.connectTimeout"), - getIntProperty("http.socketTimeout")); + getTimeoutValue("http.connectionRequestTimeout", threadPoolJobManager), + getTimeoutValue("http.connectTimeout", threadPoolJobManager), + getTimeoutValue("http.socketTimeout", threadPoolJobManager)); return closeableHttpClient; } diff --git a/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java b/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java index b5863fdff..e91d53147 100644 --- a/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java +++ b/core/src/main/java/org/mapfish/print/servlet/job/impl/ThreadPoolJobManager.java @@ -136,6 +136,10 @@ public final void setTimeout(final long timeout) { this.timeout = timeout; } + public final long getTimeout() { + return this.timeout; + } + public final void setAbandonedTimeout(final long abandonedTimeout) { this.abandonedTimeout = abandonedTimeout; } diff --git a/core/src/main/resources/mapfish-spring-application-context.xml b/core/src/main/resources/mapfish-spring-application-context.xml index aaf2d532d..2a42ddc33 100644 --- a/core/src/main/resources/mapfish-spring-application-context.xml +++ b/core/src/main/resources/mapfish-spring-application-context.xml @@ -62,7 +62,8 @@ - + +