Class CommandExecutorPool

java.lang.Object
com.loomcache.server.network.CommandExecutorPool
All Implemented Interfaces:
AutoCloseable

public class CommandExecutorPool extends Object implements AutoCloseable
Pool of virtual threads that execute commands while preserving FIFO order per connection.

Architecture: - Reader thread(s) enqueue CommandContext instances - Commands from the same connection drain through a dedicated serial queue - Different connections still execute concurrently on the shared executor service

Thread-safe: safe for concurrent enqueue and drain operations.

Since:
1.0
  • Constructor Details

    • CommandExecutorPool

      public CommandExecutorPool(int maxExecutorThreads, CommandExecutorPool.CommandHandler handler)
      Create a new executor pool with configurable sizing.
      Parameters:
      maxExecutorThreads - maximum number of executor threads (0 = unlimited virtual threads)
      handler - the callback to execute each dequeued command (non-null)
      Throws:
      NullPointerException - if handler is null
    • CommandExecutorPool

      public CommandExecutorPool(int maxExecutorThreads, CommandExecutorPool.CommandHandler handler, int maxPendingCommands)
  • Method Details

    • start

      public void start(int poolSize)
      Start the executor pool by submitting executor threads.

      Executor threads are submitted to the underlying ExecutorService and begin pulling commands from the queue. This method is idempotent.

      Parameters:
      poolSize - suggested number of executor threads to start (ignored if maxExecutorThreads > 0, uses that instead)
    • enqueue

      public boolean enqueue(CommandContext ctx)
      Enqueue a command for execution.

      Commands from the same connection are serialized on a dedicated queue, while commands from different connections can execute concurrently.

      Parameters:
      ctx - the command context to execute (non-null)
      Returns:
      true if the command was accepted, false if rejected (pool stopped or at capacity)
      Throws:
      NullPointerException - if ctx is null and the pool is running
    • stop

      public void stop()
      Stop the executor pool gracefully.

      Stops accepting new commands, waits for already accepted commands to finish, and then shuts down the executor service. This method is idempotent.

    • isRunning

      public boolean isRunning()
      Check if the executor pool is currently running.
      Returns:
      true if running and accepting commands, false otherwise
    • setSlowOperationDetector

      public void setSlowOperationDetector(@Nullable SlowOperationDetector detector)
    • queueSize

      public int queueSize()
      Get the current approximate queue size.
      Returns:
      number of commands currently waiting in the queue
    • totalEnqueued

      public long totalEnqueued()
      Get total commands enqueued since pool start.
      Returns:
      total enqueue count
    • totalExecuted

      public long totalExecuted()
      Get total commands successfully executed since pool start.
      Returns:
      total execution count
    • totalErrors

      public long totalErrors()
      Get total command execution errors since pool start.

      This includes all exceptions thrown by the command handler.

      Returns:
      total error count
    • close

      public void close()
      Close the executor pool (AutoCloseable implementation).

      Stops the pool gracefully and forces shutdown of the underlying executor service. This method is safe to call multiple times.

      Specified by:
      close in interface AutoCloseable