Add autostart flag for top-level machine - take2
- Add actual start calls - Rename autoStart to autoStartup in config methods. - Tweak so that this works in factory and builder
This commit is contained in:
@@ -25,6 +25,7 @@ import java.util.Map.Entry;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
@@ -85,6 +86,8 @@ public abstract class AbstractStateMachineFactory<S, E> extends LifecycleObjectS
|
||||
|
||||
private Boolean contextEvents;
|
||||
|
||||
private boolean handleAutostartup = false;
|
||||
|
||||
/**
|
||||
* Instantiates a new enum state machine factory.
|
||||
*
|
||||
@@ -213,13 +216,24 @@ public abstract class AbstractStateMachineFactory<S, E> extends LifecycleObjectS
|
||||
machine = distributedStateMachine;
|
||||
}
|
||||
|
||||
return machine;
|
||||
return delegateAutoStartup(machine);
|
||||
}
|
||||
|
||||
public void setHandleAutostartup(boolean handleAutostartup) {
|
||||
this.handleAutostartup = handleAutostartup;
|
||||
}
|
||||
|
||||
public void setContextEventsEnabled(Boolean contextEvents) {
|
||||
this.contextEvents = contextEvents;
|
||||
}
|
||||
|
||||
private StateMachine<S, E> delegateAutoStartup(StateMachine<S, E> delegate) {
|
||||
if (handleAutostartup && delegate instanceof SmartLifecycle && ((SmartLifecycle) delegate).isAutoStartup()) {
|
||||
((SmartLifecycle)delegate).start();
|
||||
}
|
||||
return delegate;
|
||||
}
|
||||
|
||||
private BeanFactory resolveBeanFactory() {
|
||||
if (stateMachineConfigurationConfig.getBeanFactory() != null) {
|
||||
return stateMachineConfigurationConfig.getBeanFactory();
|
||||
|
||||
@@ -114,6 +114,7 @@ public class StateMachineBuilder {
|
||||
StateMachineConfigurationConfig<S, E> stateMachineConfigurationConfig = stateMachineConfig.getStateMachineConfigurationConfig();
|
||||
ObjectStateMachineFactory<S, E> stateMachineFactory = new ObjectStateMachineFactory<S, E>(
|
||||
stateMachineConfigurationConfig, stateMachineTransitions, stateMachineStates);
|
||||
stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());
|
||||
|
||||
if (stateMachineConfigurationConfig.getBeanFactory() != null) {
|
||||
stateMachineFactory.setBeanFactory(stateMachineConfigurationConfig.getBeanFactory());
|
||||
@@ -124,7 +125,6 @@ public class StateMachineBuilder {
|
||||
if (stateMachineConfigurationConfig.getTaskScheduler() != null) {
|
||||
stateMachineFactory.setTaskScheduler(stateMachineConfigurationConfig.getTaskScheduler());
|
||||
}
|
||||
|
||||
return stateMachineFactory.getStateMachine();
|
||||
} catch (Exception e) {
|
||||
throw new StateMachineException("Error building state machine", e);
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
@@ -66,10 +67,12 @@ public class StateMachineConfiguration<S extends Enum<S>, E extends Enum<E>> ext
|
||||
}
|
||||
|
||||
private static class StateMachineDelegatingFactoryBean<S extends Enum<S>, E extends Enum<E>>
|
||||
extends BeanDelegatingFactoryBean<StateMachine<S, E>,StateMachineConfigBuilder<S, E>,StateMachineConfig<S, E>> {
|
||||
extends BeanDelegatingFactoryBean<StateMachine<S, E>,StateMachineConfigBuilder<S, E>,StateMachineConfig<S, E>>
|
||||
implements SmartLifecycle {
|
||||
|
||||
private String clazzName;
|
||||
private Boolean contextEvents;
|
||||
private SmartLifecycle lifecycle;
|
||||
|
||||
public StateMachineDelegatingFactoryBean(StateMachineConfigBuilder<S, E> builder, Class<StateMachine<S, E>> clazz,
|
||||
String clazzName, Boolean contextEvents) {
|
||||
@@ -94,7 +97,39 @@ public class StateMachineConfiguration<S extends Enum<S>, E extends Enum<E>> ext
|
||||
stateMachineConfigurationConfig, stateMachineTransitions, stateMachineStates);
|
||||
stateMachineFactory.setBeanFactory(getBeanFactory());
|
||||
stateMachineFactory.setContextEventsEnabled(contextEvents);
|
||||
setObject(stateMachineFactory.getStateMachine());
|
||||
StateMachine<S, E> stateMachine = stateMachineFactory.getStateMachine();
|
||||
this.lifecycle = (SmartLifecycle) stateMachine;
|
||||
setObject(stateMachine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
lifecycle.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
lifecycle.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return lifecycle.isRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return lifecycle.isAutoStartup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(Runnable callback) {
|
||||
lifecycle.stop(callback);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,9 +60,9 @@ public interface ConfigurationConfigurer<S, E> extends
|
||||
* Specify if state machine should be started automatically.
|
||||
* On default state machine is not started automatically.
|
||||
*
|
||||
* @param autoStart the autostart flag
|
||||
* @param autoStartup the autoStartup flag
|
||||
* @return configurer for chaining
|
||||
*/
|
||||
ConfigurationConfigurer<S, E> autoStart(boolean autoStart);
|
||||
ConfigurationConfigurer<S, E> autoStartup(boolean autoStartup);
|
||||
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class DefaultConfigurationConfigurer<S, E>
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigurationConfigurer<S, E> autoStart(boolean autoStart) {
|
||||
public ConfigurationConfigurer<S, E> autoStartup(boolean autoStart) {
|
||||
this.autoStart = autoStart;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -119,6 +119,7 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
||||
ObjectStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
assertThat(machine.isAutoStartup(), is(true));
|
||||
assertThat(machine.isRunning(), is(true));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -450,7 +451,7 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
||||
public void configure(StateMachineConfigurationConfigurer<TestStates, TestEvents> config) throws Exception {
|
||||
config
|
||||
.withConfiguration()
|
||||
.autoStart(true);
|
||||
.autoStartup(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.core.task.SyncTaskExecutor;
|
||||
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
@@ -137,6 +138,33 @@ public class ManualBuilderTests {
|
||||
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoStartFlagOn() throws Exception {
|
||||
Builder<String, String> builder = StateMachineBuilder.builder();
|
||||
|
||||
builder.configureConfiguration()
|
||||
.withConfiguration()
|
||||
.autoStartup(true)
|
||||
.taskExecutor(new SyncTaskExecutor())
|
||||
.taskScheduler(new ConcurrentTaskScheduler());
|
||||
|
||||
builder.configureStates()
|
||||
.withStates()
|
||||
.initial("S1").state("S2");
|
||||
|
||||
builder.configureTransitions()
|
||||
.withExternal()
|
||||
.source("S1").target("S2").event("E1")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source("S2").target("S1").event("E2");
|
||||
|
||||
StateMachine<String, String> stateMachine = builder.build();
|
||||
|
||||
assertThat(((SmartLifecycle)stateMachine).isAutoStartup(), is(true));
|
||||
assertThat(((SmartLifecycle)stateMachine).isRunning(), is(true));
|
||||
}
|
||||
|
||||
static class Config extends StateMachineConfigurerAdapter<String, String> {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user