Class OpenPool
java.lang.Object
cloud.opencode.base.pool.OpenPool
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 | 使用示例:
// Simplest: create pool from Supplier
ObjectPool<StringBuilder> pool = OpenPool.createPool(StringBuilder::new);
// Create pool from Supplier + Consumer (with destroyer)
ObjectPool<Connection> pool = OpenPool.createPool(
() -> DriverManager.getConnection(url),
Connection::close);
// Create generic pool with factory
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 (with max age for connection recycling)
EvictionPolicy<Connection> policy = OpenPool.allEviction(
OpenPool.idleTimeEviction(Duration.ofMinutes(30)),
OpenPool.maxAgeEviction(Duration.ofHours(1)));
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 Summary
Modifier and TypeMethodDescriptionstatic <T> EvictionPolicy<T> allEviction(EvictionPolicy<T>... policies) Creates a composite eviction policy (all must match).static <T> EvictionPolicy<T> anyEviction(EvictionPolicy<T>... policies) Creates a composite eviction policy (any can match).static PoolConfig.BuilderCreates a pool configuration builder.static <K,V> KeyedObjectPool <K, V> createKeyedPool(KeyedPooledObjectFactory<K, V> factory) Creates a keyed object pool with default configuration.static <K,V> KeyedObjectPool <K, V> createKeyedPool(KeyedPooledObjectFactory<K, V> factory, PoolConfig config) Creates a keyed object pool with custom configuration.static <T> ObjectPool<T> createPool(PooledObjectFactory<T> factory) Creates a generic object pool with default configuration.static <T> ObjectPool<T> createPool(PooledObjectFactory<T> factory, PoolConfig config) Creates a generic object pool with custom configuration.static <T> ObjectPool<T> createPool(Supplier<T> creator) Creates a pool from a Supplier (simplest API).static <T> ObjectPool<T> createPool(Supplier<T> creator, PoolConfig config) Creates a pool from a Supplier with custom configuration.static <T> ObjectPool<T> createPool(Supplier<T> creator, Consumer<T> destroyer) Creates a pool from a Supplier with a destroyer Consumer.static <T> ObjectPool<T> createPool(Supplier<T> creator, Consumer<T> destroyer, PoolConfig config) Creates a pool from a Supplier with a destroyer and custom configuration.static <T> ObjectPool<T> createSoftReferencePool(PooledObjectFactory<T> factory) Creates a soft reference pool (GC-friendly).static <T> ObjectPool<T> createSoftReferencePool(PooledObjectFactory<T> factory, PoolConfig config) Creates a soft reference pool with custom configuration.static <T> ObjectPool<T> createThreadLocalPool(PooledObjectFactory<T> factory) Creates a thread-local pool (one object per thread).static <T> ObjectPool<T> createVirtualThreadPool(PooledObjectFactory<T> factory) Creates a virtual thread optimized pool with default configuration.static <T> ObjectPool<T> createVirtualThreadPool(PooledObjectFactory<T> factory, PoolConfig config) Creates a virtual thread optimized pool with custom configuration.static PoolConfigGets the default pool configuration.static <T> EvictionPolicy<T> idleTimeEviction(Duration maxIdleTime) Creates an idle time eviction policy.static <T> EvictionPolicy<T> lfuEviction(long minBorrowCount) Creates an LFU eviction policy.static <T> EvictionPolicy<T> lruEviction(int maxObjects) Creates an LRU eviction policy.static <T> EvictionPolicy<T> maxAgeEviction(Duration maxLifetime) Creates a max age eviction policy.
-
Method Details
-
createPool
Creates a generic object pool with default configuration. 使用默认配置创建通用对象池。- Type Parameters:
T- the object type - 对象类型- Parameters:
factory- the object factory - 对象工厂- Returns:
- the object pool - 对象池
-
createPool
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 - 对象池
-
createPool
Creates a pool from a Supplier (simplest API). 从Supplier创建池(最简API)。- Type Parameters:
T- the object type - 对象类型- Parameters:
creator- the object creator - 对象创建器- Returns:
- the object pool - 对象池
-
createPool
Creates a pool from a Supplier with a destroyer Consumer. 从Supplier和销毁Consumer创建池。- Type Parameters:
T- the object type - 对象类型- Parameters:
creator- the object creator - 对象创建器destroyer- the object destroyer - 对象销毁器- Returns:
- the object pool - 对象池
-
createPool
Creates a pool from a Supplier with custom configuration. 从Supplier和自定义配置创建池。- Type Parameters:
T- the object type - 对象类型- Parameters:
creator- the object creator - 对象创建器config- the pool configuration - 池配置- Returns:
- the object pool - 对象池
-
createPool
public static <T> ObjectPool<T> createPool(Supplier<T> creator, Consumer<T> destroyer, PoolConfig config) Creates a pool from a Supplier with a destroyer and custom configuration. 从Supplier、销毁Consumer和自定义配置创建池。- Type Parameters:
T- the object type - 对象类型- Parameters:
creator- the object creator - 对象创建器destroyer- the object destroyer - 对象销毁器config- the pool configuration - 池配置- Returns:
- the object pool - 对象池
-
createKeyedPool
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
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
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
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
Creates a pool configuration builder. 创建池配置构建器。- Returns:
- the builder - 构建器
-
defaultConfig
Gets the default pool configuration. 获取默认池配置。- Returns:
- the default config - 默认配置
-
idleTimeEviction
Creates an idle time eviction policy. 创建空闲时间驱逐策略。- Type Parameters:
T- the object type - 对象类型- Parameters:
maxIdleTime- the maximum idle time - 最大空闲时间- Returns:
- the eviction policy - 驱逐策略
-
lruEviction
Creates an LRU eviction policy. 创建LRU驱逐策略。- Type Parameters:
T- the object type - 对象类型- Parameters:
maxObjects- the maximum objects to keep - 保留的最大对象数- Returns:
- the eviction policy - 驱逐策略
-
lfuEviction
Creates an LFU eviction policy. 创建LFU驱逐策略。- Type Parameters:
T- the object type - 对象类型- Parameters:
minBorrowCount- the minimum borrow count - 最小借用次数- Returns:
- the eviction policy - 驱逐策略
-
allEviction
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
Creates a composite eviction policy (any can match). 创建组合驱逐策略(任一匹配即可)。- Type Parameters:
T- the object type - 对象类型- Parameters:
policies- the policies to combine - 要组合的策略- Returns:
- the eviction policy - 驱逐策略
-
maxAgeEviction
Creates a max age eviction policy. 创建最大生命周期驱逐策略。Evicts objects that have exceeded the specified lifetime since creation. Essential for database connections that must be recycled periodically.
驱逐自创建以来超过指定生命周期的对象。 对于必须定期回收的数据库连接至关重要。
- Type Parameters:
T- the object type - 对象类型- Parameters:
maxLifetime- the maximum object lifetime - 最大对象生命周期- Returns:
- the eviction policy - 驱逐策略
-