Reputation: 1
My goal: configure State Machine to persist internal data at any changes, restore from DB when I need to use it.
Realisation: Spring Docs
Problem: It works fine at one start (saving to db and restoring). After restart, state machine resets to initial (with losing all data) at first usage.
Relevant State machine config:
@EnableStateMachineFactory
@Configuration
@AllArgsConstructor
public class StateMachineConfig extends StateMachineConfigurerAdapter<ConversationState, BotEvent> {
private final StateMachineRuntimePersister<ConversationState, BotEvent, String> stateMachineRuntimePersister;
@Bean
public StateMachineService<ConversationState, BotEvent> stateMachineService(
StateMachineFactory<ConversationState, BotEvent> stateMachineFactory,
StateMachineRuntimePersister<ConversationState, BotEvent, String> stateMachineRuntimePersister) {
return new DefaultStateMachineService<ConversationState, BotEvent>(stateMachineFactory, stateMachineRuntimePersister);
}
@Override
public void configure(StateMachineConfigurationConfigurer<ConversationState, BotEvent> config) throws Exception {
config.withConfiguration()
.listener(listener())
.autoStartup(true)
.and()
.withPersistence().runtimePersister(stateMachineRuntimePersister)
;
}
// states, transitions, actions etc. config...
}
@Configuration
public class JpaPersisterConfig {
@Bean
public StateMachineRuntimePersister<ConversationState, BotEvent, String> stateMachineRuntimePersister(
JpaStateMachineRepository jpaStateMachineRepository) {
return new JpaPersistingStateMachineInterceptor<>(jpaStateMachineRepository);
}
}
Usage:
@Service
@AllArgsConstructor
public class SomeClass{
private StateMachineService<ConversationState, BotEvent> stateMachineService;
public void someMethod(String userId) {
StateMachine<ConversationState, BotEvent> stateMachine = restoreStateMachine(userId);
// work with stateMachine, sending events
}
}
Upvotes: 0
Views: 36