Class ShutdownCoordinator
This coordinator manages the orderly shutdown of transports, ensuring in-flight operations complete (or timeout) before resources are released. It provides a state machine for shutdown phases and notifies listeners of progress.
Shutdown Flow
1. shutdown() called
└─▶ Phase: RUNNING → DRAINING
└─▶ Stop accepting new operations
└─▶ Wait for in-flight operations (up to drainTimeout)
2. Drain complete OR timeout
└─▶ Phase: DRAINING → CLOSING
└─▶ Close all connections
└─▶ Release backend resources
3. Resources released
└─▶ Phase: CLOSING → TERMINATED
└─▶ Notify listeners of completion
Usage Example
ShutdownCoordinator coordinator = new ShutdownCoordinator();
// Track in-flight operations
coordinator.operationStarted(); // When starting a send
coordinator.operationCompleted(); // When send completes
// Initiate graceful shutdown
boolean graceful = coordinator.shutdown(
Duration.ofSeconds(5), // Max time to wait for drain
() -> closeConnections(),
() -> releaseResources()
);
if (graceful) {
System.out.println("Shutdown completed gracefully");
} else {
System.out.println("Forced shutdown due to timeout");
}
Thread Safety
All methods are thread-safe. Phase transitions use atomic operations to ensure correct ordering even with concurrent shutdown requests.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(ShutdownListener listener) Registers a listener for shutdown events.booleanawaitTermination(Duration timeout) Waits for shutdown to complete.intReturns the number of in-flight operations.getPhase()Returns the current shutdown phase.booleanChecks if the coordinator is accepting new operations.booleanChecks if shutdown has completed.voidRecords that an operation has completed.booleanRecords that an operation has started.booleanremoveListener(ShutdownListener listener) Removes a previously registered listener.booleanInitiates graceful shutdown.voidshutdownNow(Runnable connectionCloser, Runnable resourceReleaser) Initiates immediate (non-graceful) shutdown.
-
Constructor Details
-
ShutdownCoordinator
public ShutdownCoordinator()Creates a new shutdown coordinator in RUNNING state.
-
-
Method Details
-
getPhase
-
isAcceptingOperations
public boolean isAcceptingOperations()Checks if the coordinator is accepting new operations.- Returns:
- true only if in RUNNING phase
-
isTerminated
public boolean isTerminated()Checks if shutdown has completed.- Returns:
- true if in TERMINATED phase
-
getInFlightCount
public int getInFlightCount()Returns the number of in-flight operations.- Returns:
- the count of operations not yet completed
-
addListener
Registers a listener for shutdown events.Listeners can be added at any time. If shutdown has already started, the listener will receive subsequent phase changes but not previous ones.
- Parameters:
listener- the listener to register
-
removeListener
Removes a previously registered listener.- Parameters:
listener- the listener to remove- Returns:
- true if the listener was found and removed
-
operationStarted
public boolean operationStarted()Records that an operation has started.Call this before starting any tracked operation (send, receive, etc.). Must be paired with
operationCompleted().- Returns:
- true if the operation was accepted, false if shutdown is in progress
-
operationCompleted
public void operationCompleted()Records that an operation has completed.Call this when any tracked operation completes (successfully or with error).
-
shutdown
public boolean shutdown(Duration drainTimeout, Runnable connectionCloser, Runnable resourceReleaser) throws InterruptedException Initiates graceful shutdown.This method blocks until shutdown is complete or the timeout expires. It progresses through all shutdown phases, calling the provided callbacks at appropriate points.
- Parameters:
drainTimeout- maximum time to wait for in-flight operations to completeconnectionCloser- callback to close connections (called in CLOSING phase)resourceReleaser- callback to release resources (called before TERMINATED)- Returns:
- true if shutdown was graceful, false if forced due to timeout
- Throws:
InterruptedException- if the calling thread is interrupted
-
shutdownNow
-
awaitTermination
Waits for shutdown to complete.- Parameters:
timeout- maximum time to wait- Returns:
- true if terminated within timeout
- Throws:
InterruptedException- if interrupted while waiting
-