http连接池如何在泽西岛工作? [英] How does http connection pooling work in work in Jersey?

查看:186
本文介绍了http连接池如何在泽西岛工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。

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;
    }
}

我有以下问题。


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

  1. 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);

换句话说为什么要创建客户进行昂贵的操作?我还没有提到网址或请求。

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.

推荐答案

客户实例是重量级的



初始化< a href =https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/Client.html\"rel =nofollow noreferrer> 客户 实例可能是一项昂贵的操作,因为 客户端 是重量级对象,用于管理服务器的底层通信基础设施。

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.

你应该只创建一小部分 客户端 实例,并在可能的情况下重复使用它们。 文档说明了以下内容:

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


客户端是管理客户端通信基础设施的重量级对象。初始化以及处理客户端实例可能是一项相当昂贵的操作。因此,建议在应用程序中仅构造少量 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



默认情况下,Jersey中的传输层由 HttpURLConnection 。此支持在泽西岛通过 HttpUrlConnectorProvider 。如果需要,可以替换默认连接器。

Using the ApacheConnectorProvider

By default, the transport layer in Jersey is provided by HttpURLConnection. This support is implemented in Jersey via HttpUrlConnectorProvider. You can replace the default connector if you want to.

Jersey通过 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.

这篇关于http连接池如何在泽西岛工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆