Class BasePooledObjectFactory<T>

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

public abstract class BasePooledObjectFactory<T> extends Object implements PooledObjectFactory<T>
BasePooledObjectFactory - Base Pooled Object Factory BasePooledObjectFactory - 基础池化对象工厂

Abstract base class for PooledObjectFactory implementations. Subclasses only need to implement the create() method.

PooledObjectFactory实现的抽象基类。子类只需实现create()方法。

Features | 主要功能:

  • Simplified factory implementation - 简化的工厂实现
  • Default no-op lifecycle methods - 默认空操作的生命周期方法
  • Automatic object wrapping - 自动对象包装
  • Override only what you need - 只覆盖需要的方法

Usage Examples | 使用示例:

PooledObjectFactory<StringBuilder> factory = new BasePooledObjectFactory<>() {
    @Override
    protected StringBuilder create() {
        return new StringBuilder();
    }

    @Override
    public void passivateObject(PooledObject<StringBuilder> obj) {
        obj.getObject().setLength(0);  // Reset on return
    }
};

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 Details

    • BasePooledObjectFactory

      public BasePooledObjectFactory()
  • Method Details

    • create

      protected abstract T create() throws OpenPoolException
      Creates the actual object instance. 创建实际的对象实例。

      Subclasses must implement this method to create the object.

      子类必须实现此方法来创建对象。

      Returns:
      the new object - 新对象
      Throws:
      OpenPoolException - if creation fails - 如果创建失败
    • wrap

      protected PooledObject<T> wrap(T obj)
      Wraps an object in a PooledObject wrapper. 将对象包装在PooledObject包装器中。
      Parameters:
      obj - the object to wrap - 要包装的对象
      Returns:
      the wrapped object - 包装后的对象
    • makeObject

      public PooledObject<T> makeObject() throws OpenPoolException
      Description copied from interface: PooledObjectFactory
      Creates a new pooled object. 创建新的池化对象。

      Called when the pool needs to create a new object.

      当池需要创建新对象时调用。

      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
      Description copied from interface: PooledObjectFactory
      Destroys a pooled object. 销毁池化对象。

      Called when the object is being removed from the pool.

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

      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)
      Description copied from interface: PooledObjectFactory
      Validates a pooled object. 验证池化对象。

      Called to check if the object is still valid for use.

      调用以检查对象是否仍可使用。

      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
      Description copied from interface: PooledObjectFactory
      Activates a pooled object. 激活池化对象。

      Called before the object is borrowed from the pool.

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

      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
      Description copied from interface: PooledObjectFactory
      Passivates a pooled object. 钝化池化对象。

      Called after the object is returned to the pool.

      在对象归还到池后调用。

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