Skip to content

Commit 048e930

Browse files
xJoeWoojvalkeal
authored andcommitted
Replace resetStateMachine with resetStateMachineReactively
- improve AbstractPersistStateMachineHandler#handleEventWithStateReactively - Fixex #949 Signed-off-by: xJoeWoo <[email protected]>
1 parent 0c88fb9 commit 048e930

File tree

8 files changed

+52
-54
lines changed

8 files changed

+52
-54
lines changed

spring-statemachine-core/src/main/java/org/springframework/statemachine/access/StateMachineAccess.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public interface StateMachineAccess<S, E> extends ReactiveStateMachineAccess<S,
4242
* Reset state machine.
4343
*
4444
* @param stateMachineContext the state machine context
45+
* @see #resetStateMachineReactively(StateMachineContext)
4546
*/
47+
@Deprecated
4648
void resetStateMachine(StateMachineContext<S, E> stateMachineContext);
4749

4850
/**

spring-statemachine-core/src/main/java/org/springframework/statemachine/ensemble/DistributedStateMachine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void stateMachineJoined(final StateMachine<S, E> stateMachine, final Stat
296296
log.debug("Joining with context " + context);
297297
}
298298

299-
delegate.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(context));
299+
delegate.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(context).block());
300300
}
301301
log.info("Requesting to start delegating state machine " + delegate);
302302
log.info("Delegating machine id " + delegate.getUuid());

spring-statemachine-core/src/main/java/org/springframework/statemachine/persist/AbstractStateMachinePersister.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final void persist(StateMachine<S, E> stateMachine, T contextObj) throws
6767
public final StateMachine<S, E> restore(StateMachine<S, E> stateMachine, T contextObj) throws Exception {
6868
final StateMachineContext<S, E> context = stateMachinePersist.read(contextObj);
6969
stateMachine.stopReactively().block();
70-
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(context));
70+
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(context).block());
7171
stateMachine.startReactively().block();
7272
return stateMachine;
7373
}

spring-statemachine-core/src/main/java/org/springframework/statemachine/service/DefaultStateMachineService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected StateMachine<S, E> restoreStateMachine(StateMachine<S, E> stateMachine
166166
}
167167
stateMachine.stopReactively().block();
168168
// only go via top region
169-
stateMachine.getStateMachineAccessor().doWithRegion(function -> function.resetStateMachine(stateMachineContext));
169+
stateMachine.getStateMachineAccessor().doWithRegion(function -> function.resetStateMachineReactively(stateMachineContext).block());
170170
return stateMachine;
171171
}
172172

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -763,20 +763,16 @@ public Mono<Void> resetStateMachineReactively(StateMachineContext<S, E> stateMac
763763
.then();
764764
mono = mono.then(resetMono);
765765
} else if (s.isOrthogonal() && stateMachineContext.getChilds() != null) {
766-
Collection<Region<S, E>> regions = ((AbstractState<S, E>)s).getRegions();
767-
Mono<Void> resetMono = Flux.fromIterable(regions)
768-
.flatMap(region -> {
769-
return Flux.fromIterable(stateMachineContext.getChilds())
770-
.flatMap(child -> {
771-
return Mono.fromRunnable(() -> {
772-
((StateMachine<S, E>)region).getStateMachineAccessor()
773-
.doWithRegion(function -> function.resetStateMachine(child));
774-
});
775-
})
776-
.then();
777-
})
766+
Collection<Region<S, E>> regions = ((AbstractState<S, E>) s).getRegions();
767+
Mono<Void> resetMono = Flux.fromIterable(regions).flatMap(region ->
768+
Flux.fromIterable(stateMachineContext.getChilds())
769+
.flatMap(child ->
770+
((StateMachine<S, E>) region).getStateMachineAccessor().withRegion().resetStateMachineReactively(child)
771+
)
772+
.then()
773+
)
778774
.then();
779-
mono = mono.then(resetMono);
775+
mono = mono.thenEmpty(resetMono);
780776
}
781777

782778
if (log.isDebugEnabled()) {
@@ -787,20 +783,19 @@ public Mono<Void> resetStateMachineReactively(StateMachineContext<S, E> stateMac
787783
break;
788784
} else if (stateMachineContext.getChilds() != null && !stateMachineContext.getChilds().isEmpty()) {
789785
if (s.isOrthogonal()) {
790-
Collection<Region<S, E>> regions = ((AbstractState<S, E>)s).getRegions();
791-
Mono<Void> resetMono = Flux.fromIterable(regions)
792-
.flatMap(region -> {
793-
return Flux.fromIterable(stateMachineContext.getChilds())
786+
Collection<Region<S, E>> regions = ((AbstractState<S, E>) s).getRegions();
787+
Mono<Void> resetMono = Flux.fromIterable(regions).flatMap(region ->
788+
Flux.fromIterable(stateMachineContext.getChilds())
794789
.flatMap(child -> {
795-
return Mono.fromRunnable(() -> {
796-
if (ObjectUtils.nullSafeEquals(region.getId(), child.getId())) {
797-
((StateMachine<S, E>)region).getStateMachineAccessor()
798-
.doWithRegion(function -> function.resetStateMachine(child));
799-
}
800-
});
790+
if (ObjectUtils.nullSafeEquals(region.getId(), child.getId())) {
791+
return ((StateMachine<S, E>) region).getStateMachineAccessor()
792+
.withRegion().resetStateMachineReactively(child);
793+
} else {
794+
return Mono.empty();
795+
}
801796
})
802-
.then();
803-
})
797+
.then()
798+
)
804799
.then();
805800
monos.add(resetMono);
806801
} else {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testResetSubStates1() throws Exception {
7777
ExtendedState extendedState = new DefaultExtendedState(variables);
7878
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S12, Events.I, null, extendedState);
7979

80-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
80+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
8181

8282
doStartAndAssert(machine);
8383
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S1, States.S12);
@@ -95,7 +95,7 @@ public void testResetSubStates2() throws Exception {
9595
ExtendedState extendedState = new DefaultExtendedState(variables);
9696
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S211, Events.C, null, extendedState);
9797

98-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
98+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
9999

100100
doStartAndAssert(machine);
101101
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S2, States.S21, States.S211);
@@ -113,7 +113,7 @@ public void testResetSubStates3() throws Exception {
113113
ExtendedState extendedState = new DefaultExtendedState(variables);
114114
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S2, Events.C, null, extendedState);
115115

116-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
116+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
117117

118118
doStartAndAssert(machine);
119119
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S2, States.S21, States.S211);
@@ -138,7 +138,7 @@ public void testResetRegions1() {
138138
DefaultStateMachineContext<TestStates, TestEvents> stateMachineContext =
139139
new DefaultStateMachineContext<TestStates, TestEvents>(childs, TestStates.S2, TestEvents.E1, null, null);
140140

141-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
141+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
142142

143143
doStartAndAssert(machine);
144144
assertThat(machine.getState().getIds()).containsOnly(TestStates.S2, TestStates.S21, TestStates.S31);
@@ -162,7 +162,7 @@ public void testResetRegions2() {
162162
DefaultStateMachineContext<TestStates, TestEvents> stateMachineContext =
163163
new DefaultStateMachineContext<TestStates, TestEvents>(childs, TestStates.S2, null, null, null);
164164

165-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
165+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
166166

167167
doStartAndAssert(machine);
168168
assertThat(machine.getState().getIds()).containsOnly(TestStates.S2, TestStates.S21, TestStates.S31);
@@ -184,7 +184,7 @@ public void testResetUpdateExtendedStateVariables() {
184184
ExtendedState extendedState = new DefaultExtendedState(variables);
185185
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S0, null, null, extendedState);
186186

187-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
187+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
188188

189189
doStartAndAssert(machine);
190190
assertThat((Integer)machine.getExtendedState().getVariables().get("count")).isEqualTo(1);
@@ -207,7 +207,7 @@ public void testResetWithNullContext() throws Exception {
207207
assertThat((Integer)machine.getExtendedState().getVariables().get("foo")).isZero();
208208

209209
doStopAndAssert(machine);
210-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(null));
210+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(null).block());
211211
doStartAndAssert(machine);
212212
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S1, States.S11);
213213
assertThat(machine.getExtendedState().getVariables()).isEmpty();
@@ -229,7 +229,7 @@ public void testResetWithEnumToCorrectStartState() throws Exception {
229229
DefaultStateMachineContext<States, Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(
230230
States.S11, null, null, null);
231231
machine.getStateMachineAccessor()
232-
.doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
232+
.doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
233233

234234
doStartAndAssert(machine);
235235
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S1, States.S11);
@@ -246,7 +246,7 @@ public void testRestoreWithTimer() throws Exception {
246246

247247
DefaultStateMachineContext<States, Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S1, null,
248248
null, null);
249-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
249+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
250250

251251
doStartAndAssert(machine);
252252
Thread.sleep(1100);
@@ -290,7 +290,7 @@ public void testResetKeepsExtendedStateIntactInSubmachine() {
290290
ExtendedState extendedState = new DefaultExtendedState(variables);
291291
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S0, null, null, extendedState);
292292

293-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
293+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
294294
doStartAndAssert(machine);
295295

296296
assertThat((Integer)machine.getExtendedState().getVariables().get("count1")).isEqualTo(1);
@@ -321,7 +321,7 @@ public void testResetFunkyEnumTypes1() throws Exception {
321321
DefaultStateMachineContext<MyState, MyEvent> stateMachineContext = new DefaultStateMachineContext<MyState, MyEvent>(
322322
SubState.SUB_NEXT, null, null, null);
323323

324-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
324+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
325325

326326
doStartAndAssert(machine);
327327
assertThat(machine.getState().getIds()).containsOnly(SuperState.PARENT, SubState.SUB_NEXT);
@@ -337,7 +337,7 @@ public void testResetFunkyEnumTypes2() throws Exception {
337337
DefaultStateMachineContext<MyState, MyEvent> stateMachineContext = new DefaultStateMachineContext<MyState, MyEvent>(
338338
SuperState.INITIAL, null, null, null);
339339

340-
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
340+
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
341341

342342
doStartAndAssert(machine);
343343
assertThat(machine.getState().getIds()).containsOnly(SuperState.INITIAL);

spring-statemachine-recipes/src/main/java/org/springframework/statemachine/recipes/persist/AbstractPersistStateMachineHandler.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.statemachine.support.LifecycleObjectSupport;
2525
import org.springframework.statemachine.support.StateMachineInterceptorAdapter;
2626
import org.springframework.statemachine.transition.Transition;
27+
import reactor.core.publisher.Flux;
2728
import reactor.core.publisher.Mono;
2829

2930
import java.util.Iterator;
@@ -63,7 +64,7 @@ public boolean handleEventWithState(Message<E> event, S state) {
6364
stateMachine.stopReactively().block();
6465
List<StateMachineAccess<S, E>> withAllRegions = stateMachine.getStateMachineAccessor().withAllRegions();
6566
for (StateMachineAccess<S, E> a : withAllRegions) {
66-
a.resetStateMachine(new DefaultStateMachineContext<S, E>(state, null, null, null));
67+
a.resetStateMachineReactively(new DefaultStateMachineContext<S, E>(state, null, null, null)).block();
6768
}
6869
stateMachine.startReactively().block();
6970
return stateMachine.sendEvent(event);
@@ -77,18 +78,18 @@ public boolean handleEventWithState(Message<E> event, S state) {
7778
* @return mono for completion
7879
*/
7980
public Mono<Void> handleEventWithStateReactively(Message<E> event, S state) {
80-
StateMachine<S, E> stateMachine = getInitStateMachine();
81-
// TODO: REACTOR add docs and revisit this function concept
82-
return Mono.from(stateMachine.stopReactively())
83-
.then(Mono.fromRunnable(() -> {
84-
List<StateMachineAccess<S, E>> withAllRegions = stateMachine.getStateMachineAccessor().withAllRegions();
85-
for (StateMachineAccess<S, E> a : withAllRegions) {
86-
a.resetStateMachine(new DefaultStateMachineContext<S, E>(state, null, null, null));
87-
}
88-
}))
89-
.then(stateMachine.startReactively())
90-
.thenMany(stateMachine.sendEvent(Mono.just(event)))
91-
.then();
81+
return Mono.defer(() -> {
82+
StateMachine<S, E> stateMachine = getInitStateMachine();
83+
// TODO: REACTOR add docs and revisit this function concept
84+
return Mono.from(stateMachine.stopReactively())
85+
.thenEmpty(
86+
Flux.fromIterable(stateMachine.getStateMachineAccessor().withAllRegions())
87+
.flatMap(region -> region.resetStateMachineReactively(new DefaultStateMachineContext<S, E>(state, null, null, null)))
88+
)
89+
.then(stateMachine.startReactively())
90+
.thenMany(stateMachine.sendEvent(Mono.just(event)))
91+
.then();
92+
});
9293
}
9394

9495
/**

spring-statemachine-recipes/src/main/java/org/springframework/statemachine/recipes/tasks/TasksHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void resetFromPersistStore() {
166166
}
167167

168168
stateMachine.stopReactively().block();
169-
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(context));
169+
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(context).block());
170170
stateMachine.startReactively().block();
171171
}
172172

0 commit comments

Comments
 (0)