11package com .baeldung .spring .statemachine .config ;
22
3- import java .util .Arrays ;
4- import java .util .HashSet ;
5- import java .util .logging .Logger ;
6-
73import org .springframework .context .annotation .Bean ;
84import org .springframework .context .annotation .Configuration ;
95import org .springframework .statemachine .action .Action ;
1410import org .springframework .statemachine .config .builders .StateMachineTransitionConfigurer ;
1511import org .springframework .statemachine .guard .Guard ;
1612
13+ import java .util .Arrays ;
14+ import java .util .HashSet ;
15+ import java .util .logging .Logger ;
16+
1717@ Configuration
1818@ EnableStateMachine
1919public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapter <String , String > {
2020
2121 public static final Logger LOGGER = Logger .getLogger (SimpleStateMachineConfiguration .class .getName ());
2222
2323 @ Override
24- public void configure (StateMachineConfigurationConfigurer <String , String > config )
25- throws Exception {
24+ public void configure (StateMachineConfigurationConfigurer <String , String > config ) throws Exception {
2625 config
27- .withConfiguration ()
28- .autoStartup (true )
29- .listener (new StateMachineListener ());
26+ .withConfiguration ()
27+ .autoStartup (true )
28+ .listener (new StateMachineListener ());
3029 }
3130
3231 @ Override
3332 public void configure (StateMachineStateConfigurer <String , String > states ) throws Exception {
3433 states
35- .withStates ()
36- .initial ("SI" )
37- .end ("SF" )
38- .states (new HashSet <>(Arrays .asList ("S1" , "S2" )))
39- .stateEntry ("S3" , entryAction ())
40- .stateExit ("S3" , exitAction ())
41- .state ("S4" , executeAction (), errorAction ())
42- .stateDo ("S5" , executeAction ());
34+ .withStates ()
35+ .initial ("SI" )
36+ .end ("SF" )
37+ .states (new HashSet <>(Arrays .asList ("S1" , "S2" )))
38+ .stateEntry ("S3" , entryAction ())
39+ .stateExit ("S3" , exitAction ())
40+ .state ("S4" , executeAction (), errorAction ())
41+ .stateDo ("S5" , executeAction ());
4342
4443 }
4544
4645 @ Override
4746 public void configure (StateMachineTransitionConfigurer <String , String > transitions ) throws Exception {
48- transitions .withExternal ()
49- .source ("SI" ).target ("S1" ).event ("E1" ).action (initAction ())
50- .and ().withExternal ()
51- .source ("S1" ).target ("S2" ).event ("E2" )
52- .and ().withExternal ()
53- .source ("SI" ).target ("S3" ).event ("E3" )
54- .and ().withExternal ()
55- .source ("S3" ).target ("S4" ).event ("E4" )
56- .and ().withExternal ()
57- .source ("S4" ).target ("S5" ).event ("E5" )
58- .and ().withExternal ()
59- .source ("S5" ).target ("SF" ).event ("end" ).guard (simpleGuard ());
47+ transitions
48+ .withExternal ()
49+ .source ("SI" )
50+ .target ("S1" )
51+ .event ("E1" )
52+ .action (initAction ())
53+ .and ()
54+ .withExternal ()
55+ .source ("S1" )
56+ .target ("S2" )
57+ .event ("E2" )
58+ .and ()
59+ .withExternal ()
60+ .source ("SI" )
61+ .target ("S3" )
62+ .event ("E3" )
63+ .and ()
64+ .withExternal ()
65+ .source ("S3" )
66+ .target ("S4" )
67+ .event ("E4" )
68+ .and ()
69+ .withExternal ()
70+ .source ("S4" )
71+ .target ("S5" )
72+ .event ("E5" )
73+ .and ()
74+ .withExternal ()
75+ .source ("S5" )
76+ .target ("SF" )
77+ .event ("end" )
78+ .guard (simpleGuard ());
6079 }
6180
6281 @ Bean
6382 public Guard <String , String > simpleGuard () {
6483 return (ctx ) -> {
65- int approvalCount = (int ) ctx .getExtendedState ().getVariables ().getOrDefault ("approvalCount" , 0 );
84+ int approvalCount = (int ) ctx
85+ .getExtendedState ()
86+ .getVariables ()
87+ .getOrDefault ("approvalCount" , 0 );
6688 return approvalCount > 0 ;
6789 };
6890 }
6991
7092 @ Bean
7193 public Action <String , String > entryAction () {
72- return ( ctx ) -> {
73- LOGGER . info ( "Entry " + ctx . getTarget (). getId ());
74- } ;
94+ return ctx -> LOGGER . info ( "Entry " + ctx
95+ . getTarget ()
96+ . getId ()) ;
7597 }
7698
7799 @ Bean
78100 public Action <String , String > doAction () {
79- return ( ctx ) -> {
80- LOGGER . info ( "Do " + ctx . getTarget (). getId ());
81- } ;
101+ return ctx -> LOGGER . info ( "Do " + ctx
102+ . getTarget ()
103+ . getId ()) ;
82104 }
83105
84106 @ Bean
85107 public Action <String , String > executeAction () {
86- return (ctx ) -> {
87- LOGGER .info ("Execute " + ctx .getTarget ().getId ());
88- int approvals = (int ) ctx .getExtendedState ().getVariables ().getOrDefault ("approvalCount" , 0 );
108+ return ctx -> {
109+ LOGGER .info ("Execute " + ctx
110+ .getTarget ()
111+ .getId ());
112+ int approvals = (int ) ctx
113+ .getExtendedState ()
114+ .getVariables ()
115+ .getOrDefault ("approvalCount" , 0 );
89116 approvals ++;
90- ctx .getExtendedState ().getVariables ().put ("approvalCount" , approvals );
117+ ctx
118+ .getExtendedState ()
119+ .getVariables ()
120+ .put ("approvalCount" , approvals );
91121 };
92122 }
93123
94124 @ Bean
95125 public Action <String , String > exitAction () {
96- return (ctx ) -> {
97- LOGGER .info ("Exit " + ctx .getSource ().getId () + " -> " + ctx .getTarget ().getId ());
98- };
126+ return ctx -> LOGGER .info ("Exit " + ctx
127+ .getSource ()
128+ .getId () + " -> " + ctx
129+ .getTarget ()
130+ .getId ());
99131 }
100132
101133 @ Bean
102134 public Action <String , String > errorAction () {
103- return ( ctx ) -> {
104- LOGGER . info ( "Error " + ctx . getSource (). getId () + ctx . getException ());
105- } ;
135+ return ctx -> LOGGER . info ( "Error " + ctx
136+ . getSource ()
137+ . getId () + ctx . getException ()) ;
106138 }
107139
108140 @ Bean
109141 public Action <String , String > initAction () {
110- return ( ctx ) -> {
111- LOGGER . info ( ctx . getTarget (). getId ());
112- } ;
142+ return ctx -> LOGGER . info ( ctx
143+ . getTarget ()
144+ . getId ()) ;
113145 }
114146}
0 commit comments