we have observed one thread for statemachine is getting impacted because of others and is getting stopped abruptly. for this we needed to make a lock on every state changes for that statemachine.
Do we alternative solution here ? can statemachine provide thread safety here ?
code snippet
@component
@slf4j
public class TicketStateHandler extends LifecycleObjectSupport{
private final Lock lock;
private void handleEvent(Message event, TicketStatus sourceState) {
lock.lock();
try {
stateMachine.stop();
stateMachine
.getStateMachineAccessor()
.doWithAllRegions(
access ->
access.resetStateMachine(
new DefaultStateMachineContext<>(sourceState, null, null, null)));
stateMachine.start();
boolean accepted = stateMachine.sendEvent(event);
if (!accepted) {
throw new InvalidBookingStateTrasitionException(
"Invalid Ticket State Transition error. Current Ticket State is "
+ sourceState.toString(),
new ExceptionMessages(ErrorCodes.ERR2103.toString(), sourceState.toString()));
}
log.info("Exit handleEvent()");
} finally {
lock.unlock();
}
}
}