Spring 状态机 - 变量在 Action 中为空

Spring State Machine - Variable is empty in Action

尽管我有 Spring 状态机,但我还是遇到了问题。我已经声明了一个动作,我想从扩展状态中获取一个变量,但是我所有的变量都是空的。

这是我的配置,我用 Action 配置了第一个转换 startMeasureAction() 我想得到一个我发送到状态机的变量。

@Configuration
@EnableStateMachine
public class MeasureStateMachineConfig extends StateMachineConfigurerAdapter<State, MeasureEvents> {

private final Logger logger = LoggerFactory.getLogger(MeasureStateMachineConfig.class);

@Override
public void configure(StateMachineConfigurationConfigurer<State, MeasureEvents> config) throws Exception {

    StateMachineListenerAdapter<State, MeasureEvents> adapter =
        new StateMachineListenerAdapter<State, MeasureEvents>() {

            @Override
            public void stateChanged(org.springframework.statemachine.state.State<State, MeasureEvents> from,
                                     org.springframework.statemachine.state.State<State, MeasureEvents> to) {
                logger.info(format("stateChanged(from: %s, to %s)", from + "", to + ""));
            }
        };

    config.withConfiguration().autoStartup(true).listener(adapter);
}

@Override
public void configure(StateMachineStateConfigurer<State, MeasureEvents> states) throws Exception {
    states.withStates()
        .initial(State.OPEN)
        .states(EnumSet.allOf(State.class));
}

@Override
public void configure(StateMachineTransitionConfigurer<State, MeasureEvents> transition) throws Exception {
    transition
        .withInternal().source(State.OPEN).event(OPEN).action(startMeasureAction())
        .and()
        .withExternal().source(State.OPEN).source(INPROGRESS).event(START)
        .and()
        .withExternal().source(State.OPEN).target(REJECTED).event(REJECT)
        .and()
        .withExternal().source(REJECTED).target(State.OPEN).event(OPEN)
        .and()
        .withExternal().source(INPROGRESS).target(IMPLEMENTED).event(IMPLEMENT)
        .and()
        .withExternal().source(IMPLEMENTED).target(State.INEFFECTIVE).event(INEFFECTIVE)
        .and()
        .withExternal().source(State.IMPLEMENTED).target(FINISHED).event(FINISH)
        .and()
        .withExternal().source(State.INEFFECTIVE).target(State.OPEN).event(OPEN)
        .and()
        .withExternal().source(FINISHED).target(State.OPEN).event(OPEN);
}

@Bean
public Action<State, MeasureEvents> startMeasureAction() {
    return new MeasureStartAction();
}

}

我的逻辑,我在其中发送带有数据的事件。这里我设置了一个Header,将数据和事件发送给了状态机。但是在 Action (MeasureStartAction) 中调试时,没有可用的变量。

@Override
public void save(Measure entity) {
    validator.validate(entity);

    Message<MeasureEvents> startMeasureMessage =
        MessageBuilder.withPayload(OPEN).setHeader("MEASURE_ID", 1L).build();
    sm.sendEvent(startMeasureMessage);

}

这是我的Action,这里我要获取变量。

public class MeasureStartAction implements Action<State, MeasureEvents> {

private static final Logger logger = LoggerFactory.getLogger(MeasureStartAction.class);

@Override
public void execute(StateContext<State, MeasureEvents> context) {

    final long measure = context.getExtendedState().get("MEASURE_ID", Long.class);

    logger.info("measureID" + measure);
}

}

测量总是空的,我做错了什么?

我想问题出在你的 save() 方法上。
我在 Spring State-Machine documentation 中做了一点搜索,我发现只有一个使用了“MessageBuilder.withPayload(EVENT)”。要使用它,您需要准备一些处理程序。

所以我的建议是以更简单的方式更改变量放置代码:

sm.getExtendedState().getVariables().put("MEASURE_ID", 1L);
sm.sendEvent(OPEN);