Package com.tnsai.coordination.pool
Class LazyAgentPool
java.lang.Object
com.tnsai.coordination.pool.LazyAgentPool
- All Implemented Interfaces:
Closeable,AutoCloseable
A lazy agent pool that creates agents on demand and manages their lifecycle.
Supports configurable activation policies, idle eviction, and pool metrics.
Usage:
LazyAgentPool pool = LazyAgentPool.builder()
.factory(() -> new MyAgent(...))
.maxSize(20)
.minIdle(2)
.idleTimeout(5, TimeUnit.MINUTES)
.policy(ActivationPolicy.PREWARMED)
.build();
try (AgentHandle handle = pool.checkout()) {
String result = handle.execute("Do something");
} // agent is returned to pool
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(PoolListener listener) Adds a listener for pool events.static LazyAgentPool.Builderbuilder()Returns a new builder.checkout()Checks out an agent from the pool.voidclose()Shuts down the pool, destroying all agents.Returns a snapshot of pool metrics.intpreWarm(int count) Pre-warms the pool by creating idle agents up to the specified count.voidremoveListener(PoolListener listener) Removes a previously added listener.
-
Method Details
-
checkout
Checks out an agent from the pool. The returned handle must be closed to return the agent.- Returns:
- a handle wrapping the checked-out agent
- Throws:
IllegalStateException- if the pool is closed or exhausted
-
preWarm
public int preWarm(int count) Pre-warms the pool by creating idle agents up to the specified count. Will not exceed maxSize. Useful for predictive activation.- Parameters:
count- number of agents to pre-warm- Returns:
- actual number of agents created
-
getMetrics
Returns a snapshot of pool metrics. -
addListener
Adds a listener for pool events. -
removeListener
Removes a previously added listener. -
close
public void close()Shuts down the pool, destroying all agents.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
builder
Returns a new builder.
-