Class ConnectionStateMachine
This class enforces valid state transitions and notifies listeners of state changes. It provides a robust foundation for connection management with proper state validation.
Valid Transitions
NEW → CONNECTING, CLOSED CONNECTING → CONNECTED, FAILED, CLOSING CONNECTED → CLOSING, FAILED FAILED → CONNECTING, CLOSED CLOSING → CLOSED CLOSED → (terminal, no transitions)
Usage Example
ConnectionStateMachine state = new ConnectionStateMachine();
// Add listener for state changes
state.addListener((prev, curr, cause) -> {
logger.info("State: {} -> {}", prev, curr);
});
// Connection flow
state.transitionTo(ConnectionState.CONNECTING); // Start connection
state.transitionTo(ConnectionState.CONNECTED); // Success
// ... later ...
state.transitionTo(ConnectionState.CLOSING); // Graceful close
state.transitionTo(ConnectionState.CLOSED); // Complete
Thread Safety
All methods are thread-safe. State transitions use atomic operations to ensure consistency even with concurrent access.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new state machine inConnectionState.NEWstate.ConnectionStateMachine(String connectionId) Creates a new state machine with an identifier. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(ConnectionStateListener listener) Registers a listener for state change events.voidforceState(ConnectionState newState, Throwable cause) Forces a transition to a state, bypassing validation.Returns the connection identifier.getState()Returns the current state.static Set<ConnectionState> Returns the set of valid target states from a given state.booleanisActive()Checks if the connection is active.booleanisClosed()Checks if the connection is fully closed.booleanChecks if the connection is closed or closing.static booleanisValidTransition(ConnectionState from, ConnectionState to) Checks if a transition from one state to another is valid.booleanremoveListener(ConnectionStateListener listener) Removes a previously registered listener.voidsetConnectionId(String connectionId) Sets the connection identifier.toString()booleantransitionFrom(ConnectionState expectedState, ConnectionState newState) Attempts to transition from a specific expected state.booleantransitionFrom(ConnectionState expectedState, ConnectionState newState, Throwable cause) Attempts to transition from a specific expected state with a cause.booleantransitionTo(ConnectionState newState) Attempts to transition to a new state.booleantransitionTo(ConnectionState newState, Throwable cause) Attempts to transition to a new state with a cause.
-
Constructor Details
-
ConnectionStateMachine
public ConnectionStateMachine()Creates a new state machine inConnectionState.NEWstate. -
ConnectionStateMachine
Creates a new state machine with an identifier.- Parameters:
connectionId- identifier for this connection
-
-
Method Details
-
getState
-
getConnectionId
Returns the connection identifier.- Returns:
- the connection ID, or null if not set
-
setConnectionId
Sets the connection identifier.- Parameters:
connectionId- the identifier
-
isActive
public boolean isActive()Checks if the connection is active.- Returns:
- true if in
ConnectionState.CONNECTEDstate
-
isClosedOrClosing
public boolean isClosedOrClosing()Checks if the connection is closed or closing.- Returns:
- true if in CLOSING or CLOSED state
-
isClosed
public boolean isClosed()Checks if the connection is fully closed.- Returns:
- true if in CLOSED state
-
addListener
Registers a listener for state change events.- 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
-
transitionTo
Attempts to transition to a new state.The transition succeeds only if it's a valid transition from the current state. Listeners are notified on successful transition.
- Parameters:
newState- the desired new state- Returns:
- true if the transition was successful
-
transitionTo
Attempts to transition to a new state with a cause.The cause is passed to listeners and is typically used for failure transitions.
- Parameters:
newState- the desired new statecause- the reason for the transition (may be null)- Returns:
- true if the transition was successful
-
transitionFrom
Attempts to transition from a specific expected state.This is useful for conditional transitions where you want to ensure the current state hasn't changed since you checked it.
- Parameters:
expectedState- the expected current statenewState- the desired new state- Returns:
- true if transition successful, false if current state doesn't match
-
transitionFrom
public boolean transitionFrom(ConnectionState expectedState, ConnectionState newState, Throwable cause) Attempts to transition from a specific expected state with a cause.- Parameters:
expectedState- the expected current statenewState- the desired new statecause- the reason for the transition (may be null)- Returns:
- true if transition successful
-
forceState
Forces a transition to a state, bypassing validation.Warning: This should only be used in exceptional circumstances, such as error recovery. Normal code should use
transitionTo(ConnectionState).- Parameters:
newState- the state to forcecause- the reason for forced transition
-
isValidTransition
Checks if a transition from one state to another is valid.- Parameters:
from- the source stateto- the target state- Returns:
- true if the transition is allowed
-
getValidTransitions
Returns the set of valid target states from a given state.- Parameters:
from- the source state- Returns:
- set of valid target states
-
toString
-