Class SingleStateful<S>
- All Implemented Interfaces:
Stateful<S>
All state changes (including completion of the states() stream) happen while holding this object's
monitor, so each subscriber sees the changes in the order they happened.
A subscriber is usually notified synchronously on the thread making the change, while that thread holds this object's monitor. This is not guaranteed, though: if a change happens while a new subscriber is still receiving the replayed latest state, the subscribing thread delivers the change to that subscriber possibly after the changing thread has released the monitor. Subscribers must not rely on either behavior.
Consequently, a subscriber must not block, and must not do work that might wait for a lock
held by another thread that could be waiting for this object's monitor. A subscriber that needs to do
anything non-trivial should first move to a different thread (with publishOn, for example).
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Completes thestates()stream.booleancompareAndTransition(S expectedState, S newState) If the expected state is in place the new one is applied and consumers notified.static <S> SingleStateful<S> fromInitial(S initialState) Creates a new stateful component with an initial state.static <S> SingleStateful<S> fromInitial(S initialState, BiConsumer<S, S> beforeTransitionCallback) Creates a new stateful component with an initial state.state()Returns the current state of the stateful component.reactor.core.publisher.Flux<S> states()Returns a stream of states for this component as they change.voidtransition(S newState) Transition into a new state, notifying consumers.
-
Method Details
-
fromInitial
Creates a new stateful component with an initial state.- Parameters:
initialState- the initial state of the component.- Returns:
- an initialized stateful component with the state provided.
-
fromInitial
public static <S> SingleStateful<S> fromInitial(S initialState, BiConsumer<S, S> beforeTransitionCallback) Creates a new stateful component with an initial state.- Parameters:
initialState- the initial state of the component.- Returns:
- an initialized stateful component with the state provided.
-
state
Description copied from interface:StatefulReturns the current state of the stateful component. -
states
Description copied from interface:StatefulReturns a stream of states for this component as they change.Subscribers may be notified synchronously while the component holds internal locks, so they must not block or do non-trivial work on the notifying thread. See
SingleStatefulfor details. -
transition
Transition into a new state, notifying consumers.Note that if the new state is identical to the old state no transition will be performed.
- Parameters:
newState- the new state to apply.
-
compareAndTransition
If the expected state is in place the new one is applied and consumers notified.- Parameters:
expectedState- the old expected state.newState- the new state to apply.- Returns:
- true if the comparison has been successful.
-
close
public void close()Completes thestates()stream. Subsequent transitions still change the current state, but are not emitted.
-