Class GenericKeyedObjectPool<K,V>
java.lang.Object
cloud.opencode.base.pool.impl.GenericKeyedObjectPool<K,V>
- Type Parameters:
K- the key type - 键类型V- the value type - 值类型
- All Implemented Interfaces:
KeyedObjectPool<K,V>, AutoCloseable
GenericKeyedObjectPool - Generic Keyed Object Pool Implementation
GenericKeyedObjectPool - 通用键控对象池实现
Keyed object pool that manages separate pools for each key. Useful for multi-tenant or multi-datasource scenarios.
为每个键管理独立池的键控对象池。适用于多租户或多数据源场景。
Features | 主要功能:
- Per-key pool management - 每键池管理
- Lazy pool creation - 延迟池创建
- Shared configuration per pool - 每池共享配置
- Thread-safe key operations - 线程安全的键操作
Usage Examples | 使用示例:
KeyedPooledObjectFactory<String, Connection> factory =
new BaseKeyedPooledObjectFactory<>() {
@Override
protected Connection create(String dsName) {
return getDataSource(dsName).getConnection();
}
};
GenericKeyedObjectPool<String, Connection> pool =
new GenericKeyedObjectPool<>(factory, config);
Connection masterConn = pool.borrowObject("master");
Connection slaveConn = pool.borrowObject("slave");
Security | 安全性:
- Thread-safe: Yes (ConcurrentHashMap, AtomicBoolean) - 线程安全: 是(ConcurrentHashMap,AtomicBoolean)
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-pool V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionGenericKeyedObjectPool(KeyedPooledObjectFactory<K, V> factory) Creates a keyed pool with default configuration.GenericKeyedObjectPool(KeyedPooledObjectFactory<K, V> factory, PoolConfig config) Creates a keyed pool with custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionborrowObject(K key) Borrows an object for the given key.borrowObject(K key, Duration timeout) Borrows an object for the given key with timeout.voidclear()Clears all objects for all keys.voidClears objects for the given key.voidclose()intgetNumActive(K key) Gets the active count for the given key.intgetNumIdle(K key) Gets the idle count for the given key.intGets the total number of keys.intGets total active count across all keys.intGets total idle count across all keys.voidinvalidateObject(K key, V obj) Invalidates an object for the given key.voidreturnObject(K key, V obj) Returns an object for the given key.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface KeyedObjectPool
execute
-
Constructor Details
-
GenericKeyedObjectPool
Creates a keyed pool with default configuration. 使用默认配置创建键控池。- Parameters:
factory- the keyed factory - 键控工厂
-
GenericKeyedObjectPool
Creates a keyed pool with custom configuration. 使用自定义配置创建键控池。- Parameters:
factory- the keyed factory - 键控工厂config- the pool configuration - 池配置
-
-
Method Details
-
borrowObject
Description copied from interface:KeyedObjectPoolBorrows an object for the given key. 为给定的键借用对象。- Specified by:
borrowObjectin interfaceKeyedObjectPool<K,V> - Parameters:
key- the key - 键- Returns:
- the borrowed object - 借用的对象
- Throws:
OpenPoolException- if borrowing fails - 如果借用失败
-
borrowObject
Description copied from interface:KeyedObjectPoolBorrows an object for the given key with timeout. 带超时为给定的键借用对象。- Specified by:
borrowObjectin interfaceKeyedObjectPool<K,V> - Parameters:
key- the key - 键timeout- the maximum wait time - 最大等待时间- Returns:
- the borrowed object - 借用的对象
- Throws:
OpenPoolException- if borrowing fails or times out - 如果借用失败或超时
-
returnObject
Description copied from interface:KeyedObjectPoolReturns an object for the given key. 归还给定键的对象。- Specified by:
returnObjectin interfaceKeyedObjectPool<K,V> - Parameters:
key- the key - 键obj- the object to return - 要归还的对象
-
invalidateObject
Description copied from interface:KeyedObjectPoolInvalidates an object for the given key. 使给定键的对象失效。- Specified by:
invalidateObjectin interfaceKeyedObjectPool<K,V> - Parameters:
key- the key - 键obj- the object to invalidate - 要失效的对象
-
getNumIdle
Description copied from interface:KeyedObjectPoolGets the idle count for the given key. 获取给定键的空闲数。- Specified by:
getNumIdlein interfaceKeyedObjectPool<K,V> - Parameters:
key- the key - 键- Returns:
- the idle count - 空闲数
-
getNumActive
Description copied from interface:KeyedObjectPoolGets the active count for the given key. 获取给定键的活跃数。- Specified by:
getNumActivein interfaceKeyedObjectPool<K,V> - Parameters:
key- the key - 键- Returns:
- the active count - 活跃数
-
clear
Description copied from interface:KeyedObjectPoolClears objects for the given key. 清除给定键的对象。- Specified by:
clearin interfaceKeyedObjectPool<K,V> - Parameters:
key- the key - 键
-
clear
public void clear()Description copied from interface:KeyedObjectPoolClears all objects for all keys. 清除所有键的所有对象。- Specified by:
clearin interfaceKeyedObjectPool<K,V>
-
getNumKeys
public int getNumKeys()Description copied from interface:KeyedObjectPoolGets the total number of keys. 获取键的总数。- Specified by:
getNumKeysin interfaceKeyedObjectPool<K,V> - Returns:
- the key count - 键数
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getTotalNumIdle
public int getTotalNumIdle()Gets total idle count across all keys. 获取所有键的空闲总数。- Returns:
- the total idle count - 空闲总数
-
getTotalNumActive
public int getTotalNumActive()Gets total active count across all keys. 获取所有键的活跃总数。- Returns:
- the total active count - 活跃总数
-