All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.vality.http.client.pool.ProxyServerHttpClientPool Maven / Gradle / Ivy

The newest version!
package dev.vality.http.client.pool;

import dev.vality.http.client.factory.ProxyHttpClientFactory;
import dev.vality.http.client.properties.ProxyRequestConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

@Slf4j
@RequiredArgsConstructor
public class ProxyServerHttpClientPool implements HttpClientPool {

    private final ProxyHttpClientFactory httpClientFactory;
    private final Function keyGeneratorFunction;

    private Map pool = new ConcurrentHashMap<>();

    @Override
    public CloseableHttpClient get(ProxyRequestConfig config) {
        return pool.computeIfAbsent(keyGeneratorFunction.apply(config), s -> httpClientFactory.create(config));
    }

    public void destroy() {
        pool.values().forEach(closeableHttpClient -> {
            try {
                closeableHttpClient.close();
            } catch (IOException e) {
                log.error("Error when close HttpClientPool e: ", e);
            }
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy