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 连接池在泽西岛的工作中是如何工作的?
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01