Class OpenPool

java.lang.Object
cloud.opencode.base.pool.OpenPool

public final class OpenPool extends Object
OpenPool - Pool Component Facade Entry Class OpenPool - 池组件门面入口类

Provides simplified APIs for creating and managing object pools. This is the main entry point for the pool component.

提供创建和管理对象池的简化API。这是池组件的主要入口点。

Features | 主要功能:

  • Factory methods for all pool types - 所有池类型的工厂方法
  • Configuration builder access - 配置构建器访问
  • Eviction policy helpers - 驱逐策略辅助方法
  • Fluent API design - 流式API设计

Usage Examples | 使用示例:

// Create generic pool with default config
ObjectPool<Connection> pool = OpenPool.createPool(factory);

// Create pool with custom config
ObjectPool<Connection> pool = OpenPool.createPool(factory,
    OpenPool.configBuilder()
        .maxTotal(20)
        .testOnBorrow(true)
        .build());

// Create keyed pool
KeyedObjectPool<String, Connection> keyedPool =
    OpenPool.createKeyedPool(keyedFactory, config);

// Create eviction policy
EvictionPolicy<Connection> policy = OpenPool.allEviction(
    OpenPool.idleTimeEviction(Duration.ofMinutes(30)),
    OpenPool.lfuEviction(5));

Security | 安全性:

  • Thread-safe: Yes (utility class) - 线程安全: 是(工具类)
  • Null-safe: No (null parameters not accepted) - 空值安全: 否(不接受空参数)
Since:
JDK 25, opencode-base-pool V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • createPool

      public static <T> ObjectPool<T> createPool(PooledObjectFactory<T> factory)
      Creates a generic object pool with default configuration. 使用默认配置创建通用对象池。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      factory - the object factory - 对象工厂
      Returns:
      the object pool - 对象池
    • createPool

      public static <T> ObjectPool<T> createPool(PooledObjectFactory<T> factory, PoolConfig config)
      Creates a generic object pool with custom configuration. 使用自定义配置创建通用对象池。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      factory - the object factory - 对象工厂
      config - the pool configuration - 池配置
      Returns:
      the object pool - 对象池
    • createKeyedPool

      public static <K,V> KeyedObjectPool<K,V> createKeyedPool(KeyedPooledObjectFactory<K,V> factory)
      Creates a keyed object pool with default configuration. 使用默认配置创建键控对象池。
      Type Parameters:
      K - the key type - 键类型
      V - the value type - 值类型
      Parameters:
      factory - the keyed factory - 键控工厂
      Returns:
      the keyed pool - 键控池
    • createKeyedPool

      public static <K,V> KeyedObjectPool<K,V> createKeyedPool(KeyedPooledObjectFactory<K,V> factory, PoolConfig config)
      Creates a keyed object pool with custom configuration. 使用自定义配置创建键控对象池。
      Type Parameters:
      K - the key type - 键类型
      V - the value type - 值类型
      Parameters:
      factory - the keyed factory - 键控工厂
      config - the pool configuration - 池配置
      Returns:
      the keyed pool - 键控池
    • createThreadLocalPool

      public static <T> ObjectPool<T> createThreadLocalPool(PooledObjectFactory<T> factory)
      Creates a thread-local pool (one object per thread). 创建线程本地池(每线程一个对象)。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      factory - the object factory - 对象工厂
      Returns:
      the thread-local pool - 线程本地池
    • createSoftReferencePool

      public static <T> ObjectPool<T> createSoftReferencePool(PooledObjectFactory<T> factory)
      Creates a soft reference pool (GC-friendly). 创建软引用池(GC友好)。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      factory - the object factory - 对象工厂
      Returns:
      the soft reference pool - 软引用池
    • createSoftReferencePool

      public static <T> ObjectPool<T> createSoftReferencePool(PooledObjectFactory<T> factory, PoolConfig config)
      Creates a soft reference pool with custom configuration. 使用自定义配置创建软引用池。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      factory - the object factory - 对象工厂
      config - the pool configuration - 池配置
      Returns:
      the soft reference pool - 软引用池
    • createVirtualThreadPool

      public static <T> ObjectPool<T> createVirtualThreadPool(PooledObjectFactory<T> factory)
      Creates a virtual thread optimized pool with default configuration. 使用默认配置创建虚拟线程优化的池。

      This pool is optimized for Virtual Threads with ScopedValue context propagation.

      此池针对虚拟线程进行了优化,支持 ScopedValue 上下文传播。

      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      factory - the object factory - 对象工厂
      Returns:
      the virtual thread pool - 虚拟线程池
    • createVirtualThreadPool

      public static <T> ObjectPool<T> createVirtualThreadPool(PooledObjectFactory<T> factory, PoolConfig config)
      Creates a virtual thread optimized pool with custom configuration. 使用自定义配置创建虚拟线程优化的池。

      This pool is optimized for Virtual Threads with ScopedValue context propagation.

      此池针对虚拟线程进行了优化,支持 ScopedValue 上下文传播。

      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      factory - the object factory - 对象工厂
      config - the pool configuration - 池配置
      Returns:
      the virtual thread pool - 虚拟线程池
    • configBuilder

      public static PoolConfig.Builder configBuilder()
      Creates a pool configuration builder. 创建池配置构建器。
      Returns:
      the builder - 构建器
    • defaultConfig

      public static PoolConfig defaultConfig()
      Gets the default pool configuration. 获取默认池配置。
      Returns:
      the default config - 默认配置
    • idleTimeEviction

      public static <T> EvictionPolicy<T> idleTimeEviction(Duration maxIdleTime)
      Creates an idle time eviction policy. 创建空闲时间驱逐策略。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      maxIdleTime - the maximum idle time - 最大空闲时间
      Returns:
      the eviction policy - 驱逐策略
    • lruEviction

      public static <T> EvictionPolicy<T> lruEviction(int maxObjects)
      Creates an LRU eviction policy. 创建LRU驱逐策略。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      maxObjects - the maximum objects to keep - 保留的最大对象数
      Returns:
      the eviction policy - 驱逐策略
    • lfuEviction

      public static <T> EvictionPolicy<T> lfuEviction(long minBorrowCount)
      Creates an LFU eviction policy. 创建LFU驱逐策略。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      minBorrowCount - the minimum borrow count - 最小借用次数
      Returns:
      the eviction policy - 驱逐策略
    • allEviction

      @SafeVarargs public static <T> EvictionPolicy<T> allEviction(EvictionPolicy<T>... policies)
      Creates a composite eviction policy (all must match). 创建组合驱逐策略(全部必须匹配)。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      policies - the policies to combine - 要组合的策略
      Returns:
      the eviction policy - 驱逐策略
    • anyEviction

      @SafeVarargs public static <T> EvictionPolicy<T> anyEviction(EvictionPolicy<T>... policies)
      Creates a composite eviction policy (any can match). 创建组合驱逐策略(任一匹配即可)。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      policies - the policies to combine - 要组合的策略
      Returns:
      the eviction policy - 驱逐策略