Closed AbstractAlao closed 2 years ago
So I realized the problem was with this method.
stateMachineService.hasStateMachine(id)
Looking at the code it only looks for state machines that are in memory. To get the ones that are in the database you need to use JpaRepositoryStateMachine
StateMachineRepository<JpaRepositoryStateMachine> stateMachineRepository;
Then do the following to test if there is a state machine in the database and create one if there is not.
Optional<JpaRepositoryStateMachine> instance = stateMachineRepository.findById(Integer.toString(loanDetailId));
if (instance.isPresent()){
StateMachine<State, Event> stateMachine = stateMachineService.acquireStateMachine(id);
It is possible to restore a state machine from a DB. Have you configured the persistence fully (especially, specified the runtime persister in the configuration class of your state machine)? Something like this:
// Persistence specific beans:
@Configuration
public class MachinePersist {
@Bean
public StateMachineRuntimePersister<DomainState, DomainEvent, String> stateMachineRuntimePersister(
JpaStateMachineRepository jpaStateMachineRepository) {
return new JpaPersistingStateMachineInterceptor<>(jpaStateMachineRepository);
}
@Bean
public StateMachineService<DomainState, DomainEvent> stateMachineService(
StateMachineFactory<DomainState, DomainEvent> stateMachineFactory,
StateMachineRuntimePersister<DomainState, DomainEvent, String> stateMachineRuntimePersister) {
return new DefaultStateMachineService<>(stateMachineFactory, stateMachineRuntimePersister);
}
}
Additional configuration in your state machine configuration class:
// 1. Autowire the runtime persister
private final StateMachineRuntimePersister<DomainState, DomainEvent, String> stateMachineRuntimePersister;
// 2. Add a runtime persisted to the configure method
@Override
public void configure(StateMachineConfigurationConfigurer<DomainState, DomainEvent> config) throws Exception {
config.withConfiguration().autoStartup(true);
// here it is:
config.withPersistence().runtimePersister(stateMachineRuntimePersister);
}
Technically, it should be enough to make acquireStateMachine(id) working.
So I realized the problem was with this method.
stateMachineService.hasStateMachine(id)
I was using this to check if I had a state machine that already existed. I was assuming it was querying the database.
I ended up doing the following:
StateMachineRepository<JpaRepositoryStateMachine> stateMachineRepository;
Optional<JpaRepositoryStateMachine> instance = stateMachineRepository.findById(Integer.toString(loanDetailId));
if (instance.isPresent()){
StateMachine<State, Event> stateMachine = stateMachineService.acquireStateMachine(id);
I have been following the data persist examples outlined in the spring docs
When I acquire a state machine it doesn't pull it from the database but checks in memory. I see records being written but I just can't restore them. Here is my configuration I have defined the StateRepository and the
Here are my beans based on the examples
I thought restoring them would be as simple as the following:
However, when I step into the DefaultStateMachineService I notice that the machines are empty. Shouldn't it restore when the application starts up or am i missing something