Class StatePoller<S,F extends Enum<F>>
Provides polling within a ManagedObjectSource to keep particular
state up to date.
Polling is undertaken by invoking a ProcessState from the
ManagedObjectExecuteContext of the ManagedObjectSource to
load the StatePollContext with the next state value. Also, means to
provide failures that are logged.
This is a convenient and efficient means to make keeping state of a
ManagedObjectSource up to date (without having to manage the
ManagedObjectExecuteContext or separate Timer that needs to
be coordinated with the OfficeFloorListener).
Example use would be keeping JWT keys up to date.
- Author:
- Daniel Sagenschneider
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classStatePoller.Builder<S,F extends Enum<F>> Builder for theStatePoller.static interfaceInitialiser for polling state.static interfaceCustom poller. -
Method Summary
Modifier and TypeMethodDescriptionstatic <S> StatePoller.Builder<S,Indexed> builder(Class<S> stateType, Clock<Long> clock, int flowIndex, ManagedObjectExecuteContext<Indexed> executeContext, Function<StatePollContext<S>, ManagedObject> managedObjectFactory) Creates aStatePoller.BuilderforFlowindex.static <S,F extends Enum<F>>
StatePoller.Builder<S,F> builder(Class<S> stateType, Clock<Long> clock, F flowKey, ManagedObjectExecuteContext<F> executeContext, Function<StatePollContext<S>, ManagedObject> managedObjectFactory) Creates aStatePoller.BuilderforFlowkey.static <S> StatePoller.Builder<S,None> builder(Class<S> stateType, Clock<Long> clock, StatePoller.Initialiser<S> initialiser, StatePoller.Poller<S> poller) Creates aStatePoller.Builderfor customStatePoller.Poller.voidclear()Allows clearing the state.Obtains the current state.Obtains the current state ornullif not yet initialised.voidpoll()Manually trigger poll.static <S,F extends Enum<F>>
StatePoller<S,F> state(S state) Creates an initialisedStatePollerthat does not poll.
-
Method Details
-
state
Creates an initialised
StatePollerthat does not poll.This is useful if state is loaded without needing polling. This allows the
ManagedObjectSourceto poll in some cases and load from configuration in others. This then allows usingStatePollerfor single interface to state.- Type Parameters:
S- State type.F-FlowEnum.- Parameters:
state- State.- Returns:
StatePollerinitialised with state.
-
builder
public static <S,F extends Enum<F>> StatePoller.Builder<S,F> builder(Class<S> stateType, Clock<Long> clock, F flowKey, ManagedObjectExecuteContext<F> executeContext, Function<StatePollContext<S>, ManagedObject> managedObjectFactory) Creates aStatePoller.BuilderforFlowkey.- Type Parameters:
S- State type.F- Flow key type.- Parameters:
stateType- State type.clock-Clock.flowKey-Flowkey to use to invoke the pollingProcessStatefrom theManagedObjectExecuteContext.executeContext-ManagedObjectExecuteContext.managedObjectFactory- Factory to create theManagedObject.- Returns:
StatePoller.Builder.
-
builder
public static <S> StatePoller.Builder<S,Indexed> builder(Class<S> stateType, Clock<Long> clock, int flowIndex, ManagedObjectExecuteContext<Indexed> executeContext, Function<StatePollContext<S>, ManagedObject> managedObjectFactory) Creates aStatePoller.BuilderforFlowindex.- Type Parameters:
S- State type.- Parameters:
stateType- State type.clock-Clock.flowIndex-Flowindex to use to invoke the pollingProcessStatefrom theManagedObjectExecuteContext.executeContext-ManagedObjectExecuteContext.managedObjectFactory- Factory to create theManagedObject.- Returns:
StatePoller.Builder.
-
builder
public static <S> StatePoller.Builder<S,None> builder(Class<S> stateType, Clock<Long> clock, StatePoller.Initialiser<S> initialiser, StatePoller.Poller<S> poller) Creates a
StatePoller.Builderfor customStatePoller.Poller.This allows for higher level types that rely on but do not expose the
ManagedObjectSourceto use thisStatePoller.- Type Parameters:
S- State type.- Parameters:
stateType- State type.clock-Clock.initialiser-StatePoller.Initialiser.poller-StatePoller.Poller.- Returns:
StatePoller.Builder.
-
getStateNow
Obtains the current state or
nullif not yet initialised.This is a non-blocking method to enable obtaining the current state immediately. Useful if state is optional or has defaults, so does not need to wait to be initialised with first poll.
- Returns:
- Current state or
nullif not yet initialised.
-
getState
Obtains the current state.- Parameters:
timeout- The maximum time to wait.unit- The time unit of the timeout.- Returns:
- State.
- Throws:
TimeoutException- If times out waiting on the initial state.
-
poll
public void poll()Manually trigger poll. -
clear
public void clear()Allows clearing the state.
-