Spring 状态机:如何获取无法进行转换的信息?

Spring State Machine: How to get information that transition is not possible?

当我发送一个在当前机器状态下不可能发生的事件时,我想得到类似异常或任何提示的信息。

没有异常,没有任何反应。我唯一注意到的是:

stateMachine.sendEvent(myMessage)

return 错误。但是下面的方法不return true:

stateMachine.hasStateMachineError()

所以我想也不例外...!?

有人知道我怎么会得到像 "This event can not be applied in the current state of the state machine" 这样的错误吗?

提前致谢!

如果您有未知事件,状态机不会因错误而失败,它会忽略它。 获得有关无效事件的更多信息的唯一方法是添加状态机侦听器:

public class InvalidEventListener extends StateMachineListenerAdapter {

    @Override
    public void eventNotAccepted(Message event) {
        // it is useless to throw an exception here, it is handled at a higher level
        // here you can add custom logic, but limited.
        super.eventNotAccepted(event);
    }
}

您必须将此侦听器添加到您的状态机:

stateMachine.addStateListener(new InvalidEventListener());