StateMachineService.acquireStateMachine 不应该启动 stateMachine



使用stateMachineService获取状态机时,机器启动,但我将'false'作为第二个参数传递。

stateMachine = stateMachineService.acquireStateMachine(id, false)

根据控制台输出"acquireStateMachine"启动机器。

我正在使用DefaultStateMachineService

@Bean
public StateMachineService<BookingItemState, BookingItemEvent> stateMachineService(
StateMachineFactory<BookingItemState, BookingItemEvent> stateMachineFactory,
StateMachineRuntimePersister<BookingItemState, BookingItemEvent, String> stateMachineRuntimePersister) {
return new DefaultStateMachineService<>(stateMachineFactory, stateMachineRuntimePersister);
}

问题在DefaultStateMachineService类中。我想您已经将SM配置为如下启用autoStartap属性:

@Override
public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
config
.withConfiguration()
.autoStartup(true);
}

如果您调用acquireStateMachine,DefaultStateMachineService将使用stateMachineFactory创建一个新的SM(但您的SM已启用autoStartup(,它将启动一个新SM并将其存储到DB。

让我们考虑一下metnod:

public StateMachine<S, E> acquireStateMachine(String machineId, boolean start) {
log.info("Acquiring machine with id " + machineId);
StateMachine<S, E> stateMachine;
// naive sync to handle concurrency with release
synchronized (machines) {
stateMachine = machines.get(machineId);
if (stateMachine == null) {
log.info("Getting new machine from factory with id " + machineId);
stateMachine = stateMachineFactory.getStateMachine(machineId);
if (stateMachinePersist != null) {
try {
StateMachineContext<S, E> stateMachineContext = stateMachinePersist.read(machineId);
stateMachine = restoreStateMachine(stateMachine, stateMachineContext);
} catch (Exception e) {
log.error("Error handling context", e);
throw new StateMachineException("Unable to read context from store", e);
}
}
machines.put(machineId, stateMachine);
}
}
// handle start outside of sync as it might take some time and would block other machines acquire
return handleStart(stateMachine, start);
}

为了避免此问题,您可以禁用autoStartup选项或实现自定义StateMachineService。但是您必须显式调用stateMachine.start((.

最新更新