Add autostart flag for top-level machine
- Fixes #82 - Now have configure method ConfigurationConfigurer.autoStart(boolean) which can be used to enable autostart feature for top-level state machine. We keep this feature disabled as default because most of the cases it is easier to enable it than disable it.
This commit is contained in:
@@ -188,6 +188,11 @@ public abstract class AbstractStateMachineFactory<S, E> extends LifecycleObjectS
|
||||
stateStack.push(stateData);
|
||||
}
|
||||
|
||||
// setup autostart for top-level machine
|
||||
if (machine instanceof LifecycleObjectSupport) {
|
||||
((LifecycleObjectSupport)machine).setAutoStartup(stateMachineConfigurationConfig.isAutoStart());
|
||||
}
|
||||
|
||||
// set top-level machine as relay
|
||||
final StateMachine<S, E> fmachine = machine;
|
||||
fmachine.getStateMachineAccessor().doWithAllRegions(new StateMachineFunction<StateMachineAccess<S, E>>() {
|
||||
|
||||
@@ -42,6 +42,7 @@ public class StateMachineConfigurationBuilder<S, E>
|
||||
private BeanFactory beanFactory;
|
||||
private TaskExecutor taskExecutor;
|
||||
private TaskScheduler taskScheculer;
|
||||
private boolean autoStart = false;
|
||||
private StateMachineEnsemble<S, E> ensemble;
|
||||
|
||||
/**
|
||||
@@ -83,7 +84,7 @@ public class StateMachineConfigurationBuilder<S, E>
|
||||
|
||||
@Override
|
||||
protected StateMachineConfigurationConfig<S, E> performBuild() throws Exception {
|
||||
return new StateMachineConfigurationConfig<>(beanFactory, taskExecutor, taskScheculer, ensemble);
|
||||
return new StateMachineConfigurationConfig<>(beanFactory, taskExecutor, taskScheculer, autoStart, ensemble);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,4 +123,13 @@ public class StateMachineConfigurationBuilder<S, E>
|
||||
this.ensemble = ensemble;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the auto start.
|
||||
*
|
||||
* @param autoStart the new autostart flag
|
||||
*/
|
||||
public void setAutoStart(boolean autoStart) {
|
||||
this.autoStart = autoStart;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ public class StateMachineConfigurationConfig<S, E> {
|
||||
private final BeanFactory beanFactory;
|
||||
private final TaskExecutor taskExecutor;
|
||||
private final TaskScheduler taskScheduler;
|
||||
private final boolean autoStart;
|
||||
private final StateMachineEnsemble<S, E> ensemble;
|
||||
|
||||
/**
|
||||
@@ -41,13 +42,15 @@ public class StateMachineConfigurationConfig<S, E> {
|
||||
* @param beanFactory the bean factory
|
||||
* @param taskExecutor the task executor
|
||||
* @param taskScheduler the task scheduler
|
||||
* @param autoStart the autostart flag
|
||||
* @param ensemble the state machine ensemble
|
||||
*/
|
||||
public StateMachineConfigurationConfig(BeanFactory beanFactory, TaskExecutor taskExecutor,
|
||||
TaskScheduler taskScheduler, StateMachineEnsemble<S, E> ensemble) {
|
||||
TaskScheduler taskScheduler, boolean autoStart, StateMachineEnsemble<S, E> ensemble) {
|
||||
this.beanFactory = beanFactory;
|
||||
this.taskExecutor = taskExecutor;
|
||||
this.taskScheduler = taskScheduler;
|
||||
this.autoStart = autoStart;
|
||||
this.ensemble = ensemble;
|
||||
}
|
||||
|
||||
@@ -87,4 +90,12 @@ public class StateMachineConfigurationConfig<S, E> {
|
||||
return ensemble;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns autostart flag.
|
||||
*
|
||||
* @return true, if is autostart is enabled.
|
||||
*/
|
||||
public boolean isAutoStart() {
|
||||
return autoStart;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,4 +56,13 @@ public interface ConfigurationConfigurer<S, E> extends
|
||||
*/
|
||||
ConfigurationConfigurer<S, E> taskScheduler(TaskScheduler taskScheduler);
|
||||
|
||||
/**
|
||||
* Specify if state machine should be started automatically.
|
||||
* On default state machine is not started automatically.
|
||||
*
|
||||
* @param autoStart the autostart flag
|
||||
* @return configurer for chaining
|
||||
*/
|
||||
ConfigurationConfigurer<S, E> autoStart(boolean autoStart);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,12 +38,14 @@ public class DefaultConfigurationConfigurer<S, E>
|
||||
private BeanFactory beanFactory;
|
||||
private TaskExecutor taskExecutor;
|
||||
private TaskScheduler taskScheculer;
|
||||
private boolean autoStart = false;
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineConfigurationBuilder<S, E> builder) throws Exception {
|
||||
builder.setBeanFactory(beanFactory);
|
||||
builder.setTaskExecutor(taskExecutor);
|
||||
builder.setTaskScheculer(taskScheculer);
|
||||
builder.setAutoStart(autoStart);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,4 +66,10 @@ public class DefaultConfigurationConfigurer<S, E>
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigurationConfigurer<S, E> autoStart(boolean autoStart) {
|
||||
this.autoStart = autoStart;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.statemachine.config;
|
||||
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -34,6 +35,7 @@ import org.springframework.statemachine.AbstractStateMachineTests;
|
||||
import org.springframework.statemachine.ObjectStateMachine;
|
||||
import org.springframework.statemachine.StateMachineSystemConstants;
|
||||
import org.springframework.statemachine.action.Action;
|
||||
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
|
||||
@@ -109,6 +111,16 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
||||
assertThat(machine, notNullValue());
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@Test
|
||||
public void testAutoStartFlagOn() throws Exception {
|
||||
context.register(Config9.class);
|
||||
context.refresh();
|
||||
ObjectStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
assertThat(machine.isAutoStartup(), is(true));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
public static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
@@ -430,4 +442,25 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
public static class Config9 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineConfigurationConfigurer<TestStates, TestEvents> config) throws Exception {
|
||||
config
|
||||
.withConfiguration()
|
||||
.autoStart(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(TestStates.S1)
|
||||
.states(EnumSet.allOf(TestStates.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user