Class ReadyIndicator
Ready beans to indicate when the bean has become ready.- Since:
- 1
- Version:
- 9
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback for when an indicator is reset -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a state object that must be marked ready before the indicator is ready.booleanisReady()Indicates if the attached bean is ready or not.booleanisReady(ReadyCallback callback) Returns true if the indicator is ready.voidremove(ReadyCallback callback) Remove theReadyCallbackfrom the ready lists.voidremove(ReadyIndicator.ResetCallback callback) Remove theResetCallbackfrom the reset list.voidreset()Reset the indicator so that is can become ready again.voidsetReady()Call this method to indicate that the indicator is ready.voidsetStateReady(Object state) Call this method to indicate that the specified state is ready, wherestatecorresponds to aaddState(state)call.voidBlock the caller until ready.voidwhenReady(ReadyCallback callback) Calls the specified callback when ready.voidwhenReadyRecurring(ReadyCallback callback) Calls the specified callback when ready.voidwhenReset(ReadyIndicator.ResetCallback callback) Add a reset callback.
-
Constructor Details
-
ReadyIndicator
public ReadyIndicator()
-
-
Method Details
-
addState
Add a state object that must be marked ready before the indicator is ready. Every specified state object must have a corresponding call tosetStateReady(state), in addition to the standard call tosetReady()for this indicator to become ready.This is useful for objects that should only become ready when their normal
setReady()call has been made, such as when called fromBeanContextwhen all dependencies are ready, in addition to some internal initialization process completes, which callssetStateReady(state). This allows any number of states to be defines before the object becomes ready.Calls to
addState()must occur before the indicator is ready to have any effect.- Parameters:
state- the object used to represent the state that is being tracked- Since:
- 1
-
setReady
public void setReady()Call this method to indicate that the indicator is ready. If there are no pending states from callingaddState()then the indicator will be marked ready which will:- Set the ready flag to
true - Call the
onReady()method on each callback object - Clear the list of callbacks
setReady()has been called.- Since:
- 1
- Set the ready flag to
-
setStateReady
Call this method to indicate that the specified state is ready, wherestatecorresponds to aaddState(state)call. This will remove the state from the pending list of states for this indicator. If this is the last state andsetReady()has already been called, the indicator will be marked ready which will:- Set the ready flag to
true - Call the
onReady()method on each callback object - Clear the list of callbacks
setReady()has not been called, this will simply return after removing the state from the pending list.- Parameters:
state- a state object equal to a state provided toaddState()- Since:
- 1
- Set the ready flag to
-
isReady
public boolean isReady()Indicates if the attached bean is ready or not.- Returns:
trueif this bean is ready, otherwisefalse- Since:
- 1
-
isReady
Returns true if the indicator is ready. If not, the callback will be queued and called when the indicator becomes ready. UnlikewhenReady(), this will only use the callback if the indicator is not currently ready.- Returns:
trueif ready; if not, add the givencallbackto the list- Since:
- 1
-
whenReady
Calls the specified callback when ready. If already ready, then the callback is called immediately, otherwise it will be called when the indicator becomes ready. The callback will be called at most one time. Once called, the callback will be removed from the indicator. If the indicator resets and becomes ready again, this callback will not be called a second time. For this use case, usewhenReadyRecurring(com.kosdev.kos.commons.util.ready.ReadyCallback).- Parameters:
callback- called when the indicator becomes ready- Since:
- 1
-
whenReadyRecurring
Calls the specified callback when ready. If already ready, then the callback is called immediately, otherwise it will be called when the indicator becomes ready. The callback will be retained by the indicator and will be called again if the indicator is reset and becomes ready again. This callback can removed usingremove(com.kosdev.kos.commons.util.ready.ReadyCallback).Most indicators, such as when used in a service, only become ready a single time. For these use cases, use
whenReady(com.kosdev.kos.commons.util.ready.ReadyCallback)as it doesn't retain a reference to the callback unless the service never becomes ready. This limits the need to remove callbacks in cleanup code paths.- Parameters:
callback- called when the indicator becomes ready- Since:
- 9
-
whenReset
Add a reset callback. This will be called any time the indicator is reset. As callbacks are retained in the indicator, if there is a need to remove the callback, useremove(com.kosdev.kos.commons.util.ready.ReadyIndicator.ResetCallback).- Parameters:
callback- called when the indicator is reset- Since:
- 9
-
reset
public void reset()Reset the indicator so that is can become ready again. If the indicator originally used multiple states to become ready, the states are not restored, meaning that the indicator will become ready on the next call tosetReady(). If states need to be restored, use aResetCallbackto add the states back when the indicator is reset.If the indicator is not ready, this will be ignored as there is nothing to reset.
Resetting an indicator will call any attached reset callbacks.
-
remove
Remove theReadyCallbackfrom the ready lists.- Parameters:
callback- the callback to remove- Since:
- 9
-
remove
Remove theResetCallbackfrom the reset list.- Parameters:
callback- the callback to remove- Since:
- 9
-
waitUntilReady
public void waitUntilReady()Block the caller until ready.- Since:
- 1
-