Class CommandExecutorPool
- All Implemented Interfaces:
AutoCloseable
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for command execution. -
Constructor Summary
ConstructorsConstructorDescriptionCommandExecutorPool(int maxExecutorThreads, CommandExecutorPool.CommandHandler handler) Create a new executor pool with configurable sizing.CommandExecutorPool(int maxExecutorThreads, CommandExecutorPool.CommandHandler handler, int maxPendingCommands) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the executor pool (AutoCloseable implementation).booleanenqueue(CommandContext ctx) Enqueue a command for execution.booleanCheck if the executor pool is currently running.intGet the current approximate queue size.voidsetSlowOperationDetector(@Nullable SlowOperationDetector detector) voidstart(int poolSize) Start the executor pool by submitting executor threads.voidstop()Stop the executor pool gracefully.longGet total commands enqueued since pool start.longGet total command execution errors since pool start.longGet total commands successfully executed since pool start.
-
Constructor Details
-
CommandExecutorPool
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
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
-
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:
closein interfaceAutoCloseable
-