spring-StateMachine

public class FSMT {

public static void main(String[] args)throws Exception {

StateMachineBuilder.Builder builder = StateMachineBuilder.builder();

        builder.configureConfiguration()

.withConfiguration()

.machineId("a").listener(listener())

;

        //必须指定initial 过程中有多种状态变更需要指定 state states 指定end后当状态变更为end指定状态 后续流程将不再执行 比如BE.REFUND_2 event

        builder.configureStates()

.withStates()

.initial(AE.INIT).states(EnumSet.allOf(AE.class)).end(AE.END)

//                .and().withStates().initial(AE.STA).end(AE.ING)

        ;

        //可以指定多个状态变更流程 event是钥匙 需要后续StateMachine发送这个事件

        builder.configureTransitions()

.withExternal().event(BE.REFUND_FAILED).source(AE.INIT).target(AE.ING).action(BC.builder().build())

.and().withExternal().source(AE.ING).target(AE.INIT).event(BE.CLOSE_REFUND).action(AC.builder().build())

.and().withExternal().source(AE.INIT).target(AE.END).event(BE.REFUND_1).action(AC.builder().build())

.and().withExternal().source(AE.END).target(AE.INIT).event(BE.REFUND_2).action(AC.builder().build())

;

        StateMachine sm = builder.build();

        sm.start();

        //3. 发送当前请求的状态

        System.out.println(sm.sendEvent(BE.REFUND_FAILED));

        System.out.println(sm.sendEvent(BE.CLOSE_REFUND));

        System.out.println(sm.sendEvent(BE.REFUND_1));

        System.out.println(sm.sendEvent(BE.REFUND_2));

    }

public static StateMachineListenerlistener() {

return new StateMachineListenerAdapter() {

StringstateChanged ="stateChanged";

            @Override

            public void stateChanged(State from, State to) {

if (!Objects.isNull(from)) {

System.out.println(from.getId() +"~~~~~~~~" +stateChanged +"from");

                }

if (!Objects.isNull(to)) {

System.out.println(to.getId() +"~~~~~~~~" +stateChanged +"to");

                }

}

@Override

            public void transition(Transition transition) {

System.out.println(transition.getTarget().getId() +"~~~~~~~~" +"transition");

            }

};

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容