Skip to content

Clear machine id with null context restore #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2017 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.
Expand Down Expand Up @@ -198,17 +198,13 @@ public boolean shouldDefer(Message<E> event) {
public void exit(StateContext<S, E> context) {
cancelStateActions();
stateListener.onExit(context);
for (Trigger<S, E> trigger : triggers) {
trigger.disarm();
}
disarmTriggers();
}

@Override
public void entry(StateContext<S, E> context) {
stateListener.onEntry(context);
for (Trigger<S, E> trigger : triggers) {
trigger.arm();
}
armTriggers();
scheduleStateActions(context);
}

Expand Down Expand Up @@ -292,6 +288,16 @@ public void removeActionListener(ActionListener<S, E> listener) {
}
}

@Override
protected void doStart() {
armTriggers();
}

@Override
protected void doStop() {
disarmTriggers();
}

/**
* Gets the submachine.
*
Expand Down Expand Up @@ -332,6 +338,24 @@ public List<Trigger<S, E>> getTriggers() {
return triggers;
}

/**
* Arm triggers.
*/
protected void armTriggers() {
for (Trigger<S, E> trigger : triggers) {
trigger.arm();
}
}

/**
* Disarm triggers.
*/
protected void disarmTriggers() {
for (Trigger<S, E> trigger : triggers) {
trigger.disarm();
}
}

/**
* Cancel existing state actions and clear list.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.Lifecycle;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
Expand Down Expand Up @@ -719,6 +720,9 @@ public void apply(StateMachineAccess<S, E> function) {
if (stateSet && stateMachineContext.getExtendedState() != null) {
this.extendedState = stateMachineContext.getExtendedState();
}
if (currentState instanceof Lifecycle) {
((Lifecycle)currentState).start();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.springframework.statemachine.access.StateMachineFunction;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
Expand Down Expand Up @@ -251,6 +253,31 @@ public void apply(StateMachineAccess<States, Events> function) {
assertThat(machine.getExtendedState().getVariables().size(), is(0));
}

@Test
public void testRestoreWithTimer() throws Exception {
context.register(Config4.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachineFactory<States, Events> factory = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINEFACTORY,
StateMachineFactory.class);
StateMachine<States, Events> machine = factory.getStateMachine();

DefaultStateMachineContext<States, Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S1, null,
null, null);
machine.getStateMachineAccessor().doWithAllRegions(new StateMachineFunction<StateMachineAccess<States,Events>>() {

@Override
public void apply(StateMachineAccess<States, Events> function) {
function.resetStateMachine(stateMachineContext);
}
});

machine.start();
Thread.sleep(1100);
assertThat(machine.getState().getIds(), containsInAnyOrder(States.S2));

}

@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<States, Events> {
Expand Down Expand Up @@ -420,6 +447,36 @@ public void execute(StateContext<States, Events> context) {

}

@Configuration
@EnableStateMachineFactory
static class Config4 extends EnumStateMachineConfigurerAdapter<States, Events> {

@Override
public void configure(StateMachineStateConfigurer<States, Events> states)
throws Exception {
states
.withStates()
.initial(States.S0)
.state(States.S1)
.state(States.S2);
}

@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
throws Exception {
transitions
.withExternal()
.source(States.S0)
.target(States.S1)
.event(Events.A)
.and()
.withExternal()
.source(States.S1)
.target(States.S2)
.timerOnce(1000);
}
}

public static enum States {
S0, S1, S11, S12, S2, S21, S211, S212
}
Expand Down