spring-statemachine
spring-statemachine copied to clipboard
Sending an event while another event is being processed does not work.
On our project, we wanted to update the library version from 2.0.RELEASE to 4.0.0, but because of this, we started having errors. State machine configurations:
@Override
public void configure(StateMachineTransitionConfigurer<ReplacementState, ReplacementEvent> transitions) throws Exception {
transitions
.withExternal().source(ReplacementState.NEW).target(ReplacementState.PREPARED).event(ReplacementEvent.PREPARE)
.and().withExternal().source(ReplacementState.PREPARED).target(ReplacementState.IN_WORK).event(ReplacementEvent.TO_WORK)
.guard(toWorkGuard())
.action(toWorkAction(), errorHandlingAction())
.action(toReadyToInstallState())
The first action toWorkAction() is handled correctly. During the second action toReadyToInstallState(), a new event READY_TO_INSTALL is sent. But after a new event, the state machine closes without changing the state. In the following StateMachineListenerAdapter code after sending a new event Stage = STATEMACHINE_STOP and the code is not executed by condition.
@Override
public void stateContext(StateContext<ReplacementState, ReplacementEvent> stateContext) {
if (stateContext.getStage().equals(StateContext.Stage.STATE_CHANGED) && stateContext.getEvent() != null) {
var replacement = (AbstractReplacement) stateContext
.getStateMachine()
.getExtendedState()
.getVariables()
.getOrDefault(ReplacementStateMachineContextRepository.ENTITY_CONTEXT_KEY, null);
ReplacementState state = stateContext.getTarget().getId();
if (replacement != null) {
if (ReplacementEvent.CLOSE.equals(stateContext.getEvent())) {
baseReplacementService.updateStateSystem(replacement, stateContext.getEvent(), state);
} else {
baseReplacementService.updateState(replacement, stateContext.getEvent(), state);
}
}
}
}
Let me remind you that no code has changed except for version 2.0.RELEASE to 4.0.0.