目前Spring状态机通过StateMachinePersister接口的实现,完成状态机状态的存储
目前已经提供的存储方式包含Redis,mongoDB,Jpa三种实现,分别对应JpaStateMachineRuntimePersister,RedisStateMachineRuntimePersister 和MongoDbStateMachineRuntimePersister
其中StateContext序列化的方式为kryo,序列化和反序列化都在实现类中自行实现了
基于redis的实例化工具使用
/* 实例化工具配置部分代码,在StateMachine配置类中 */
// 初始化redis连接工厂
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
redisConfig.setHostName("1.1.1.1");
redisConfig.setPassword(RedisPassword.of("xxxx"));
redisConfig.setPort(6379);
return new JedisConnectionFactory(redisConfig);
}
// 通过名称进行区分(带有不同枚举导致不能混用)
@Bean(name = "newStateMachinePersister")
public RedisStateMachinePersister<NewStates, NewEvents> redisStateMachinePersister(RedisConnectionFactory connectionFactory) {
RedisStateMac