Apache HttpClient 与有限的服务员

问题描述 投票:0回答:1

我正在使用 Apache HttpClient 4.5,我想配置连接池以限制等待线程的数量。因此,如果池耗尽,它应该允许最多 n 线程等待连接(有一定的超时),但任何其他请求都应该立即失败。

有相应的配置选项吗?如果没有,是否有一种干净的方法用信号量来装饰它来实现相同的目的?

java connection-pooling apache-httpclient-4.x
1个回答
0
投票

尝试以下方法

    import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class ConnectionPoolExample {

    public static void main(String[] args) {
        // Create a connection manager with pool settings ,use it with your requirments
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(100); // maximum number of connections in the pool
        connectionManager.setDefaultMaxPerRoute(10); //maximum number of connections per route

        // create a HttpClientBuilder and set the connection manager
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setConnectionManager(connectionManager);

        //closeableHttpClient
        CloseableHttpClient httpClient = httpClientBuilder.build();

        // Use the httpClient for making requests
        
    }
}

我查看了装饰器,以下内容应该会有所帮助。因为据我所知,Apache HttpClient 4.x 中没有可用的直接配置选项来限制连接池中等待线程的数量

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class HttpClientExample {

    public static void main(String[] args) {
        // create a connection manager with the desired maximum connections
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(100); // set the maximum total connections
        connectionManager.setDefaultMaxPerRoute(50); // set the maximum connections per route

        // Create a semaphore to limit the number of waiting threads
        int maxWaitingThreads = 10; // Set the maximum number of waiting threads
        Semaphore semaphore = new Semaphore(maxWaitingThreads);

        // Decorate the connection manager with the semaphore
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .setConnectionManagerShared(true)
                .addInterceptorFirst((HttpRequestInterceptor) (request, context) -> {
                    try {
                        semaphore.acquire(); // acquire a permit from the semaphore
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                })
                .addInterceptorLast((HttpResponseInterceptor) (response, context) -> {
                    semaphore.release(); // Release the permit back to the semaphore
                })
                .build();


        
        httpClient.close();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.