Backport: Add default taskExecutor/taskScheduler via builder

- Backporting #161
- As with javaconfig, use default intances if not set
  because it is anyway required. User can then customise
  or override.
- Fixes #163
This commit is contained in:
Janne Valkealahti
2016-01-16 19:28:57 +00:00
parent 2e7be9da83
commit 3d37f14a34
2 changed files with 31 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.springframework.statemachine.config;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineException;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
@@ -121,9 +123,13 @@ public class StateMachineBuilder {
}
if (stateMachineConfigurationConfig.getTaskExecutor() != null) {
stateMachineFactory.setTaskExecutor(stateMachineConfigurationConfig.getTaskExecutor());
} else {
stateMachineFactory.setTaskExecutor(new SyncTaskExecutor());
}
if (stateMachineConfigurationConfig.getTaskScheduler() != null) {
stateMachineFactory.setTaskScheduler(stateMachineConfigurationConfig.getTaskScheduler());
} else {
stateMachineFactory.setTaskScheduler(new ConcurrentTaskScheduler());
}
return stateMachineFactory.getStateMachine();
} catch (Exception e) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.TestUtils;
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfig;
@@ -187,6 +188,28 @@ public class ManualBuilderTests {
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S2"));
}
@Test
public void testManualBuildDefaultTaskExecutor() throws Exception {
Builder<String, String> builder = StateMachineBuilder.builder();
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(stateMachine, notNullValue());
assertThat(TestUtils.readField("taskExecutor", stateMachine), notNullValue());
assertThat(TestUtils.readField("taskScheduler", stateMachine), notNullValue());
}
@Test
public void testAutoStartFlagOn() throws Exception {
Builder<String, String> builder = StateMachineBuilder.builder();