Skip to content

Commit 319dd26

Browse files
dkornishevzhendrikse
authored andcommitted
Spring State Machine (eugenp#1424)
* Neo4j cleanup * Neo4j cleanup * Neo4j cleanup x2 * State Machine Init * cleanup * White background, Java Util Logging * Change to Logging * Static import of asserts. rename test methods
1 parent f673acb commit 319dd26

20 files changed

Lines changed: 813 additions & 1 deletion

‎pom.xml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
<module>spring-sleuth</module>
189189
<module>spring-social-login</module>
190190
<module>spring-spel</module>
191+
<module>spring-state-machine</module>
191192
<module>spring-thymeleaf</module>
192193
<module>spring-userservice</module>
193194
<module>spring-zuul</module>
@@ -210,7 +211,9 @@
210211

211212
<module>rabbitmq</module>
212213
<module>vertx</module>
213-
</modules>
214+
215+
</modules>
216+
214217
<build>
215218
<plugins>
216219
<plugin>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1489770331405" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
3+
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
4+
<startEvent id="_2" name="Start">
5+
<messageEventDefinition id="_2_ED_1"/>
6+
</startEvent>
7+
<parallelGateway gatewayDirection="Unspecified" id="_3" name="Fork"/>
8+
<userTask activiti:exclusive="true" id="_4" name="UserTask"/>
9+
<scriptTask activiti:exclusive="true" id="_5" name="ScriptTask"/>
10+
<endEvent id="_7" name="EndEvent">
11+
<terminateEventDefinition id="_7_ED_1"/>
12+
<extensionElements>
13+
<activiti:executionListener event="start">
14+
<activiti:field>
15+
<activiti:string/>
16+
</activiti:field>
17+
</activiti:executionListener>
18+
<activiti:executionListener event="start"/>
19+
<activiti:executionListener event="start"/>
20+
<activiti:executionListener event="start"/>
21+
<activiti:executionListener event="start"/>
22+
</extensionElements>
23+
</endEvent>
24+
<inclusiveGateway gatewayDirection="Unspecified" id="_6" name="Join"/>
25+
<sequenceFlow id="_8" sourceRef="_3" targetRef="_4"/>
26+
<sequenceFlow id="_9" sourceRef="_3" targetRef="_5"/>
27+
<sequenceFlow id="_10" sourceRef="_5" targetRef="_6"/>
28+
<sequenceFlow id="_11" sourceRef="_4" targetRef="_6"/>
29+
<sequenceFlow id="_12" sourceRef="_6" targetRef="_7"/>
30+
<sequenceFlow id="_13" sourceRef="_2" targetRef="_3"/>
31+
</process>
32+
<bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
33+
<bpmndi:BPMNPlane bpmnElement="myProcess_1">
34+
<bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
35+
<omgdc:Bounds height="32.0" width="32.0" x="35.0" y="95.0"/>
36+
<bpmndi:BPMNLabel>
37+
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
38+
</bpmndi:BPMNLabel>
39+
</bpmndi:BPMNShape>
40+
<bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3">
41+
<omgdc:Bounds height="32.0" width="32.0" x="140.0" y="95.0"/>
42+
<bpmndi:BPMNLabel>
43+
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
44+
</bpmndi:BPMNLabel>
45+
</bpmndi:BPMNShape>
46+
<bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4">
47+
<omgdc:Bounds height="55.0" width="85.0" x="230.0" y="20.0"/>
48+
<bpmndi:BPMNLabel>
49+
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
50+
</bpmndi:BPMNLabel>
51+
</bpmndi:BPMNShape>
52+
<bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5">
53+
<omgdc:Bounds height="55.0" width="85.0" x="230.0" y="180.0"/>
54+
<bpmndi:BPMNLabel>
55+
<omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
56+
</bpmndi:BPMNLabel>
57+
</bpmndi:BPMNShape>
58+
<bpmndi:BPMNShape bpmnElement="_7" id="Shape-_7">
59+
<omgdc:Bounds height="32.0" width="32.0" x="475.0" y="130.0"/>
60+
<bpmndi:BPMNLabel>
61+
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
62+
</bpmndi:BPMNLabel>
63+
</bpmndi:BPMNShape>
64+
<bpmndi:BPMNShape bpmnElement="_6" id="Shape-_6">
65+
<omgdc:Bounds height="32.0" width="32.0" x="375.0" y="130.0"/>
66+
<bpmndi:BPMNLabel>
67+
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
68+
</bpmndi:BPMNLabel>
69+
</bpmndi:BPMNShape>
70+
<bpmndi:BPMNEdge bpmnElement="_13" id="BPMNEdge__13" sourceElement="_2" targetElement="_3">
71+
<omgdi:waypoint x="67.0" y="111.0"/>
72+
<omgdi:waypoint x="140.0" y="111.0"/>
73+
<bpmndi:BPMNLabel>
74+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
75+
</bpmndi:BPMNLabel>
76+
</bpmndi:BPMNEdge>
77+
<bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12" sourceElement="_6" targetElement="_7">
78+
<omgdi:waypoint x="407.0" y="146.0"/>
79+
<omgdi:waypoint x="475.0" y="146.0"/>
80+
<bpmndi:BPMNLabel>
81+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
82+
</bpmndi:BPMNLabel>
83+
</bpmndi:BPMNEdge>
84+
<bpmndi:BPMNEdge bpmnElement="_8" id="BPMNEdge__8" sourceElement="_3" targetElement="_4">
85+
<omgdi:waypoint x="172.0" y="111.0"/>
86+
<omgdi:waypoint x="230.0" y="47.5"/>
87+
<bpmndi:BPMNLabel>
88+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
89+
</bpmndi:BPMNLabel>
90+
</bpmndi:BPMNEdge>
91+
<bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9" sourceElement="_3" targetElement="_5">
92+
<omgdi:waypoint x="172.0" y="111.0"/>
93+
<omgdi:waypoint x="230.0" y="207.5"/>
94+
<bpmndi:BPMNLabel>
95+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
96+
</bpmndi:BPMNLabel>
97+
</bpmndi:BPMNEdge>
98+
<bpmndi:BPMNEdge bpmnElement="_11" id="BPMNEdge__11" sourceElement="_4" targetElement="_6">
99+
<omgdi:waypoint x="315.0" y="45.0"/>
100+
<omgdi:waypoint x="390.0" y="45.0"/>
101+
<omgdi:waypoint x="390.0" y="131.0"/>
102+
<bpmndi:BPMNLabel>
103+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
104+
</bpmndi:BPMNLabel>
105+
</bpmndi:BPMNEdge>
106+
<bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10" sourceElement="_5" targetElement="_6">
107+
<omgdi:waypoint x="315.0" y="190.0"/>
108+
<omgdi:waypoint x="355.0" y="190.0"/>
109+
<omgdi:waypoint x="375.0" y="146.0"/>
110+
<bpmndi:BPMNLabel>
111+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
112+
</bpmndi:BPMNLabel>
113+
</bpmndi:BPMNEdge>
114+
</bpmndi:BPMNPlane>
115+
</bpmndi:BPMNDiagram>
116+
</definitions>
49.6 KB
Loading
22.2 KB
Loading
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1489503806724" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
3+
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
4+
<startEvent id="_2" name="SI">
5+
<messageEventDefinition id="_2_ED_1"/>
6+
</startEvent>
7+
<userTask activiti:exclusive="true" id="_3" name="S1"/>
8+
<userTask activiti:exclusive="true" id="_4" name="S2"/>
9+
<endEvent id="_5" name="SF">
10+
<terminateEventDefinition id="_5_ED_1"/>
11+
<extensionElements>
12+
<activiti:executionListener event="start">
13+
<activiti:field>
14+
<activiti:string/>
15+
</activiti:field>
16+
</activiti:executionListener>
17+
<activiti:executionListener event="start"/>
18+
<activiti:executionListener event="start"/>
19+
<activiti:executionListener event="start"/>
20+
<activiti:executionListener event="start"/>
21+
</extensionElements>
22+
</endEvent>
23+
<sequenceFlow id="_6" sourceRef="_2" targetRef="_3"/>
24+
<sequenceFlow id="_7" sourceRef="_3" targetRef="_4"/>
25+
<sequenceFlow id="_8" sourceRef="_4" targetRef="_5"/>
26+
</process>
27+
<bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
28+
<bpmndi:BPMNPlane bpmnElement="myProcess_1">
29+
<bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
30+
<omgdc:Bounds height="32.0" width="32.0" x="85.0" y="95.0"/>
31+
<bpmndi:BPMNLabel>
32+
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
33+
</bpmndi:BPMNLabel>
34+
</bpmndi:BPMNShape>
35+
<bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3">
36+
<omgdc:Bounds height="40.0" width="75.0" x="180.0" y="105.0"/>
37+
<bpmndi:BPMNLabel>
38+
<omgdc:Bounds height="40.0" width="75.0" x="0.0" y="0.0"/>
39+
</bpmndi:BPMNLabel>
40+
</bpmndi:BPMNShape>
41+
<bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4">
42+
<omgdc:Bounds height="40.0" width="70.0" x="320.0" y="105.0"/>
43+
<bpmndi:BPMNLabel>
44+
<omgdc:Bounds height="40.0" width="70.0" x="0.0" y="0.0"/>
45+
</bpmndi:BPMNLabel>
46+
</bpmndi:BPMNShape>
47+
<bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5">
48+
<omgdc:Bounds height="32.0" width="32.0" x="460.0" y="95.0"/>
49+
<bpmndi:BPMNLabel>
50+
<omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
51+
</bpmndi:BPMNLabel>
52+
</bpmndi:BPMNShape>
53+
<bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_2" targetElement="_3">
54+
<omgdi:waypoint x="117.0" y="111.0"/>
55+
<omgdi:waypoint x="180.0" y="125.0"/>
56+
<bpmndi:BPMNLabel>
57+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
58+
</bpmndi:BPMNLabel>
59+
</bpmndi:BPMNEdge>
60+
<bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7" sourceElement="_3" targetElement="_4">
61+
<omgdi:waypoint x="255.0" y="125.0"/>
62+
<omgdi:waypoint x="320.0" y="125.0"/>
63+
<bpmndi:BPMNLabel>
64+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
65+
</bpmndi:BPMNLabel>
66+
</bpmndi:BPMNEdge>
67+
<bpmndi:BPMNEdge bpmnElement="_8" id="BPMNEdge__8" sourceElement="_4" targetElement="_5">
68+
<omgdi:waypoint x="390.0" y="125.0"/>
69+
<omgdi:waypoint x="460.0" y="111.0"/>
70+
<bpmndi:BPMNLabel>
71+
<omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
72+
</bpmndi:BPMNLabel>
73+
</bpmndi:BPMNEdge>
74+
</bpmndi:BPMNPlane>
75+
</bpmndi:BPMNDiagram>
76+
</definitions>

‎spring-state-machine/pom.xml‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>baeldung-spring-state-machine</artifactId>
13+
<properties>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.statemachine</groupId>
21+
<artifactId>spring-statemachine-core</artifactId>
22+
<version>1.2.3.RELEASE</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.11</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.spring.stateMachine.applicationReview;
2+
3+
public enum ApplicationReviewEvents {
4+
APPROVE, REJECT
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.spring.stateMachine.applicationReview;
2+
3+
public enum ApplicationReviewStates {
4+
PEER_REVIEW, PRINCIPAL_REVIEW, APPROVED, REJECTED
5+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.spring.stateMachine.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.statemachine.config.EnableStateMachine;
6+
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
7+
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
8+
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
9+
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
10+
import org.springframework.statemachine.guard.Guard;
11+
12+
@Configuration
13+
@EnableStateMachine
14+
public class ForkJoinStateMachineConfiguration extends StateMachineConfigurerAdapter<String, String> {
15+
16+
@Override
17+
public void configure(StateMachineConfigurationConfigurer<String, String> config)
18+
throws Exception {
19+
config
20+
.withConfiguration()
21+
.autoStartup(true)
22+
.listener(new StateMachineListener());
23+
}
24+
25+
@Override
26+
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
27+
states
28+
.withStates()
29+
.initial("SI")
30+
.fork("SFork")
31+
.join("SJoin")
32+
.end("SF")
33+
.and()
34+
.withStates()
35+
.parent("SFork")
36+
.initial("Sub1-1")
37+
.end("Sub1-2")
38+
.and()
39+
.withStates()
40+
.parent("SFork")
41+
.initial("Sub2-1")
42+
.end("Sub2-2");
43+
}
44+
45+
@Override
46+
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
47+
transitions.withExternal()
48+
.source("SI").target("SFork").event("E1")
49+
.and().withExternal()
50+
.source("Sub1-1").target("Sub1-2").event("sub1")
51+
.and().withExternal()
52+
.source("Sub2-1").target("Sub2-2").event("sub2")
53+
.and()
54+
.withFork()
55+
.source("SFork")
56+
.target("Sub1-1")
57+
.target("Sub2-1")
58+
.and()
59+
.withJoin()
60+
.source("Sub1-2")
61+
.source("Sub2-2")
62+
.target("SJoin");
63+
}
64+
65+
@Bean
66+
public Guard<String, String> mediumGuard() {
67+
return (ctx) -> false;
68+
}
69+
70+
@Bean
71+
public Guard<String, String> highGuard() {
72+
return (ctx) -> false;
73+
}
74+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.spring.stateMachine.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.statemachine.config.EnableStateMachine;
5+
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
6+
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
7+
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
8+
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
9+
10+
@Configuration
11+
@EnableStateMachine
12+
public class HierarchicalStateMachineConfiguration extends StateMachineConfigurerAdapter<String, String> {
13+
14+
@Override
15+
public void configure(StateMachineConfigurationConfigurer<String, String> config)
16+
throws Exception {
17+
config
18+
.withConfiguration()
19+
.autoStartup(true)
20+
.listener(new StateMachineListener());
21+
}
22+
23+
@Override
24+
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
25+
states
26+
.withStates()
27+
.initial("SI")
28+
.state("SI")
29+
.end("SF")
30+
.and()
31+
.withStates()
32+
.parent("SI")
33+
.initial("SUB1")
34+
.state("SUB2")
35+
.end("SUBEND");
36+
}
37+
38+
@Override
39+
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
40+
transitions.withExternal()
41+
.source("SI").target("SF").event("end")
42+
.and().withExternal()
43+
.source("SUB1").target("SUB2").event("se1")
44+
.and().withExternal()
45+
.source("SUB2").target("SUBEND").event("s-end");
46+
}
47+
}

0 commit comments

Comments
 (0)