
no.digipost.http.client.HttpClientConnectionSettings Maven / Gradle / Ivy
Show all versions of http-client-builder5 Show documentation
/**
* Copyright (C) Posten Norge AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package no.digipost.http.client;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLogger;
/**
* A subset of configuration parameters for new {@link org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder http client client connection managers}.
*
* For complete configuration facilities, use the builder acquired from
* {@link HttpClientConnectionManagerFactory#createDefaultBuilder()}.
*/
public class HttpClientConnectionSettings implements PotentiallyDangerous {
public static final HttpClientConnectionSettings DEFAULT = new HttpClientConnectionSettings(
NOPLogger.NOP_LOGGER
, HttpClientDefaults.CONNECTION_AMOUNT_NORMAL
, HttpClientDefaults.SOCKET_TIMEOUT_MS
, HttpClientDefaults.VALIDATE_CONNECTION_AFTER_INACTIVITY_SECOND);
public HttpClientConnectionSettings logConfigurationTo(Logger logger) {
return new HttpClientConnectionSettings(logger, connectionAmount, socketTimeoutMs, validateAfterInactivitySeconds);
}
public HttpClientConnectionSettings connections(HttpClientConnectionAmount connectionAmount) {
return new HttpClientConnectionSettings(logger, connectionAmount, socketTimeoutMs, validateAfterInactivitySeconds);
}
/**
* Socket timeout is used for both requests and, if any,
* underlying layered sockets (typically for
* secure sockets).
*/
public HttpClientConnectionSettings socketTimeout(int timeoutsMs) {
return new HttpClientConnectionSettings(logger, connectionAmount, timeoutsMs, validateAfterInactivitySeconds);
}
/**
* @param seconds Set to a negative value to disable connection validation.
* @see PoolingHttpClientConnectionManager javadoc
*/
public HttpClientConnectionSettings validateConnectionAfterInactivity(int seconds) {
return new HttpClientConnectionSettings(logger, connectionAmount, socketTimeoutMs, seconds);
}
/**
* Total time to live (TTL) set at construction time defines maximum life span of persistent connections regardless of their expiration setting. No persistent connection will be re-used past its TTL value.
*
* @see PoolingHttpClientConnectionManager javadoc
*/
public HttpClientConnectionSettings connectionTTL() {
return new HttpClientConnectionSettings(logger, connectionAmount, socketTimeoutMs, validateAfterInactivitySeconds);
}
@Override
public String toString() {
return String.format(
" - max total connections %s\n"
+ " - max connections per route %s\n"
+ " - socket timeout %s ms\n",
connectionAmount.maxTotal,
connectionAmount.maxPerRoute,
socketTimeoutMs != 0 ? socketTimeoutMs : "[infinite]"
);
}
final Logger logger;
final HttpClientConnectionAmount connectionAmount;
final int socketTimeoutMs;
final int validateAfterInactivitySeconds;
private HttpClientConnectionSettings(
Logger instantiationLogger
, HttpClientConnectionAmount connectionAmount
, int socketTimeoutMs
, int validateAfterInactivitySeconds) {
this.logger = instantiationLogger;
this.connectionAmount = connectionAmount;
this.socketTimeoutMs = Validation.equalOrGreater(socketTimeoutMs, 0, "socket timeout");
this.validateAfterInactivitySeconds = validateAfterInactivitySeconds;
}
@Override
public boolean isPotentiallyDangerous() {
return socketTimeoutMs == 0;
}
}