我正在尝试使用Spring状态机完成一项复杂的任务!指文档状态机有两种类型的操作,步骤操作和事务操作!可以从状态动作内部调用事件!
但是s1()
,s2()
,..不要打电话!似乎@EnableWithStateMachine
无法正常工作!
我的配置类:
@Configuration
public class DeploySM {
@Configuration
@EnableStateMachineFactory
@EnableWithStateMachine
public static class Config extends EnumStateMachineConfigurerAdapter<Manifest.Step, Manifest.Event> {
@Override
public void configure(StateMachineStateConfigurer<Manifest.Step, Manifest.Event> states) throws Exception {
super.configure(states);
states.withStates().initial(Manifest.Step.INIT)
.state(Manifest.Step.S1 )
.state(Manifest.Step.S2)
.state(Manifest.Step.S3)
.state(Manifest.Step.S5)
.exit(Manifest.Step.S4);
}
@Override
public void configure(StateMachineConfigurationConfigurer<Manifest.Step, Manifest.Event> config) throws Exception {
super.configure(config);
config.withConfiguration()
.autoStartup(false);
}
@Override
public void configure(StateMachineTransitionConfigurer<Manifest.Step, Manifest.Event> transitions) throws Exception {
super.configure(transitions);
transitions.withExternal()
.source(Manifest.Step.INIT).target(Manifest.Step.S1)
.event(Manifest.Event.SUCCESS)
.and().withExternal()
.source(Manifest.Step.S1).target(Manifest.Step.S2)
.event(Manifest.Event.SUCCESS)
.and().withExternal()
.source(Manifest.Step.S1).target(Manifest.Step.S5)
.event(Manifest.Event.FAILED)
//
.and()
.withExternal()
.source(Manifest.Step.S2).target(Manifest.Step.S3)
.event(Manifest.Event.BUILD_SOURCE)
.and().withExternal()
.source(Manifest.Step.S2).target(Manifest.Step.S4)
.event(Manifest.Event.SUCCESS)
.and().withExternal()
.source(Manifest.Step.S2).target(Manifest.Step.FAILED)
.event(Manifest.Event.FAILED);
}
状态机动作bean:
@EnableWithStateMachine
public static class DeploySMActionsBean {
@Bean
public DeploySMActions deploySM() {
return new DeploySMActions();
}
}
状态机操作类:
@WithStateMachine(name = "", id = "deploySM")
@Slf4j
public class DeploySMActions extends StateMachineActions {
private Manifest manifest;
@OnStateEntry(source = "INITIAL", target = "INIT")
public void init() {
manifest = new Manifest();
log.info("init state: " + manifest.getUuid());
sm.sendEvent(Manifest.Event.SUCCESS);
}
@OnStateEntry(source = "INIT", target = "S1")
public void s1() {
log.info("validate_config state: " + manifest.getUuid());
sm.sendEvent(Manifest.Event.SUCCESS);
}
@OnStateEntry(source = "S1", target = "S2")
public void s2() {
log.info("create application");
sm.sendEvent(Manifest.Event.FAILED);
}
//and other steps
}