Class SimplePooledObjectFactory<T>

java.lang.Object
cloud.opencode.base.pool.factory.SimplePooledObjectFactory<T>
Type Parameters:
T - the type of object being pooled - 池化对象类型
All Implemented Interfaces:
PooledObjectFactory<T>

public final class SimplePooledObjectFactory<T> extends Object implements PooledObjectFactory<T>
SimplePooledObjectFactory - Functional Pooled Object Factory SimplePooledObjectFactory - 函数式池化对象工厂

A simplified PooledObjectFactory implementation that accepts Supplier, Consumer, and Predicate for the common use case where subclassing BasePooledObjectFactory is overkill.

简化的 PooledObjectFactory 实现,接受 SupplierConsumerPredicate,适用于继承 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:
  • Method Details

    • of

      public static <T> SimplePooledObjectFactory<T> of(Supplier<T> creator)
      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

      public static <T> SimplePooledObjectFactory<T> of(Supplier<T> creator, Consumer<T> destroyer)
      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

      public static <T> SimplePooledObjectFactory.Builder<T> builder(Supplier<T> creator)
      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

      public PooledObject<T> makeObject() throws OpenPoolException
      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:
      makeObject in interface PooledObjectFactory<T>
      Returns:
      the new pooled object - 新的池化对象
      Throws:
      OpenPoolException - if creation fails - 如果创建失败
    • destroyObject

      public void destroyObject(PooledObject<T> obj) throws OpenPoolException
      Destroys a pooled object. 销毁池化对象。

      Called when the object is being removed from the pool.

      当对象从池中移除时调用。

      Destroys the object using the configured destroyer consumer.

      使用配置的销毁器消费者销毁对象。

      Specified by:
      destroyObject in interface PooledObjectFactory<T>
      Parameters:
      obj - the object to destroy - 要销毁的对象
      Throws:
      OpenPoolException - if destruction fails - 如果销毁失败
    • validateObject

      public boolean validateObject(PooledObject<T> obj)
      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:
      validateObject in interface PooledObjectFactory<T>
      Parameters:
      obj - the object to validate - 要验证的对象
      Returns:
      true if valid, false otherwise - 有效返回true,否则返回false
    • activateObject

      public void activateObject(PooledObject<T> obj) throws OpenPoolException
      Activates a pooled object. 激活池化对象。

      Called before the object is borrowed from the pool.

      在对象从池中借出前调用。

      Activates the object using the configured activator consumer.

      使用配置的激活器消费者激活对象。

      Specified by:
      activateObject in interface PooledObjectFactory<T>
      Parameters:
      obj - the object to activate - 要激活的对象
      Throws:
      OpenPoolException - if activation fails - 如果激活失败
    • passivateObject

      public void passivateObject(PooledObject<T> obj) throws OpenPoolException
      Passivates a pooled object. 钝化池化对象。

      Called after the object is returned to the pool.

      在对象归还到池后调用。

      Passivates the object using the configured passivator consumer.

      使用配置的钝化器消费者钝化对象。

      Specified by:
      passivateObject in interface PooledObjectFactory<T>
      Parameters:
      obj - the object to passivate - 要钝化的对象
      Throws:
      OpenPoolException - if passivation fails - 如果钝化失败