美文网首页状态机
spring-statemachine状态持久化

spring-statemachine状态持久化

作者: java0110 | 来源:发表于2019-06-02 21:11 被阅读0次

同步deploy于:wtx博客

持久化到内存hashmap中

总结: 实现StateMachinePersist接口,并通过实现write和read方法,然后构造DefaultStateMachinePersister bean

FSMStateMachinePersist 实现接口 StateMachinePersist

package com.wtx.springboot2.statemachine;

import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.support.DefaultStateMachineContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wtx
 * @Date 2019/6/2
 */

@Component
public class FSMStateMachinePersist implements StateMachinePersist<FSMStates, FSMEvents, Integer> {

    static Map<Integer, FSMStates> cache = new HashMap<>(16);

    @Override
    public void write(StateMachineContext<FSMStates, FSMEvents> stateMachineContext, Integer integer) throws Exception {
        cache.put(integer, stateMachineContext.getState());
    }

    @Override
    public StateMachineContext<FSMStates, FSMEvents> read(Integer integer) throws Exception {
        // 注意状态机的初识状态与配置中定义的一致
        return cache.containsKey(integer) ?
                new DefaultStateMachineContext<>(cache.get(integer), null, null, null, null, "stateMachine") :
                new DefaultStateMachineContext<>(FSMStates.A, null, null, null, null, "stateMachine");
    }
}

然后在 StatemachineConfigurer 中 注入:FSMStateMachinePersist


@Autowired
   private FSMStateMachinePersist fsmStateMachinePersist;

   @Bean
   public StateMachinePersister<FSMStates, FSMEvents, Integer> stateMachinePersist() {
       return new DefaultStateMachinePersister<>(fsmStateMachinePersist);
   }

最后在main 函数中注入:

@Autowired
 private StateMachinePersister<FSMStates, FSMEvents, Integer> stateMachinePersist;
package com.wtx.springboot2.statemachine;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.persist.StateMachinePersister;

/**
 * @Author wtx
 * @Date 2019/6/2
 */
@SpringBootApplication
public class StatemachineApplication implements CommandLineRunner {
    @Autowired
    private StateMachine<FSMStates, FSMEvents> stateMachine;
    @Autowired
    private StateMachinePersister<FSMStates, FSMEvents, Integer> stateMachinePersist;

    @Override
    public void run(String... args) throws Exception {
        stateMachine.start();

        stateMachinePersist.restore(stateMachine, 1);
        System.out.println("------");
        stateMachine.sendEvent(FSMEvents.ToB);
        stateMachinePersist.persist(stateMachine, 1);

        stateMachinePersist.restore(stateMachine, 1);
        System.out.println("------");
        stateMachine.sendEvent(FSMEvents.ToC);
        stateMachinePersist.persist(stateMachine, 1);

        stateMachinePersist.restore(stateMachine, 1);
        System.out.println("------");
        stateMachine.sendEvent(FSMEvents.ToD);
        stateMachinePersist.persist(stateMachine, 1);

        stateMachinePersist.restore(stateMachine, 1);
        System.out.println("------");
        stateMachine.sendEvent(FSMEvents.ToA);
        stateMachinePersist.persist(stateMachine, 1);
        stateMachine.stop();
    }
    public static void main(String[] args) {
        SpringApplication.run(StatemachineApplication.class, args);
    }

}

相关文章

网友评论

    本文标题:spring-statemachine状态持久化

    本文链接:https://www.haomeiwen.com/subject/xbdfxctx.html