diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/service/DefaultStateMachineService.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/service/DefaultStateMachineService.java index 3bc328f6..77f5305e 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/service/DefaultStateMachineService.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/service/DefaultStateMachineService.java @@ -15,12 +15,14 @@ */ package org.springframework.statemachine.service; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.DisposableBean; import org.springframework.context.Lifecycle; import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachineContext; @@ -40,7 +42,7 @@ import org.springframework.util.Assert; * @param the type of state * @param the type of event */ -public class DefaultStateMachineService implements StateMachineService { +public class DefaultStateMachineService implements StateMachineService, DisposableBean { private final static Log log = LogFactory.getLog(DefaultStateMachineService.class); private final StateMachineFactory stateMachineFactory; @@ -69,6 +71,11 @@ public class DefaultStateMachineService implements StateMachineService acquireStateMachine(String machineId) { return acquireStateMachine(machineId, true); @@ -130,6 +137,16 @@ public class DefaultStateMachineService implements StateMachineService machineIds = new ArrayList<>(machines.keySet()); + for (String machineId : machineIds) { + releaseStateMachine(machineId, true); + } + } + } + protected StateMachine restoreStateMachine(StateMachine stateMachine, final StateMachineContext stateMachineContext) { if (stateMachineContext == null) { return stateMachine; diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/service/DefaultStateMachineServiceTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/service/DefaultStateMachineServiceTests.java index a9a95472..5989c95b 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/service/DefaultStateMachineServiceTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/service/DefaultStateMachineServiceTests.java @@ -18,6 +18,8 @@ package org.springframework.statemachine.service; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import java.util.Map; + import org.junit.Test; import org.springframework.context.Lifecycle; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -28,6 +30,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.statemachine.AbstractStateMachineTests; import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachineSystemConstants; +import org.springframework.statemachine.TestUtils; import org.springframework.statemachine.config.EnableStateMachineFactory; import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; import org.springframework.statemachine.config.StateMachineFactory; @@ -153,6 +156,24 @@ public class DefaultStateMachineServiceTests extends AbstractStateMachineTests { assertThat(((Lifecycle)machine1).isRunning(), is(true)); } + @Test + public void testServiceStop() throws Exception { + context.register(Config1.class); + context.refresh(); + StateMachineFactory stateMachineFactory = + context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINEFACTORY, StateMachineFactory.class); + + DefaultStateMachineService service = new DefaultStateMachineService<>(stateMachineFactory); + StateMachine machine1 = service.acquireStateMachine("m1", false); + StateMachine machine2 = service.acquireStateMachine("m2", false); + assertThat(((Lifecycle)machine1).isRunning(), is(false)); + assertThat(((Lifecycle)machine2).isRunning(), is(false)); + Map machines = TestUtils.readField("machines", service); + assertThat(machines.size(), is(2)); + service.destroy(); + assertThat(machines.size(), is(0)); + } + @Configuration @EnableStateMachineFactory static class Config1 extends EnumStateMachineConfigurerAdapter {