Change persistence model structure
- Fix wrong acquire logic in DefaultStatemachineService. - Overhaul StateMachineRuntimePersister - Change StateMachineRuntimePersister to extend StateMachinePersist which should work better on a bean level. Also add generic type T to it and config where needed. - Relates to #432 - Relates to #427
This commit is contained in:
@@ -75,7 +75,7 @@ public class StateMachineConfigurationBuilder<S, E>
|
||||
private SecurityRule transitionSecurityRule;
|
||||
private StateMachineMonitor<S, E> stateMachineMonitor;
|
||||
private final List<StateMachineInterceptor<S, E>> interceptors = new ArrayList<StateMachineInterceptor<S, E>>();
|
||||
private StateMachineRuntimePersister<S, E> persister;
|
||||
private StateMachineRuntimePersister<S, E, ?> persister;
|
||||
|
||||
/**
|
||||
* Instantiates a new state machine configuration builder.
|
||||
@@ -289,7 +289,7 @@ public class StateMachineConfigurationBuilder<S, E>
|
||||
*
|
||||
* @param persister the state machine runtime persister
|
||||
*/
|
||||
public void setStateMachineRuntimePersister(StateMachineRuntimePersister<S, E> persister) {
|
||||
public void setStateMachineRuntimePersister(StateMachineRuntimePersister<S, E, ?> persister) {
|
||||
this.persister = persister;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class DefaultPersistenceConfigurer<S, E> extends
|
||||
AnnotationConfigurerAdapter<ConfigurationData<S, E>, StateMachineConfigurationConfigurer<S, E>, StateMachineConfigurationBuilder<S, E>>
|
||||
implements PersistenceConfigurer<S, E> {
|
||||
|
||||
private StateMachineRuntimePersister<S, E> persister;
|
||||
private StateMachineRuntimePersister<S, E, ?> persister;
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineConfigurationBuilder<S, E> builder) throws Exception {
|
||||
@@ -41,7 +41,7 @@ public class DefaultPersistenceConfigurer<S, E> extends
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistenceConfigurer<S, E> runtimePersister(StateMachineRuntimePersister<S, E> persister) {
|
||||
public PersistenceConfigurer<S, E> runtimePersister(StateMachineRuntimePersister<S, E, ?> persister) {
|
||||
this.persister = persister;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -36,5 +36,5 @@ public interface PersistenceConfigurer<S, E> extends
|
||||
* @param persister the state machine runtime persister
|
||||
* @return configurer for chaining
|
||||
*/
|
||||
PersistenceConfigurer<S, E> runtimePersister(StateMachineRuntimePersister<S, E> persister);
|
||||
PersistenceConfigurer<S, E> runtimePersister(StateMachineRuntimePersister<S, E, ?> persister);
|
||||
}
|
||||
|
||||
@@ -49,9 +49,10 @@ import org.springframework.statemachine.transition.TransitionKind;
|
||||
*
|
||||
* @param <S> the type of state
|
||||
* @param <E> the type of event
|
||||
* @param <T> the type of persister context object
|
||||
*/
|
||||
public abstract class AbstractPersistingStateMachineInterceptor<S, E> extends StateMachineInterceptorAdapter<S, E>
|
||||
implements StateMachinePersist<S, E, Object> {
|
||||
public abstract class AbstractPersistingStateMachineInterceptor<S, E, T> extends StateMachineInterceptorAdapter<S, E>
|
||||
implements StateMachinePersist<S, E, T> {
|
||||
|
||||
@Override
|
||||
public void preStateChange(State<S, E> state, Message<E> message, Transition<S, E> transition, StateMachine<S, E> stateMachine) {
|
||||
@@ -85,7 +86,7 @@ public abstract class AbstractPersistingStateMachineInterceptor<S, E> extends St
|
||||
* @param context the state machine context
|
||||
* @param contextObj the context object
|
||||
*/
|
||||
public abstract void write(StateMachineContext<S, E> context, Object contextObj) throws Exception;
|
||||
public abstract void write(StateMachineContext<S, E> context, T contextObj) throws Exception;
|
||||
|
||||
/**
|
||||
* Read {@link StateMachineContext} from persistent store.
|
||||
@@ -93,7 +94,7 @@ public abstract class AbstractPersistingStateMachineInterceptor<S, E> extends St
|
||||
* @param contextObj the context object
|
||||
* @return the state machine context
|
||||
*/
|
||||
public abstract StateMachineContext<S, E> read(Object contextObj) throws Exception;
|
||||
public abstract StateMachineContext<S, E> read(T contextObj) throws Exception;
|
||||
|
||||
/**
|
||||
* Builds the state machine context.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.statemachine.persist;
|
||||
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.StateMachinePersist;
|
||||
import org.springframework.statemachine.support.StateMachineInterceptor;
|
||||
|
||||
/**
|
||||
@@ -25,8 +26,9 @@ import org.springframework.statemachine.support.StateMachineInterceptor;
|
||||
*
|
||||
* @param <S> the type of state
|
||||
* @param <E> the type of event
|
||||
* @param <T> the type of persister context object
|
||||
*/
|
||||
public interface StateMachineRuntimePersister<S, E> {
|
||||
public interface StateMachineRuntimePersister<S, E, T> extends StateMachinePersist<S, E, T> {
|
||||
|
||||
/**
|
||||
* Gets a {@link StateMachineInterceptor} handling machine persistence.
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.statemachine.service;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.StateMachineContext;
|
||||
import org.springframework.statemachine.StateMachineException;
|
||||
@@ -37,6 +39,7 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class DefaultStateMachineService<S, E> implements StateMachineService<S, E> {
|
||||
|
||||
private final static Log log = LogFactory.getLog(DefaultStateMachineService.class);
|
||||
private final StateMachineFactory<S, E> stateMachineFactory;
|
||||
private final Map<String, StateMachine<S, E>> machines = new HashMap<String, StateMachine<S, E>>();
|
||||
private StateMachinePersist<S, E, String> stateMachinePersist;
|
||||
@@ -65,30 +68,34 @@ public class DefaultStateMachineService<S, E> implements StateMachineService<S,
|
||||
|
||||
@Override
|
||||
public StateMachine<S, E> acquireStateMachine(String machineId) {
|
||||
log.info("Acquiring machine with id " + machineId);
|
||||
synchronized (machines) {
|
||||
StateMachine<S,E> stateMachine = machines.get(machineId);
|
||||
if (stateMachine == null) {
|
||||
log.info("Getting new machine from factory with id " + machineId);
|
||||
stateMachine = stateMachineFactory.getStateMachine(machineId);
|
||||
if (stateMachinePersist != null) {
|
||||
try {
|
||||
StateMachineContext<S, E> stateMachineContext = stateMachinePersist.read(machineId);
|
||||
stateMachine = restoreStateMachine(stateMachine, stateMachineContext);
|
||||
} catch (Exception e) {
|
||||
log.error("Error handling context", e);
|
||||
throw new StateMachineException("Unable to read context from store", e);
|
||||
}
|
||||
}
|
||||
machines.put(machineId, stateMachine);
|
||||
}
|
||||
if (stateMachinePersist != null) {
|
||||
try {
|
||||
StateMachineContext<S, E> stateMachineContext = stateMachinePersist.read(machineId);
|
||||
return restoreStateMachine(stateMachine, stateMachineContext);
|
||||
} catch (Exception e) {
|
||||
throw new StateMachineException("Unable to read context from store", e);
|
||||
}
|
||||
} else {
|
||||
return stateMachine;
|
||||
}
|
||||
return stateMachine;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseStateMachine(String machineId) {
|
||||
log.info("Releasing machine with id " + machineId);
|
||||
synchronized (machines) {
|
||||
StateMachine<S, E> stateMachine = machines.remove(machineId);
|
||||
if (stateMachine != null) {
|
||||
log.info("Found machine with id " + machineId);
|
||||
stateMachine.stop();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user