Skip to content

Commit 688b61d

Browse files
committed
Notify transition before calling its actions
- Add new api to Transition to expose calling its actions. - Modify rest of a framework to first notify before calling transtion action. - Backport #322 - Relates to #307
1 parent 6e64797 commit 688b61d

File tree

7 files changed

+38
-20
lines changed

7 files changed

+38
-20
lines changed

spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -307,6 +307,12 @@ public void transit(Transition<S, E> t, StateContext<S, E> ctx, Message<E> messa
307307
long now = System.currentTimeMillis();
308308
// TODO: fix above stateContext as it's not used
309309
notifyTransitionStart(buildStateContext(Stage.TRANSITION_START, message, t, getRelayStateMachine()));
310+
try {
311+
t.executeTransitionActions(ctx);
312+
} catch (Exception e) {
313+
log.warn("Aborting as transition " + t + " caused error " + e);
314+
return;
315+
}
310316
notifyTransition(buildStateContext(Stage.TRANSITION, message, t, getRelayStateMachine()));
311317
if (t.getTarget().getPseudoState() != null && t.getTarget().getPseudoState().getKind() == PseudoStateKind.JOIN) {
312318
exitFromState(t.getSource(), message, t, getRelayStateMachine());

spring-statemachine-core/src/main/java/org/springframework/statemachine/support/DefaultStateMachineExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -236,7 +236,7 @@ private boolean handleTriggerTrans(List<Transition<S, E>> trans, Message<E> queu
236236
try {
237237
transit = t.transit(stateContext);
238238
} catch (Exception e) {
239-
log.warn("Transition " + t + " caused error " + e);
239+
log.warn("Aborting as transition " + t + " caused error " + e);
240240
}
241241
if (transit) {
242242
stateMachineExecutorTransit.transit(t, stateContext, queuedMessage);

spring-statemachine-core/src/main/java/org/springframework/statemachine/transition/AbstractTransition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,7 +112,6 @@ public boolean transit(StateContext<S, E> context) {
112112
return false;
113113
}
114114
}
115-
executeAllActions(context);
116115
return true;
117116
}
118117

@@ -155,7 +154,8 @@ public void removeActionListener(ActionListener<S, E> listener) {
155154
}
156155
}
157156

158-
protected final void executeAllActions(StateContext<S, E> context) {
157+
@Override
158+
public final void executeTransitionActions(StateContext<S, E> context) {
159159
if (actions == null) {
160160
return;
161161
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/transition/InitialTransition.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,7 +64,8 @@ public InitialTransition(State<S, E> target, Collection<Action<S, E>> actions) {
6464

6565
@Override
6666
public boolean transit(StateContext<S, E> context) {
67-
executeAllActions(context);
67+
// initial itself doesn't cause further changes what
68+
// returned true might cause.
6869
return false;
6970
}
7071
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/transition/Transition.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,13 @@ public interface Transition<S, E> {
4343
*/
4444
boolean transit(StateContext<S, E> context);
4545

46+
/**
47+
* Execute transition actions.
48+
*
49+
* @param context the state context
50+
*/
51+
void executeTransitionActions(StateContext<S, E> context);
52+
4653
/**
4754
* Gets the source state of this transition.
4855
*

spring-statemachine-core/src/test/java/org/springframework/statemachine/StateContextTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,8 +64,8 @@ public void testStartCycles() throws Exception {
6464
assertThat(listener.contexts, hasSize(19));
6565

6666
assertThat(listener.contexts, contains(
67-
hasStage(Stage.EXTENDED_STATE_CHANGED),
6867
hasStage(Stage.TRANSITION_START),
68+
hasStage(Stage.EXTENDED_STATE_CHANGED),
6969
hasStage(Stage.TRANSITION),
7070
hasStage(Stage.STATE_ENTRY),
7171
hasStage(Stage.TRANSITION_START),
@@ -85,15 +85,15 @@ public void testStartCycles() throws Exception {
8585
hasStage(Stage.TRANSITION_END)
8686
));
8787

88-
assertThat(listener.contexts.get(0).getStage(), is(Stage.EXTENDED_STATE_CHANGED));
88+
assertThat(listener.contexts.get(0).getStage(), is(Stage.TRANSITION_START));
89+
assertThat(listener.contexts.get(0).getTransition(), notNullValue());
90+
assertThat(listener.contexts.get(0).getTransition().getSource(), nullValue());
91+
assertThat(listener.contexts.get(0).getTransition().getTarget(), notNullValue());
92+
assertThat(listener.contexts.get(0).getTransition().getTarget().getId(), is(States.S0));
93+
assertThat(listener.contexts.get(0).getSource(), nullValue());
94+
assertThat(listener.contexts.get(0).getTarget(), notNullValue());
8995

90-
assertThat(listener.contexts.get(1).getStage(), is(Stage.TRANSITION_START));
91-
assertThat(listener.contexts.get(1).getTransition(), notNullValue());
92-
assertThat(listener.contexts.get(1).getTransition().getSource(), nullValue());
93-
assertThat(listener.contexts.get(1).getTransition().getTarget(), notNullValue());
94-
assertThat(listener.contexts.get(1).getTransition().getTarget().getId(), is(States.S0));
95-
assertThat(listener.contexts.get(1).getSource(), nullValue());
96-
assertThat(listener.contexts.get(1).getTarget(), notNullValue());
96+
assertThat(listener.contexts.get(1).getStage(), is(Stage.EXTENDED_STATE_CHANGED));
9797

9898
assertThat(listener.contexts.get(2).getStage(), is(Stage.TRANSITION));
9999
assertThat(listener.contexts.get(2).getTransition(), notNullValue());

spring-statemachine-core/src/test/java/org/springframework/statemachine/support/StateContextExpressionMethodsTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,6 +104,10 @@ public boolean transit(StateContext<SpelStates, SpelEvents> context) {
104104
return false;
105105
}
106106

107+
@Override
108+
public void executeTransitionActions(StateContext<SpelStates, SpelEvents> context) {
109+
}
110+
107111
@Override
108112
public State<SpelStates, SpelEvents> getSource() {
109113
return new EnumState<SpelStates, SpelEvents>(SpelStates.S1);

0 commit comments

Comments
 (0)