Java 使用PoolightTPClientConnectionManager获取重用连接的统计信息
如何从PoolghttpClientConnectionManager中获取重复使用的连接数 它包含返回PoolStats的getTotalStats()方法,但不提供有关重用连接数量的信息 PoollightTPClientConnectionManager有一个CPool的私有实例,它从httpcomponents核心扩展了AbstractConnPool。当连接被重用时,getPoolEntryBlocking方法调用onReuse(entry)方法,该方法可用于此目的 我看不出有什么方法可以获取这些信息用于监控目的。Java 使用PoolightTPClientConnectionManager获取重用连接的统计信息,java,connection-pooling,apache-httpclient-4.x,apache-httpcomponents,Java,Connection Pooling,Apache Httpclient 4.x,Apache Httpcomponents,如何从PoolghttpClientConnectionManager中获取重复使用的连接数 它包含返回PoolStats的getTotalStats()方法,但不提供有关重用连接数量的信息 PoollightTPClientConnectionManager有一个CPool的私有实例,它从httpcomponents核心扩展了AbstractConnPool。当连接被重用时,getPoolEntryBlocking方法调用onReuse(entry)方法,该方法可用于此目的 我看不出有什么方法
有人看到其他替代方案吗?如果您扩展或装饰了标准池连接管理器,它将为您提供收集所需连接重用详细信息的入口点
public class MyPoolingHttpClientConnectionManager extends PoolingHttpClientConnectionManager {
final AtomicLong reusedCount = new AtomicLong();
@Override
protected HttpClientConnection leaseConnection(
Future<CPoolEntry> future,
long timeout,
TimeUnit timeUnit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
HttpClientConnection conn = super.leaseConnection(future, timeout, timeUnit);
if (conn.isOpen()) {
reusedCount.incrementAndGet();
}
return conn;
}
}
公共类MyPoolightPClientConnectionManager扩展了PoolightPClientConnectionManager{
最终AtomicLong reusedCount=新的AtomicLong();
@凌驾
受保护的HttpClientConnection最少连接(
未来,,
长时间超时,
TimeUnit(TimeUnit)引发InterruptedException、ExecutionException、ConnectionPoolTimeoutException{
HttpClientConnection conn=super.leaseConnection(未来、超时、时间单位);
如果(连接等参线()){
reusedCount.incrementAndGet();
}
返回连接;
}
}
解决方案是子类化ConnectionRequest并重写get方法,将执行委托给构造函数上接收的ConnectionRequest实例
然后我覆盖来自PoollightTPClientConnectionManager的requestConnection,并用创建的子类包装返回的ConnectionRequest
如果连接是打开的,那么我将增加重用连接的统计信息。否则,我将增加已创建连接的统计信息
public class MonitoredPoolingHttpClientConnectionManager extends PoolingHttpClientConnectionManager
private final AtomicInteger createdConnections = new AtomicInteger();
private final AtomicInteger reusedConnections = new AtomicInteger();
@Override
public ConnectionRequest requestConnection(HttpRoute route, Object state) {
ConnectionRequest requestConnection = super.requestConnection(route, state);
return new MonitoredConnectionRequest(requestConnection);
}
private class MonitoredConnectionRequest implements ConnectionRequest {
private ConnectionRequest wrapped;
public MonitoredConnectionRequest(ConnectionRequest requestConnection) {
this.wrapped = requestConnection;
}
@Override
public boolean cancel() {
return wrapped.cancel();
}
@Override
public HttpClientConnection get(long timeout, TimeUnit timeUnit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
HttpClientConnection conn = wrapped.get(timeout, timeUnit);
if (conn.isOpen()) {
reusedConnections.incrementAndGet();
} else {
createdConnections.incrementAndGet();
}
return conn;
}
}
谢谢,@ok2c但我需要澄清。我看到AbstractConnPool#getPoolEntryBlocking方法可以使用ConnFactory创建新连接。DefaultBHttpClientConnection#createConnection调用bind方法,该方法表示调用后连接将被视为打开。新连接似乎也将处于打开状态,我可能会将新连接计数为使用您的代码段重新使用的连接。@EduardoSilva新创建的连接未绑定到套接字,因此被视为已断开连接(未打开)。CPoolEntry不是公共的。这使我无法重写该方法。@EduardoSilva我看不出有什么理由需要重写CPoolEntry中的方法来收集连接重用统计数据。