当使用新的StateMachineContext重新水合状态机时,使用@OnTransition定义的转换处理程序将停止



在下面的示例代码中,我创建了一个状态机,并用我选择的状态对其进行了重新水合。但是,当我发送事件时,用@OnTransition定义的处理程序不会运行。

如果我将方法调用注释为#rehydreState,那么处理程序将正常工作。为什么会发生这种情况?

此外:如果我更换线路

var sm = factory.getStateMachine("Machine1"); with 
var sm = factory.getStateMachine("");
Then all handlers run fine. Why is that the case?
@Configuration
@EnableStateMachineFactory
public class Config extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineStateConfigurer<String, String> states)
throws Exception {
var allStates = new HashSet<String>();
allStates.add("UNPAID");
allStates.add("WAITING_FOR_RECEIVE");
allStates.add("DONE");
states
.withStates()
.initial("UNPAID")
.states(allStates);
}

@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions)
throws Exception {
transitions
.withExternal()
.source("UNPAID").target("WAITING_FOR_RECEIVE")
.event("PAY")
.and()
.withExternal()
.source("WAITING_FOR_RECEIVE").target("DONE")
.event("RECEIVE")
.and()
.withExternal()
.source("WAITING_FOR_RECEIVE").target("WAITING_FOR_RECEIVE")
.event("SELF");
}

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

@WithStateMachine
static class Action {
@OnTransition(target = "UNPAID")
public void create() {
System.out.println("UNPAID");
}

@OnTransition(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
public void pay() {
System.out.println("WAITING_FOR_RECEIVE");
}

@OnTransition(source = "WAITING_FOR_RECEIVE", target = "WAITING_FOR_RECEIVE")
public void self() {
System.out.println("self works too");
}

@OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE")
public void receive() {
System.out.println("DONE");
}
}
}

public class Main implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}

@Autowired
StateMachineFactory factory;

@Override
public void run(String... args) throws Exception {
var sm = factory.getStateMachine("Machine1");
sm.stopReactively().block();
rehydrateState(sm, sm.getExtendedState(), "WAITING_FOR_RECEIVE");
sm.startReactively().block();
sm.sendEvent("PAY");
sm.sendEvent("SELF");
}

void rehydrateState(StateMachine<String, String> sm, ExtendedState extendedState, String status) {
sm.getStateMachineAccessor().doWithAllRegions(sma -> {
sma.resetStateMachineReactively(new DefaultStateMachineContext<>(status, null, null, extendedState)).block();
});
}
}

通过传递机器Id替换#resetStateMachineReactive方法调用,所有处理程序都开始工作。

sma.resetStateMachineReactively(new DefaultStateMachineContext<>(status, null, null, extendedState, null, sm.getId())).block();

最新更新