Class LazyAgentPool

java.lang.Object
com.tnsai.coordination.pool.LazyAgentPool
All Implemented Interfaces:
Closeable, AutoCloseable

public class LazyAgentPool extends Object implements Closeable
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
 
  • Method Details

    • checkout

      public AgentHandle 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

      public PoolMetrics getMetrics()
      Returns a snapshot of pool metrics.
    • addListener

      public void addListener(PoolListener listener)
      Adds a listener for pool events.
    • removeListener

      public void removeListener(PoolListener listener)
      Removes a previously added listener.
    • close

      public void close()
      Shuts down the pool, destroying all agents.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • builder

      public static LazyAgentPool.Builder builder()
      Returns a new builder.