Interface KeyedObjectPool<K,V>
- Type Parameters:
K- the key type - 键类型V- the value type - 值类型
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
GenericKeyedObjectPool
KeyedObjectPool - Keyed Object Pool Interface
KeyedObjectPool - 键控对象池接口
Object pool interface that manages objects by key. Each key has its own pool of objects, useful for multi-tenant or multi-datasource scenarios.
按键管理对象的对象池接口。每个键有自己的对象池,适用于多租户或多数据源场景。
Features | 主要功能:
- Key-based object management - 基于键的对象管理
- Per-key borrow/return - 每键借用/归还
- Per-key statistics - 每键统计
- Multi-tenant support - 多租户支持
Usage Examples | 使用示例:
// Multi-datasource scenario
Connection masterConn = pool.borrowObject("master");
Connection slaveConn = pool.borrowObject("slave");
try {
// use connections
} finally {
pool.returnObject("master", masterConn);
pool.returnObject("slave", slaveConn);
}
// Execute pattern
String result = pool.execute("master", (key, conn) -> {
return conn.executeQuery("SELECT...");
});
Security | 安全性:
- Thread-safe: Implementation dependent - 线程安全: 取决于实现
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-pool V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
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.default <R> Rexecute(K key, BiFunction<K, V, R> action) Executes an action with a pooled object (auto-return).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.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 interface AutoCloseable
close
-
Method Details
-
borrowObject
Borrows an object for the given key. 为给定的键借用对象。- Parameters:
key- the key - 键- Returns:
- the borrowed object - 借用的对象
- Throws:
OpenPoolException- if borrowing fails - 如果借用失败
-
borrowObject
Borrows an object for the given key with timeout. 带超时为给定的键借用对象。- Parameters:
key- the key - 键timeout- the maximum wait time - 最大等待时间- Returns:
- the borrowed object - 借用的对象
- Throws:
OpenPoolException- if borrowing fails or times out - 如果借用失败或超时
-
returnObject
-
invalidateObject
-
getNumIdle
Gets the idle count for the given key. 获取给定键的空闲数。- Parameters:
key- the key - 键- Returns:
- the idle count - 空闲数
-
getNumActive
Gets the active count for the given key. 获取给定键的活跃数。- Parameters:
key- the key - 键- Returns:
- the active count - 活跃数
-
clear
-
clear
void clear()Clears all objects for all keys. 清除所有键的所有对象。 -
getNumKeys
int getNumKeys()Gets the total number of keys. 获取键的总数。- Returns:
- the key count - 键数
-
execute
Executes an action with a pooled object (auto-return). 使用池化对象执行操作(自动归还)。- Type Parameters:
R- the result type - 结果类型- Parameters:
key- the key - 键action- the action (receives key and object) - 操作(接收键和对象)- Returns:
- the action result - 操作结果
- Throws:
OpenPoolException- if execution fails - 如果执行失败
-