Class StatePoller<S,F extends java.lang.Enum<F>>
- java.lang.Object
-
- net.officefloor.plugin.managedobject.poll.StatePoller<S,F>
-
public class StatePoller<S,F extends java.lang.Enum<F>> extends java.lang.ObjectProvides polling within a
ManagedObjectSourceto keep particular state up to date.Polling is undertaken by invoking a
ProcessStatefrom theManagedObjectExecuteContextof theManagedObjectSourceto load theStatePollContextwith 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
ManagedObjectSourceup to date (without having to manage theManagedObjectExecuteContextor separateTimerthat needs to be coordinated with theOfficeFloorListener).Example use would be keeping JWT keys up to date.
- Author:
- Daniel Sagenschneider
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStatePoller.Builder<S,F extends java.lang.Enum<F>>Builder for theStatePoller.static interfaceStatePoller.Initialiser<S>Initialiser for polling state.static interfaceStatePoller.Poller<S>Custom poller.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <S> StatePoller.Builder<S,Indexed>builder(java.lang.Class<S> stateType, Clock<java.lang.Long> clock, int flowIndex, ManagedObjectExecuteContext<Indexed> executeContext, java.util.function.Function<StatePollContext<S>,ManagedObject> managedObjectFactory)Creates aStatePoller.BuilderforFlowindex.static <S,F extends java.lang.Enum<F>>
StatePoller.Builder<S,F>builder(java.lang.Class<S> stateType, Clock<java.lang.Long> clock, F flowKey, ManagedObjectExecuteContext<F> executeContext, java.util.function.Function<StatePollContext<S>,ManagedObject> managedObjectFactory)Creates aStatePoller.BuilderforFlowkey.static <S> StatePoller.Builder<S,None>builder(java.lang.Class<S> stateType, Clock<java.lang.Long> clock, StatePoller.Initialiser<S> initialiser, StatePoller.Poller<S> poller)Creates aStatePoller.Builderfor customStatePoller.Poller.voidclear()Allows clearing the state.SgetState(long timeout, java.util.concurrent.TimeUnit unit)Obtains the current state.SgetStateNow()Obtains the current state ornullif not yet initialised.voidpoll()Manually trigger poll.static <S,F extends java.lang.Enum<F>>
StatePoller<S,F>state(S state)Creates an initialisedStatePollerthat does not poll.
-
-
-
Method Detail
-
state
public static <S,F extends java.lang.Enum<F>> StatePoller<S,F> state(S 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 java.lang.Enum<F>> StatePoller.Builder<S,F> builder(java.lang.Class<S> stateType, Clock<java.lang.Long> clock, F flowKey, ManagedObjectExecuteContext<F> executeContext, java.util.function.Function<StatePollContext<S>,ManagedObject> managedObjectFactory)
Creates aStatePoller.BuilderforFlowkey.- 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(java.lang.Class<S> stateType, Clock<java.lang.Long> clock, int flowIndex, ManagedObjectExecuteContext<Indexed> executeContext, java.util.function.Function<StatePollContext<S>,ManagedObject> managedObjectFactory)
Creates aStatePoller.BuilderforFlowindex.- 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(java.lang.Class<S> stateType, Clock<java.lang.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.- Parameters:
stateType- State type.clock-Clock.initialiser-StatePoller.Initialiser.poller-StatePoller.Poller.- Returns:
StatePoller.Builder.
-
getStateNow
public S 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
public S getState(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException
Obtains the current state.- Parameters:
timeout- The maximum time to wait.unit- The time unit of the timeout.- Returns:
- State.
- Throws:
java.util.concurrent.TimeoutException- If times out waiting on the initial state.
-
poll
public void poll()
Manually trigger poll.
-
clear
public void clear()
Allows clearing the state.
-
-