How does http connection pooling work in work in Jersey?(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;
}
}
我有以下问题.
如果每次创建连接池对象"时都会将相同的客户端(通过 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
实例是重量级的
Client<的初始化/code> 实例可能是一项昂贵的操作,因为
客户端
是管理与服务器的底层通信基础设施的重量级对象.
Client
instances are heavy-weight
The initialization of Client
instances might be an expensive operation because Client
s 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
实例必须在被处理之前正确关闭以避免资源泄漏.
Client
s are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of aClient
instance may be a rather expensive operation. It is therefore advised to construct only a small number ofClient
instances in the application.Client
instances must be properly closed before being disposed to avoid leaking resources.
使用 ApacheConnectorProvider
默认情况下,泽西岛的传输层由 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 连接池在泽西岛的工作中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:http 连接池在泽西岛的工作中是如何工作的?
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01