http 连接池在泽西岛的工作中是如何工作的?工作、泽西、连接池、http

2023-09-06 11:43:11 作者:沦为爱囚

这是我的代码.

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.JerseyClientBuilder;

public class Jersey2HttpClient {

    private static class InstanceHolder {
        private static final JerseyClient INSTANCE = createClient();

        private static JerseyClient createClient() {
            ClientConfig clientConfig = new ClientConfig();
            clientConfig.property(ClientProperties.READ_TIMEOUT, 20000);
            clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 20000);
            PoolingHttpClientConnectionManager connectionManager =
                new PoolingHttpClientConnectionManager();
            connectionManager.setMaxTotal(200);
            connectionManager.setDefaultMaxPerRoute(50);
            clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
            clientConfig.connectorProvider(new ApacheConnectorProvider());

            JerseyClient client = JerseyClientBuilder.createClient(clientConfig);
            client.register(RequestLogger.requestLoggingFilter);
            return client;
        }
    }

    public static JerseyClient getInstance() {
        return InstanceHolder.INSTANCE;
    }
}

我有以下问题.

如果每次创建连接池对象"时都会将相同的客户端(通过 getInstance())返回给调用线程?似乎同一个对象(客户端)用于连接.

If every time the same client will be returned(through getInstance()) to the calling thread when are 'connection pooling objects' created? It seems like the same object (client) is used for connections.

执行以下代码时究竟会发生什么.

What exactly happens when the following code is executed.

JerseyClient 客户端 = JerseyClientBuilder.createClient(clientConfig);

JerseyClient client = JerseyClientBuilder.createClient(clientConfig);

换句话说,为什么创建客户端是一项昂贵的操作?我什至还没有提到 url 或请求.

In other words why is creating a client an expensive operation? I haven't even mentioned the url or the request yet.

对不起,我对这方面的知识薄弱.

Sorry for my weak knowledge on this subject.

推荐答案

Client 实例是重量级的

Java JDBC 事务 DAO 数据库连接池

Client 实例可能是一项昂贵的操作,因为 客户端是管理与服务器的底层通信基础设施的重量级对象.

Client instances are heavy-weight

The initialization of Client instances might be an expensive operation because Clients are heavy-weight objects that manage the underlying communication infrastructure with the server.

您应该只创建少量 Client 实例并尽可能重用它们.文档声明如下:

You should create only a small number of Client instances and reuse them when possible. The documentation states the following:

Client 是管理客户端通信基础设施的重量级对象.Client 实例的初始化和处置可能是一项相当昂贵的操作.因此建议在应用程序中只构建少量的 Client 实例.Client 实例必须在被处理之前正确关闭以避免资源泄漏.

Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of a Client instance may be a rather expensive operation. It is therefore advised to construct only a small number of Client instances in the application. Client instances must be properly closed before being disposed to avoid leaking resources.

使用 ApacheConnectorProvider

默认情况下,泽西岛的传输层由 HttpURLConnection.此支持通过 ApacheConnectorProvider.要使用它,请确保您具有以下依赖项:

Jersey integrates with Apache HTTP Client via the ApacheConnectorProvider. To use it, ensure you have the following dependecy:

<dependency>
    <groupId>org.glassfish.jersey.connectors</groupId>
    <artifactId>jersey-apache-connector</artifactId>
    <version>2.23.2</version>
</dependency>

在您的代码中,您已经实例化了一个 PoolingHttpClientConnectionManager 但你没有在任何地方使用它.将以下行添加到您的代码中:

In your code, you have instantiated a PoolingHttpClientConnectionManager but you are not using it anywhere. Add the following lines to your code:

clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
clientConfig.connectorProvider(new ApacheConnectorProvider());

有关更多详细信息,请参阅 Jersey 文档关于连接器.

For additional details, refer to Jersey documentation about connectors.