S
- the states enumeration.E
- the events enumeration.@ThreadSafe public final class DefaultStateMachineService<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>> extends java.lang.Object implements StateMachineService<S,E>
A FSM-based service implementation.
These services should be built with StateMachineServiceBuilder
.
StateMachineServiceBuilder
Modifier and Type | Class and Description |
---|---|
private class |
DefaultStateMachineService.Dispatcher |
Modifier and Type | Field and Description |
---|---|
private E |
currentEvent |
private State<S,E> |
currentState |
private @Nullable java.util.concurrent.ScheduledFuture<?> |
dispatcherFuture |
private @Nullable EventBus |
eventBus |
private java.util.Queue<E> |
eventQueue |
private java.util.function.Consumer<java.lang.Throwable> |
exceptionHandler |
private java.util.Queue<java.lang.Throwable> |
exceptions |
private boolean |
failed |
private S |
initialState |
private static org.slf4j.Logger |
log |
private S |
nextState |
private @Nullable ListeningScheduledExecutorService |
service |
private java.lang.String |
serviceName |
private java.util.concurrent.locks.StampedLock |
stateLock |
private io.vavr.collection.HashMap<S,State<S,E>> |
statesMap |
private boolean |
synchronous |
private boolean |
terminated |
Constructor and Description |
---|
DefaultStateMachineService(StateMachineServiceBuilder<S,E> builder)
Package-protected constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
awaitTermination() |
E |
currentEvent() |
S |
currentState()
Returns the current state of the machine.
|
(package private) void |
drainEvents() |
(package private) void |
execute() |
void |
failWithError(java.lang.Throwable t) |
void |
fire(E event)
Fires an event.
|
void |
forceShutdown()
Shutdowns and cleans up the service even if it have not terminate yet.
|
void |
goTo(S state) |
private void |
internalShutdown() |
private void |
internalTermination() |
boolean |
isFailed()
Tells whether the machine has failed.
|
boolean |
isInState(S state)
Checks atomically whether the service is in the given state.
|
boolean |
isRunning()
Returns whether the service is isRunning.
|
private boolean |
isRunningNotSynchronized() |
boolean |
isTerminated()
Tells whether the machine is already terminated.
|
void |
shutdown()
Shutdowns and cleans up the service.
|
java.lang.String |
toString() |
private static final org.slf4j.Logger log
private final java.lang.String serviceName
private final boolean synchronous
private final io.vavr.collection.HashMap<S extends java.lang.Enum<S>,State<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>>> statesMap
private final java.util.function.Consumer<java.lang.Throwable> exceptionHandler
private final java.util.Queue<java.lang.Throwable> exceptions
private final java.util.concurrent.locks.StampedLock stateLock
private final @Nullable ListeningScheduledExecutorService service
private final @Nullable java.util.concurrent.ScheduledFuture<?> dispatcherFuture
private final @Nullable EventBus eventBus
private volatile boolean failed
private volatile boolean terminated
DefaultStateMachineService(StateMachineServiceBuilder<S,E> builder)
Package-protected constructor.
The proper way to build the service is to use the builder StateMachineServiceBuilder
.
builder
- a builder containing the state machine definition.public void fire(E event)
StateMachineService
Fires an event.
public void goTo(S state)
public boolean isRunning()
StateMachineService
Returns whether the service is isRunning.
private boolean isRunningNotSynchronized()
public boolean isInState(S state)
StateMachineService
Checks atomically whether the service is in the given state.
public boolean isTerminated()
StateMachineService
Tells whether the machine is already terminated.
isTerminated
in interface StateMachineService<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>>
public boolean isFailed()
StateMachineService
Tells whether the machine has failed.
public void awaitTermination() throws java.lang.InterruptedException
awaitTermination
in interface StateMachineService<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>>
java.lang.InterruptedException
public void shutdown()
StateMachineService
Shutdowns and cleans up the service.
shutdown
in interface StateMachineService<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>>
StateMachineService.isTerminated()
public void forceShutdown()
StateMachineService
Shutdowns and cleans up the service even if it have not terminate yet.
forceShutdown
in interface StateMachineService<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>>
StateMachineService.isTerminated()
,
StateMachineService.shutdown()
public S currentState()
StateMachineService
Returns the current state of the machine.
currentState
in interface StateMachineService<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>>
public void failWithError(java.lang.Throwable t)
failWithError
in interface StateMachineService<S extends java.lang.Enum<S>,E extends java.lang.Enum<E>>
public E currentEvent()
public java.lang.String toString()
toString
in class java.lang.Object
void drainEvents()
void execute()
private void internalTermination()
private void internalShutdown()