我正在尝试使用像MySQL这样的RDBMS来持久化spring状态机。例如,我需要存储和检索由spring状态机控制的实体的状态。我似乎找不到合适的文档或任何具有整个配置的示例代码。我所能找到的只是一些示例代码片段,它们并没有显示持久化所需要做的一切。谁有演示如何在RDBMS上配置spring状态机的示例代码?
感谢您看过持久化配方吗?这也许有用。
我不能分享我的代码,但这里是你可能需要的
1 -依赖关系implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.statemachine:spring-statemachine-autoconfigure")
implementation("org.springframework.statemachine:spring-statemachine-data-jpa")
implementation("org.springframework.statemachine:spring-statemachine-starter")
2 -代码2.1机器配置(我使用硬编码工厂策略)
@Configuration
@EnableStateMachineFactory(contextEvents = false)
@RequiredArgsConstructor
public class StateMachineConfig
extends EnumStateMachineConfigurerAdapter<S,E >
2.2持久化配置
@Configuration
public class StateMachinePersistenceConfiguration {
@Bean
public StateMachineRuntimePersister<S, E, String> stateMachineRuntimePersister(
final JpaStateMachineRepository jpaStateMachineRepository) {
return new JpaPersistingStateMachineInterceptor<>(jpaStateMachineRepository);
}
}
2.3服务配置(允许检索机器)
@RequiredArgsConstructor
@Configuration
public class StateMachineServiceConfig {
private final StateMachineFactory<S, E> stateMachineFactory;
private final StateMachineRuntimePersister<S, E, String> stateMachineRuntimePersister;
@Bean
public StateMachineService<S, E> stateMachineService() {
return new DefaultStateMachineService<>(stateMachineFactory, stateMachineRuntimePersister);
}
}
框架应该创建一个状态机表以允许您持久化状态机