Class BaseKeyedPooledObjectFactory<K,V>
java.lang.Object
cloud.opencode.base.pool.factory.BaseKeyedPooledObjectFactory<K,V>
- Type Parameters:
K- the key type - 键类型V- the value type - 值类型
- All Implemented Interfaces:
KeyedPooledObjectFactory<K,V>
public abstract class BaseKeyedPooledObjectFactory<K,V>
extends Object
implements KeyedPooledObjectFactory<K,V>
BaseKeyedPooledObjectFactory - Base Keyed Pooled Object Factory
BaseKeyedPooledObjectFactory - 基础键控池化对象工厂
Abstract base class for KeyedPooledObjectFactory implementations. Subclasses only need to implement the create(key) method.
KeyedPooledObjectFactory实现的抽象基类。子类只需实现create(key)方法。
Features | 主要功能:
- Simplified keyed factory implementation - 简化的键控工厂实现
- Default no-op lifecycle methods - 默认空操作的生命周期方法
- Key-aware object creation - 键感知的对象创建
- Override only what you need - 只覆盖需要的方法
Usage Examples | 使用示例:
KeyedPooledObjectFactory<String, Connection> factory =
new BaseKeyedPooledObjectFactory<>() {
@Override
protected Connection create(String dsName) throws OpenPoolException {
try {
return getDataSource(dsName).getConnection();
} catch (SQLException e) {
throw new OpenPoolException("Failed to create connection", e);
}
}
@Override
public void destroyObject(String key, PooledObject<Connection> obj) {
try {
obj.getObject().close();
} catch (SQLException e) {
// log error
}
}
};
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidactivateObject(K key, PooledObject<V> obj) Activates a pooled object for the given key.protected abstract VCreates the actual object instance for the given key.voiddestroyObject(K key, PooledObject<V> obj) Destroys a pooled object for the given key.makeObject(K key) Creates a new pooled object for the given key.voidpassivateObject(K key, PooledObject<V> obj) Passivates a pooled object for the given key.booleanvalidateObject(K key, PooledObject<V> obj) Validates a pooled object for the given key.protected PooledObject<V> Wraps an object in a PooledObject wrapper.
-
Constructor Details
-
BaseKeyedPooledObjectFactory
public BaseKeyedPooledObjectFactory()
-
-
Method Details
-
create
Creates the actual object instance for the given key. 为给定的键创建实际的对象实例。Subclasses must implement this method to create the object.
子类必须实现此方法来创建对象。
- Parameters:
key- the key - 键- Returns:
- the new object - 新对象
- Throws:
OpenPoolException- if creation fails - 如果创建失败
-
wrap
Wraps an object in a PooledObject wrapper. 将对象包装在PooledObject包装器中。- Parameters:
obj- the object to wrap - 要包装的对象- Returns:
- the wrapped object - 包装后的对象
-
makeObject
Description copied from interface:KeyedPooledObjectFactoryCreates a new pooled object for the given key. 为给定的键创建新的池化对象。- Specified by:
makeObjectin interfaceKeyedPooledObjectFactory<K,V> - Parameters:
key- the key - 键- Returns:
- the new pooled object - 新的池化对象
- Throws:
OpenPoolException- if creation fails - 如果创建失败
-
destroyObject
Description copied from interface:KeyedPooledObjectFactoryDestroys a pooled object for the given key. 销毁给定键的池化对象。- Specified by:
destroyObjectin interfaceKeyedPooledObjectFactory<K,V> - Parameters:
key- the key - 键obj- the object to destroy - 要销毁的对象- Throws:
OpenPoolException- if destruction fails - 如果销毁失败
-
validateObject
Description copied from interface:KeyedPooledObjectFactoryValidates a pooled object for the given key. 验证给定键的池化对象。- Specified by:
validateObjectin interfaceKeyedPooledObjectFactory<K,V> - Parameters:
key- the key - 键obj- the object to validate - 要验证的对象- Returns:
- true if valid - 有效返回true
-
activateObject
Description copied from interface:KeyedPooledObjectFactoryActivates a pooled object for the given key. 激活给定键的池化对象。- Specified by:
activateObjectin interfaceKeyedPooledObjectFactory<K,V> - Parameters:
key- the key - 键obj- the object to activate - 要激活的对象- Throws:
OpenPoolException- if activation fails - 如果激活失败
-
passivateObject
Description copied from interface:KeyedPooledObjectFactoryPassivates a pooled object for the given key. 钝化给定键的池化对象。- Specified by:
passivateObjectin interfaceKeyedPooledObjectFactory<K,V> - Parameters:
key- the key - 键obj- the object to passivate - 要钝化的对象- Throws:
OpenPoolException- if passivation fails - 如果钝化失败
-