Class GenericKeyedObjectPool<K,V>

java.lang.Object
cloud.opencode.base.pool.impl.GenericKeyedObjectPool<K,V>
Type Parameters:
K - the key type - 键类型
V - the value type - 值类型
All Implemented Interfaces:
KeyedObjectPool<K,V>, AutoCloseable

public class GenericKeyedObjectPool<K,V> extends Object implements KeyedObjectPool<K,V>
GenericKeyedObjectPool - Generic Keyed Object Pool Implementation GenericKeyedObjectPool - 通用键控对象池实现

Keyed object pool that manages separate pools for each key. Useful for multi-tenant or multi-datasource scenarios.

为每个键管理独立池的键控对象池。适用于多租户或多数据源场景。

Features | 主要功能:

  • Per-key pool management - 每键池管理
  • Lazy pool creation - 延迟池创建
  • Shared configuration per pool - 每池共享配置
  • Thread-safe key operations - 线程安全的键操作

Usage Examples | 使用示例:

KeyedPooledObjectFactory<String, Connection> factory =
    new BaseKeyedPooledObjectFactory<>() {
        @Override
        protected Connection create(String dsName) {
            return getDataSource(dsName).getConnection();
        }
    };

GenericKeyedObjectPool<String, Connection> pool =
    new GenericKeyedObjectPool<>(factory, config);

Connection masterConn = pool.borrowObject("master");
Connection slaveConn = pool.borrowObject("slave");

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap, AtomicBoolean) - 线程安全: 是(ConcurrentHashMap,AtomicBoolean)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-pool V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • GenericKeyedObjectPool

      public GenericKeyedObjectPool(KeyedPooledObjectFactory<K,V> factory)
      Creates a keyed pool with default configuration. 使用默认配置创建键控池。
      Parameters:
      factory - the keyed factory - 键控工厂
    • GenericKeyedObjectPool

      public GenericKeyedObjectPool(KeyedPooledObjectFactory<K,V> factory, PoolConfig config)
      Creates a keyed pool with custom configuration. 使用自定义配置创建键控池。
      Parameters:
      factory - the keyed factory - 键控工厂
      config - the pool configuration - 池配置
  • Method Details

    • borrowObject

      public V borrowObject(K key) throws OpenPoolException
      Description copied from interface: KeyedObjectPool
      Borrows an object for the given key. 为给定的键借用对象。
      Specified by:
      borrowObject in interface KeyedObjectPool<K,V>
      Parameters:
      key - the key - 键
      Returns:
      the borrowed object - 借用的对象
      Throws:
      OpenPoolException - if borrowing fails - 如果借用失败
    • borrowObject

      public V borrowObject(K key, Duration timeout) throws OpenPoolException
      Description copied from interface: KeyedObjectPool
      Borrows an object for the given key with timeout. 带超时为给定的键借用对象。
      Specified by:
      borrowObject in interface KeyedObjectPool<K,V>
      Parameters:
      key - the key - 键
      timeout - the maximum wait time - 最大等待时间
      Returns:
      the borrowed object - 借用的对象
      Throws:
      OpenPoolException - if borrowing fails or times out - 如果借用失败或超时
    • returnObject

      public void returnObject(K key, V obj)
      Description copied from interface: KeyedObjectPool
      Returns an object for the given key. 归还给定键的对象。
      Specified by:
      returnObject in interface KeyedObjectPool<K,V>
      Parameters:
      key - the key - 键
      obj - the object to return - 要归还的对象
    • invalidateObject

      public void invalidateObject(K key, V obj)
      Description copied from interface: KeyedObjectPool
      Invalidates an object for the given key. 使给定键的对象失效。
      Specified by:
      invalidateObject in interface KeyedObjectPool<K,V>
      Parameters:
      key - the key - 键
      obj - the object to invalidate - 要失效的对象
    • getNumIdle

      public int getNumIdle(K key)
      Description copied from interface: KeyedObjectPool
      Gets the idle count for the given key. 获取给定键的空闲数。
      Specified by:
      getNumIdle in interface KeyedObjectPool<K,V>
      Parameters:
      key - the key - 键
      Returns:
      the idle count - 空闲数
    • getNumActive

      public int getNumActive(K key)
      Description copied from interface: KeyedObjectPool
      Gets the active count for the given key. 获取给定键的活跃数。
      Specified by:
      getNumActive in interface KeyedObjectPool<K,V>
      Parameters:
      key - the key - 键
      Returns:
      the active count - 活跃数
    • clear

      public void clear(K key)
      Description copied from interface: KeyedObjectPool
      Clears objects for the given key. 清除给定键的对象。
      Specified by:
      clear in interface KeyedObjectPool<K,V>
      Parameters:
      key - the key - 键
    • clear

      public void clear()
      Description copied from interface: KeyedObjectPool
      Clears all objects for all keys. 清除所有键的所有对象。
      Specified by:
      clear in interface KeyedObjectPool<K,V>
    • getNumKeys

      public int getNumKeys()
      Description copied from interface: KeyedObjectPool
      Gets the total number of keys. 获取键的总数。
      Specified by:
      getNumKeys in interface KeyedObjectPool<K,V>
      Returns:
      the key count - 键数
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • getTotalNumIdle

      public int getTotalNumIdle()
      Gets total idle count across all keys. 获取所有键的空闲总数。
      Returns:
      the total idle count - 空闲总数
    • getTotalNumActive

      public int getTotalNumActive()
      Gets total active count across all keys. 获取所有键的活跃总数。
      Returns:
      the total active count - 活跃总数