【问题标题】:Spring State Machine - Variable is empty in ActionSpring 状态机 - 变量在操作中为空
【发布时间】:2020-06-03 07:19:59
【问题描述】:

尽管我使用了 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);
}

}

Measure 总是为空,我做错了什么?

【问题讨论】:

    标签: spring spring-statemachine


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-19
      • 2023-04-04
      • 1970-01-01
      • 2017-06-03
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多