Add stop logic to DefaultStateMachineService
- Now implementing DisposableBean and clearing/stopping machines if service is destroyed. - Relates to #432
This commit is contained in:
@@ -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 <S> the type of state
|
||||
* @param <E> the type of event
|
||||
*/
|
||||
public class DefaultStateMachineService<S, E> implements StateMachineService<S, E> {
|
||||
public class DefaultStateMachineService<S, E> implements StateMachineService<S, E>, DisposableBean {
|
||||
|
||||
private final static Log log = LogFactory.getLog(DefaultStateMachineService.class);
|
||||
private final StateMachineFactory<S, E> stateMachineFactory;
|
||||
@@ -69,6 +71,11 @@ public class DefaultStateMachineService<S, E> implements StateMachineService<S,
|
||||
this.stateMachinePersist = stateMachinePersist;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void destroy() throws Exception {
|
||||
doStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public StateMachine<S, E> acquireStateMachine(String machineId) {
|
||||
return acquireStateMachine(machineId, true);
|
||||
@@ -130,6 +137,16 @@ public class DefaultStateMachineService<S, E> implements StateMachineService<S,
|
||||
this.stateMachinePersist = stateMachinePersist;
|
||||
}
|
||||
|
||||
protected void doStop() {
|
||||
log.info("Entering stop sequence, stopping all managed machines");
|
||||
synchronized (machines) {
|
||||
ArrayList<String> machineIds = new ArrayList<>(machines.keySet());
|
||||
for (String machineId : machineIds) {
|
||||
releaseStateMachine(machineId, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected StateMachine<S, E> restoreStateMachine(StateMachine<S, E> stateMachine, final StateMachineContext<S, E> stateMachineContext) {
|
||||
if (stateMachineContext == null) {
|
||||
return stateMachine;
|
||||
|
||||
@@ -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<TestStates, TestEvents> stateMachineFactory =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINEFACTORY, StateMachineFactory.class);
|
||||
|
||||
DefaultStateMachineService<TestStates, TestEvents> service = new DefaultStateMachineService<>(stateMachineFactory);
|
||||
StateMachine<TestStates,TestEvents> machine1 = service.acquireStateMachine("m1", false);
|
||||
StateMachine<TestStates,TestEvents> 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<TestStates, TestEvents> {
|
||||
|
||||
Reference in New Issue
Block a user