如何验证PoolingHttpClientConnectionManager是否应用于球衣客户端



下面是我用于jersey客户端连接池的代码片段。

ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, defaultConnectTimeout);
clientConfig.property(ClientProperties.READ_TIMEOUT, defaultReadTimeout);

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(50);
cm.setDefaultMaxPerRoute(5);
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, cm);
clientConfig.connectorProvider(new ApacheConnectorProvider());

如何验证我的客户端是否正在使用连接池?poolStats.getAvailable((计数是有效的确定方法吗?在我的情况下,当我测试客户端时,这个可用计数是1。

是的,计数可以是1,但要确认,可以尝试以下步骤。

  1. 您可以首先添加一个在后台运行的线程,并每隔一段时间(比如说每60秒(打印poolstats的现有状态。您可以使用以下逻辑。请确保在下面作为后台线程一部分运行的逻辑代码中引用了相同的PoolingHttpClientConnectionManager对象实例
  2. 然后,尝试调用逻辑,该逻辑在内部使用前面提到的jersey客户端继续调用外部服务(可能在for循环中(
  3. 您应该看到不同的日志(在线程逻辑中(被打印出来,这将确认jersey客户端实际上正在使用池配置

逻辑:

PoolStats poolStats = cm.getTotalStats();
Set<HttpRoute> routes = cm.getRoutes();
if(CollectionUtils.isNotEmpty(routes)) {
for (HttpRoute route : routes) {
PoolStats routeStats = poolingHttpClientConnectionManager.getStats(route);
int routeAvailable = routeStats.getAvailable();
int routeLeased = routeStats.getLeased();
int routeIdle = (routeAvailable - routeLeased);
log.info("Pool Stats for Route - Host = {}, Available = {} , Leased = {}, Idle = {}, Pending = {}, Max = {} " ,
route.getTargetHost(), routeAvailable, routeLeased, routeIdle, poolStats.getPending(), poolStats.getMax());
}
}
int available = poolStats.getAvailable();
int leased = poolStats.getLeased();
int idle = (available - leased);
log.info("Pool Stats - Available = {} , Leased = {}, Idle = {}, Pending = {}, Max = {} " ,
available, leased, idle, poolStats.getPending(), poolStats.getMax());

相关内容

最新更新