【问题标题】:resetStateMachine to set initial state and idresetStateMachine 设置初始状态和 id
【发布时间】:2021-02-24 20:11:44
【问题描述】:

似乎执行以下操作会使 id 为空

# before reset, this will log the id set when state machine was created using stateMachineFactory.getStateMachine(stateMachineId)
log.info(String.format("Before reset, current state: %s, id: %s", stateMachine.getState().getId().toString(), stateMachine.getId()));

# resetting the state machine
stateMachine.getStateMachineAccessor().doWithAllRegions(access -> access
                            .resetStateMachine(new DefaultStateMachineContext<>(correctState, null, null, null)));

# after reset, this will log id as null
log.info(String.format("After reset, state: %s, id: %s", stateMachine.getState().getId().toString(), stateMachine.getId()));

有没有办法再次保留或设置 id?

【问题讨论】:

    标签: spring spring-statemachine


    【解决方案1】:

    调用 resetStateMachine 可能会使您的状态机处于不一致的状态。我正在使用具有 JPA 持久性的状态机,并且重置状态机使我失去了状态机的扩展状态,即使我在 resetStateMachine 函数中设置了扩展状态。

    对我来说,只是停止并启动状态机来重置它,是更好的解决方案。

    3.0 版

    stateMachine.stopReactively().block();
    stateMachine.startReactively().block();
    

    早期版本

    stateMachine.stop();
    stateMachine.start();
    

    【讨论】:

      【解决方案2】:

      我发现 DefaultStateMachineContext 构造函数还有另一个签名:

      /**
           * Instantiates a new default state machine context.
           *
           * @param state the state
           * @param event the event
           * @param eventHeaders the event headers
           * @param extendedState the extended state
           * @param historyStates the history state mappings
           * @param id the machine id
           */
          public DefaultStateMachineContext(S state, E event, Map<String, Object> eventHeaders, ExtendedState extendedState,
                  Map<S, S> historyStates, String id) {
              this(new ArrayList<StateMachineContext<S, E>>(), state, event, eventHeaders, extendedState, historyStates, id);
          }
      

      所以,以下内容对我有用!

      stateMachine.getStateMachineAccessor().doWithAllRegions(access -> access
                                  .resetStateMachine(new DefaultStateMachineContext<>(correctState, null, null, null, null, stateMachine.getId())));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-02
        • 2021-03-10
        • 2018-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        相关资源
        最近更新 更多