Interface PoolEventListener<T>
- Type Parameters:
T- the type of object being pooled - 池化对象类型
public interface PoolEventListener<T>
PoolEventListener - Pool Lifecycle Event Listener
PoolEventListener - 池生命周期事件监听器
Listener interface for receiving notifications about pool lifecycle events such as borrowing, returning, creation, destruction, eviction, exhaustion, and timeout. All methods have default no-op implementations so that users only need to override the events they are interested in.
用于接收池生命周期事件通知的监听器接口,包括借用、归还、创建、销毁、 驱逐、耗尽和超时事件。所有方法都有默认的空操作实现,用户只需覆盖 感兴趣的事件即可。
Features | 主要功能:
- Borrow/return event notification - 借用/归还事件通知
- Create/destroy event notification - 创建/销毁事件通知
- Eviction event notification - 驱逐事件通知
- Pool exhaustion notification - 池耗尽通知
- Timeout notification with wait duration - 带等待时长的超时通知
- Default no-op for all methods - 所有方法默认空操作
Usage Examples | 使用示例:
PoolEventListener<Connection> listener = new PoolEventListener<>() {
@Override
public void onBorrow(Connection conn) {
log.info("Borrowed connection: {}", conn);
}
@Override
public void onExhausted() {
log.warn("Connection pool exhausted!");
}
@Override
public void onTimeout(Duration waitDuration) {
log.error("Timed out after {}ms", waitDuration.toMillis());
}
};
Security | 安全性:
- Thread-safe: Implementation dependent - 线程安全: 取决于实现
- Exception handling: Implementations should not throw - 异常处理: 实现不应抛出异常
- Since:
- JDK 25, opencode-base-pool V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidCalled after an object is successfully borrowed from the pool.default voidCalled after a new object is successfully created by the pool.default voidCalled before an object is destroyed by the pool.default voidCalled before an object is evicted from the pool.default voidCalled when the pool is exhausted (no objects available and at max capacity).default voidCalled after an object is successfully returned to the pool.default voidCalled when a borrow request times out.
-
Method Details
-
onBorrow
Called after an object is successfully borrowed from the pool. 对象从池中成功借出后调用。- Parameters:
object- the borrowed object - 借出的对象
-
onReturn
Called after an object is successfully returned to the pool. 对象成功归还到池中后调用。- Parameters:
object- the returned object - 归还的对象
-
onCreate
Called after a new object is successfully created by the pool. 池成功创建新对象后调用。- Parameters:
object- the created object - 创建的对象
-
onDestroy
Called before an object is destroyed by the pool. 池销毁对象前调用。- Parameters:
object- the object about to be destroyed - 即将被销毁的对象
-
onEvict
Called before an object is evicted from the pool. 对象从池中驱逐前调用。- Parameters:
object- the object about to be evicted - 即将被驱逐的对象
-
onExhausted
default void onExhausted()Called when the pool is exhausted (no objects available and at max capacity). 当池耗尽时调用(无可用对象且已达到最大容量)。 -
onTimeout
Called when a borrow request times out. 当借用请求超时时调用。- Parameters:
waitDuration- the duration the caller waited before timing out - 调用者超时前的等待时长
-