resetstatemachine设置初始状态和id

34gzjxbg  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(276)

似乎这样做会使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?

s2j5cfk0

s2j5cfk01#

我发现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())));

相关问题