Class SimplePooledObjectFactory<T>
- Type Parameters:
T- the type of object being pooled - 池化对象类型
- All Implemented Interfaces:
PooledObjectFactory<T>
A simplified PooledObjectFactory implementation that accepts
Supplier, Consumer, and Predicate for the common
use case where subclassing BasePooledObjectFactory is overkill.
简化的 PooledObjectFactory 实现,接受 Supplier、
Consumer 和 Predicate,适用于继承
BasePooledObjectFactory 过于繁重的常见场景。
Features | 主要功能:
- Functional-style factory creation - 函数式工厂创建
- Builder pattern for optional parameters - 可选参数的建造者模式
- Convenient static factory methods - 便捷的静态工厂方法
- Default no-op for optional lifecycle hooks - 可选生命周期钩子默认空操作
Usage Examples | 使用示例:
// Simplest case: creator only
PooledObjectFactory<StringBuilder> factory =
SimplePooledObjectFactory.of(StringBuilder::new);
// With destroyer
PooledObjectFactory<Connection> factory =
SimplePooledObjectFactory.of(
() -> DriverManager.getConnection(url),
Connection::close
);
// Full builder
PooledObjectFactory<Connection> factory =
SimplePooledObjectFactory.<Connection>builder(() -> DriverManager.getConnection(url))
.destroyer(Connection::close)
.validator(conn -> conn.isValid(1))
.activator(conn -> conn.setAutoCommit(true))
.passivator(conn -> conn.setAutoCommit(false))
.build();
Security | 安全性:
- Thread-safe: Yes (immutable after construction) - 线程安全: 是 (构造后不可变)
- Null-safe: Creator must not be null - 空值安全: 创建者不能为空
- Since:
- JDK 25, opencode-base-pool V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for SimplePooledObjectFactory. -
Method Summary
Modifier and TypeMethodDescriptionvoidactivateObject(PooledObject<T> obj) Activates a pooled object.static <T> SimplePooledObjectFactory.Builder<T> Creates a builder with the specified creator.voiddestroyObject(PooledObject<T> obj) Destroys a pooled object.Creates a new pooled object.static <T> SimplePooledObjectFactory<T> Creates a factory with only a creator.static <T> SimplePooledObjectFactory<T> Creates a factory with a creator and destroyer.voidpassivateObject(PooledObject<T> obj) Passivates a pooled object.booleanvalidateObject(PooledObject<T> obj) Validates a pooled object.
-
Method Details
-
of
Creates a factory with only a creator. 仅使用创建者创建工厂。Destroyer is no-op, validator always returns true, activator and passivator are no-op.
销毁器为空操作,验证器始终返回 true, 激活器和钝化器为空操作。
- Type Parameters:
T- the type of object being pooled - 池化对象类型- Parameters:
creator- the object creator (must not be null) - 对象创建者(不能为空)- Returns:
- the factory - 工厂
- Throws:
NullPointerException- if creator is null - 如果创建者为空
-
of
Creates a factory with a creator and destroyer. 使用创建者和销毁器创建工厂。Validator always returns true, activator and passivator are no-op.
验证器始终返回 true,激活器和钝化器为空操作。
- Type Parameters:
T- the type of object being pooled - 池化对象类型- Parameters:
creator- the object creator (must not be null) - 对象创建者(不能为空)destroyer- the object destroyer (must not be null) - 对象销毁器(不能为空)- Returns:
- the factory - 工厂
- Throws:
NullPointerException- if creator or destroyer is null - 如果创建者或销毁器为空
-
builder
Creates a builder with the specified creator. 使用指定的创建者创建建造者。- Type Parameters:
T- the type of object being pooled - 池化对象类型- Parameters:
creator- the object creator (must not be null) - 对象创建者(不能为空)- Returns:
- the builder - 建造者
- Throws:
NullPointerException- if creator is null - 如果创建者为空
-
makeObject
Creates a new pooled object. 创建新的池化对象。Called when the pool needs to create a new object.
当池需要创建新对象时调用。
Creates a new object using the configured creator supplier.
使用配置的创建者供应器创建新对象。
- Specified by:
makeObjectin interfacePooledObjectFactory<T>- Returns:
- the new pooled object - 新的池化对象
- Throws:
OpenPoolException- if creation fails - 如果创建失败
-
destroyObject
Destroys a pooled object. 销毁池化对象。Called when the object is being removed from the pool.
当对象从池中移除时调用。
Destroys the object using the configured destroyer consumer.
使用配置的销毁器消费者销毁对象。
- Specified by:
destroyObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to destroy - 要销毁的对象- Throws:
OpenPoolException- if destruction fails - 如果销毁失败
-
validateObject
Validates a pooled object. 验证池化对象。Called to check if the object is still valid for use.
调用以检查对象是否仍可使用。
Validates the object using the configured validator predicate.
使用配置的验证器谓词验证对象。
- Specified by:
validateObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to validate - 要验证的对象- Returns:
- true if valid, false otherwise - 有效返回true,否则返回false
-
activateObject
Activates a pooled object. 激活池化对象。Called before the object is borrowed from the pool.
在对象从池中借出前调用。
Activates the object using the configured activator consumer.
使用配置的激活器消费者激活对象。
- Specified by:
activateObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to activate - 要激活的对象- Throws:
OpenPoolException- if activation fails - 如果激活失败
-
passivateObject
Passivates a pooled object. 钝化池化对象。Called after the object is returned to the pool.
在对象归还到池后调用。
Passivates the object using the configured passivator consumer.
使用配置的钝化器消费者钝化对象。
- Specified by:
passivateObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to passivate - 要钝化的对象- Throws:
OpenPoolException- if passivation fails - 如果钝化失败
-