0

我在使用 Spring statemachine + Spring boot 应用程序时遇到了一些问题。

我正在使用 Redis 进行持久化,就像在事件服务演示中一样。我有一个在状态机进入状态“验证”时执行的操作。在其中,我修改了扩展状态变量。下面的代码示例经过简化,但经过测试:

这是主要的调用方法:

 public void feedMachine(String event, int orderItem) throws Exception {

    System.out.println("DEBUG FEEDMACHINE STARTED");
    StateMachine<String, String> machine = persister.restore(stateMachine, orderItem);
    machine.sendEvent(event);
    System.out.println("DEBUG FEEDMACHINE... machine = " + machine.getUuid());
    System.out.println("DEBUG FEEDMACHINE... variables = " + machine.getExtendedState().getVariables());
    if (machine.getExtendedState().getVariables().containsKey("guard")
            && !machine.getExtendedState().get("guard", Boolean.class)) {
        System.out.println("DEBUG FEEDMACHINE... guard = " + machine.getExtendedState().get("guard", Boolean.class));
        throw new GuardedEventException();
    }
    persister.persist(machine, orderItem);
    System.out.println("DEBUG FEEDMACHINE ENDED");
}

这是动作:

@Bean
public Action<String, String> CustomerOrderValidatedEntryAction() {
    return new Action<String, String>() {
        @Override
        public void execute(StateContext context) {
            System.out.println("DEBUG ACTION STARTED");
            System.out.println("DEBUG ACTION... machine = " + context.getStateMachine().getUuid());

            int orderItem = context.getStateMachine().getExtendedState().get(ORDER_ITEM, Integer.class);
            context.getExtendedState().getVariables().put("guard", false);
            System.out.println("DEBUG ACTION... variables = " + context.getExtendedState().getVariables());
                persister.persist(context.getStateMachine(), key);
                System.out.println("DEBUG ACTION ENDED");
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}

当我离开操作时,在 feedMachine extendedState.getVariables 中不包含警卫和错误变量。

这是输出:

DEBUG FEEDMACHINE STARTED
2016-08-16 11:46:17.101  INFO 26483 --- [nio-8081-exec-2] o.s.s.support.LifecycleObjectSupport     : stopped org.springframework.statemachine.support.DefaultStateMachineExecutor@a85eba8
2016-08-16 11:46:17.102  INFO 26483 --- [nio-8081-exec-2] o.s.s.support.LifecycleObjectSupport     : stopped ...  /  / uuid=336abc3e-708e-46ad-892f-5835b50713c8 / id=null
2016-08-16 11:46:17.102  INFO 26483 --- [nio-8081-exec-2] o.s.s.support.LifecycleObjectSupport     : started org.springframework.statemachine.support.DefaultStateMachineExecutor@a85eba8
2016-08-16 11:46:17.102  INFO 26483 --- [nio-8081-exec-2] o.s.s.support.LifecycleObjectSupport     : started ...  / ORDER_RECEIVED / uuid=336abc3e-708e-46ad-892f-5835b50713c8 / id=null
DEBUG ACTION STARTED
DEBUG ACTION... machine = 336abc3e-708e-46ad-892f-5835b50713c8
DEBUG ACTION... guard = false
DEBUG ACTION... variables = {orderItem=0, guard=false}
DEBUG ACTION ENDED
DEBUG FEEDMACHINE... machine = 336abc3e-708e-46ad-892f-5835b50713c8
DEBUG FEEDMACHINE... variables = {orderItem=0}
DEBUG FEEDMACHINE ENDED

任何帮助,将不胜感激。我不确定这是否与 Spring 状态机或 Spring bean 或其他东西有关。我为原始代码道歉(使用 println 而不是 log.debug 等)。

编辑:

我终于发现问题出在哪里了。我在行动中使用context.getExtendedState().getVariables().put("guard", valid);而不是context.getStateMachine().getExtendedState().getVariables().put("guard", valid);. 现在它按预期工作。问题是这两个变量列表不一样。

  • context.getExtendedState()的变量列表将更新为“守卫”
  • context.getStateMachine().getExtendedState()的变量列表不会得到更新。

但是,在创建了最小的工作示例(只有 3 个状态而不是 15+,没有嵌套状态)之后,它与context.getExtendedState().getVariables().put("guard", valid);. 几天前我还检查了context.getExtendedState()context.getStateMachine().getExtendedState()是否相同,我得出的结论是它们是相同的。

在我看来,这似乎是一个错误。

4

0 回答 0