Enhance things around StateMachinePersister

- Polish StateMachinePersister
- Add redis specific RedisStateMachinePersister
- Add more docs.
- Change eventservice sample to use StateMachinePersister.
- Relates #184
- Relates #185
This commit is contained in:
Janne Valkealahti
2016-03-11 18:43:45 +00:00
parent 5cd7d348e9
commit 66fe71e9cc
9 changed files with 333 additions and 36 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
import org.springframework.statemachine.persist.RepositoryStateMachinePersist;
import org.springframework.statemachine.redis.RedisStateMachineContextRepository;
import org.springframework.statemachine.redis.RedisStateMachinePersister;
@Configuration
public class StateMachineConfig {
@@ -214,6 +215,12 @@ public class StateMachineConfig {
new RedisStateMachineContextRepository<States, Events>(connectionFactory);
return new RepositoryStateMachinePersist<States, Events>(repository);
}
@Bean
public RedisStateMachinePersister<States, Events> redisStateMachinePersister(
StateMachinePersist<States, Events, String> stateMachinePersist) {
return new RedisStateMachinePersister<States, Events>(stateMachinePersist);
}
//end::snippetD[]
@Bean

View File

@@ -18,11 +18,7 @@ package demo.eventservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.access.StateMachineAccess;
import org.springframework.statemachine.access.StateMachineFunction;
import org.springframework.statemachine.support.DefaultStateMachineContext;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
@@ -44,7 +40,7 @@ public class StateMachineController {
private StateMachine<States, Events> stateMachine;
@Autowired
private StateMachinePersist<States, Events, String> stateMachinePersist;
private StateMachinePersister<States, Events, String> stateMachinePersister;
//end::snippetA[]
@Autowired
@@ -90,25 +86,13 @@ public class StateMachineController {
//tag::snippetD[]
private void feedMachine(String user, Events id) throws Exception {
stateMachine.sendEvent(id);
stateMachinePersist.write(new DefaultStateMachineContext<States, Events>(stateMachine.getState().getId(), null, null,
stateMachine.getExtendedState()), "testprefix:" + user);
stateMachinePersister.persist(stateMachine, "testprefix:" + user);
}
//end::snippetD[]
//tag::snippetE[]
private StateMachine<States, Events> resetStateMachineFromStore(String user) throws Exception {
final StateMachineContext<States, Events> context = stateMachinePersist.read("testprefix:" + user);
stateMachine.stop();
stateMachine.getStateMachineAccessor()
.doWithAllRegions(new StateMachineFunction<StateMachineAccess<States, Events>>() {
@Override
public void apply(StateMachineAccess<States, Events> function) {
function.resetStateMachine(context);
}
});
stateMachine.start();
return stateMachine;
return stateMachinePersister.restore(stateMachine, "testprefix:" + user);
}
//end::snippetE[]
}