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.Object
Provides polling within a
ManagedObjectSource
to keep particular state up to date.Polling is undertaken by invoking a
ProcessState
from theManagedObjectExecuteContext
of theManagedObjectSource
to load theStatePollContext
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 theManagedObjectExecuteContext
or separateTimer
that 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 class
StatePoller.Builder<S,F extends java.lang.Enum<F>>
Builder for theStatePoller
.static interface
StatePoller.Initialiser<S>
Initialiser for polling state.static interface
StatePoller.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.Builder
forFlow
index.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.Builder
forFlow
key.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.Builder
for customStatePoller.Poller
.void
clear()
Allows clearing the state.S
getState(long timeout, java.util.concurrent.TimeUnit unit)
Obtains the current state.S
getStateNow()
Obtains the current state ornull
if not yet initialised.void
poll()
Manually trigger poll.static <S,F extends java.lang.Enum<F>>
StatePoller<S,F>state(S state)
Creates an initialisedStatePoller
that does not poll.
-
-
-
Method Detail
-
state
public static <S,F extends java.lang.Enum<F>> StatePoller<S,F> state(S state)
Creates an initialised
StatePoller
that does not poll.This is useful if state is loaded without needing polling. This allows the
ManagedObjectSource
to poll in some cases and load from configuration in others. This then allows usingStatePoller
for single interface to state.- Type Parameters:
S
- State type.F
-Flow
Enum
.- Parameters:
state
- State.- Returns:
StatePoller
initialised 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.Builder
forFlow
key.- Parameters:
stateType
- State type.clock
-Clock
.flowKey
-Flow
key to use to invoke the pollingProcessState
from 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.Builder
forFlow
index.- Parameters:
stateType
- State type.clock
-Clock
.flowIndex
-Flow
index to use to invoke the pollingProcessState
from 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.Builder
for customStatePoller.Poller
.This allows for higher level types that rely on but do not expose the
ManagedObjectSource
to 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
null
if 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
null
if 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.
-
-