Skip to content

Commit a1388b6

Browse files
mansi2392pivovarit
authored andcommitted
BAEL-378 A Guide to Activiti with Java (eugenp#2245)
* Example Code For Evaluation Article This is an example code for the evaluation article on "Different Types of Bean Injection in Spring" * Added unit tests * Minor changes to application context * Removed code committed for evaluation article * BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5 * BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5 * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Exploring the Spring MVC URL Matching Improvements * BAEL-944 Code Formatting and solving build issue * BAEL-944 Resolving build issue due to change in Spring version * BAEL-944 Resolving build issue * BAEL-944 Formatting code * BAEL-944 Moving tests to correct package * BAEL-944 Moving tests to correct package * BAEL-944 Replacing @RequestMapping by @GetMapping * BAEL-944 Remove unnecessary attribute name, "value" in annotations * BAEL-79 Intro to Activiti with Spring * BAEL-79 Intro to Activiti with Spring * BAEL-79 Adding activiti module to the parent modules * BAEL-79 Using latest version * BAEL-79 Update Spring boot version that works with Activiti * BAEL-79 Replace RequestMapping with GetMapping * BAEL-79 Use Java 8 Syntax * BAEL-79 Formatting * BAEL-79 changed module name * BAEL-378 A Guide to Activiti with Java * BAEL-79 Fixed unit tests * BAEL-79 Simplified the process * BAEL-79 Fix test cases
1 parent ddee04f commit a1388b6

9 files changed

Lines changed: 388 additions & 62 deletions

File tree

‎spring-activiti/src/main/java/com/example/activitiwithspring/ActivitiController.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,11 @@ public List<TaskRepresentation> getTasks(@PathVariable String processInstanceId)
4242
}
4343

4444
@GetMapping("/complete-task-A/{processInstanceId}")
45-
public TaskRepresentation completeTaskA(@PathVariable String processInstanceId) {
45+
public void completeTaskA(@PathVariable String processInstanceId) {
4646
Task task = taskService.createTaskQuery()
4747
.processInstanceId(processInstanceId)
4848
.singleResult();
4949
taskService.complete(task.getId());
5050
logger.info("Task completed");
51-
task = taskService.createTaskQuery()
52-
.processInstanceId(processInstanceId)
53-
.singleResult();
54-
55-
return new TaskRepresentation(task.getId(), task.getName(), task.getProcessInstanceId());
5651
}
5752
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.activitiwithspring.servicetasks;
2+
3+
import org.activiti.engine.delegate.DelegateExecution;
4+
import org.activiti.engine.delegate.JavaDelegate;
5+
6+
public class SendEmailServiceTask implements JavaDelegate {
7+
8+
public void execute(DelegateExecution execution) {
9+
//logic to sent email confirmation
10+
}
11+
12+
}

‎spring-activiti/src/main/resources/processes/my-process.bpmn20.xml‎

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,35 @@
11
<?xml version='1.0' encoding='UTF-8'?>
2-
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4-
xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
5-
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
6-
typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
7-
targetNamespace="http://www.activiti.org/processdef" xmlns:modeler="http://activiti.com/modeler"
8-
modeler:version="1.0ev" modeler:exportDateTime="20170619132653"
9-
modeler:modelId="1005315" modeler:modelVersion="1"
10-
modeler:modelLastUpdated="1497875208422">
11-
<process id="my-process" name="my-process"
12-
isExecutable="true">
2+
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef" xmlns:modeler="http://activiti.com/modeler" modeler:version="1.0ev" modeler:exportDateTime="20170726123124" modeler:modelId="1005315" modeler:modelVersion="1" modeler:modelLastUpdated="1501068675875">
3+
<process id="my-process" name="say-hello-process" isExecutable="true">
134
<startEvent id="startEvent" name="startEvent">
145
</startEvent>
15-
<sequenceFlow id="sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4"
16-
sourceRef="startEvent" targetRef="sid-293091EF-68C4-4A5C-B747-B1C46809CB69">
17-
</sequenceFlow>
18-
<userTask id="sid-293091EF-68C4-4A5C-B747-B1C46809CB69" name="A"
19-
activiti:assignee="$INITIATOR">
20-
</userTask>
21-
<sequenceFlow id="sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB"
22-
sourceRef="sid-293091EF-68C4-4A5C-B747-B1C46809CB69" targetRef="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010">
23-
</sequenceFlow>
24-
<userTask id="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010" name="B"
25-
activiti:assignee="$INITIATOR">
6+
<sequenceFlow id="sequence-flow-1" sourceRef="startEvent" targetRef="A">
7+
</sequenceFlow>
8+
<userTask id="A" name="A" activiti:assignee="$INITIATOR">
269
</userTask>
27-
<sequenceFlow id="sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9"
28-
sourceRef="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010" targetRef="endEvent">
10+
<sequenceFlow id="sequence-flow-2" sourceRef="A" targetRef="endEvent">
2911
</sequenceFlow>
3012
<endEvent id="endEvent" name="endEvent">
3113
</endEvent>
3214
</process>
3315
<bpmndi:BPMNDiagram id="BPMNDiagram_say-hello-process">
34-
<bpmndi:BPMNPlane bpmnElement="say-hello-process"
35-
id="BPMNPlane_say-hello-process">
16+
<bpmndi:BPMNPlane bpmnElement="say-hello-process" id="BPMNPlane_say-hello-process">
3617
<bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
37-
<omgdc:Bounds height="30.0" width="30.0" x="120.0" y="163.0" />
18+
<omgdc:Bounds height="30.0" width="30.0" x="120.0" y="163.0"/>
3819
</bpmndi:BPMNShape>
3920
<bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent">
40-
<omgdc:Bounds height="28.0" width="28.0" x="525.0" y="164.0" />
21+
<omgdc:Bounds height="28.0" width="28.0" x="365.0" y="164.0"/>
4122
</bpmndi:BPMNShape>
42-
<bpmndi:BPMNShape bpmnElement="sid-293091EF-68C4-4A5C-B747-B1C46809CB69"
43-
id="BPMNShape_sid-293091EF-68C4-4A5C-B747-B1C46809CB69">
44-
<omgdc:Bounds height="80.0" width="100.0" x="210.0" y="138.0" />
23+
<bpmndi:BPMNShape bpmnElement="A" id="BPMNShape_A">
24+
<omgdc:Bounds height="80.0" width="100.0" x="210.0" y="138.0"/>
4525
</bpmndi:BPMNShape>
46-
<bpmndi:BPMNShape bpmnElement="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010"
47-
id="BPMNShape_sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010">
48-
<omgdc:Bounds height="80.0" width="100.0" x="360.0" y="138.0" />
49-
</bpmndi:BPMNShape>
50-
<bpmndi:BPMNEdge bpmnElement="sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9"
51-
id="BPMNEdge_sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9">
52-
<omgdi:waypoint x="460.0" y="178.0" />
53-
<omgdi:waypoint x="525.0" y="178.0" />
54-
</bpmndi:BPMNEdge>
55-
<bpmndi:BPMNEdge bpmnElement="sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB"
56-
id="BPMNEdge_sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB">
57-
<omgdi:waypoint x="310.0" y="178.0" />
58-
<omgdi:waypoint x="360.0" y="178.0" />
26+
<bpmndi:BPMNEdge bpmnElement="sequence-flow-1" id="BPMNEdge_sequence-flow-1">
27+
<omgdi:waypoint x="150.0" y="178.0"/>
28+
<omgdi:waypoint x="210.0" y="178.0"/>
5929
</bpmndi:BPMNEdge>
60-
<bpmndi:BPMNEdge bpmnElement="sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4"
61-
id="BPMNEdge_sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4">
62-
<omgdi:waypoint x="150.0" y="178.0" />
63-
<omgdi:waypoint x="210.0" y="178.0" />
30+
<bpmndi:BPMNEdge bpmnElement="sequence-flow-2" id="BPMNEdge_sequence-flow-2">
31+
<omgdi:waypoint x="310.0" y="178.0"/>
32+
<omgdi:waypoint x="365.0" y="178.0"/>
6433
</bpmndi:BPMNEdge>
6534
</bpmndi:BPMNPlane>
6635
</bpmndi:BPMNDiagram>

‎spring-activiti/src/test/java/com/example/activitiwithspring/ActivitiControllerIntegrationTest.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,12 @@ public void givenProcess_whenCompleteTaskA_thenReceivedNextTask() throws Excepti
106106
.get(0);
107107

108108
logger.info("process instance = " + pi.getId());
109-
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/complete-task-A/" + pi.getId()))
110-
.andReturn()
111-
.getResponse()
112-
.getContentAsString();
113-
114-
ObjectMapper mapper = new ObjectMapper();
115-
TaskRepresentation task = mapper.readValue(responseBody, TaskRepresentation.class);
116-
assertEquals("B", task.getName());
109+
this.mockMvc.perform(MockMvcRequestBuilders.get("/complete-task-A/" + pi.getId()))
110+
.andReturn()
111+
.getResponse()
112+
.getContentAsString();
117113

114+
List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().list();
115+
assertEquals(0, list.size());
118116
}
119117
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.example.activitiwithspring;
2+
3+
import org.activiti.engine.ProcessEngine;
4+
import org.activiti.engine.ProcessEngineConfiguration;
5+
import org.activiti.engine.ProcessEngines;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotNull;
8+
import org.junit.Test;
9+
10+
public class ProcessEngineCreationTests {
11+
12+
@Test
13+
public void givenXMLConfig_whenGetDefault_thenGotProcessEngine() {
14+
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
15+
assertNotNull(processEngine);
16+
assertEquals("root", processEngine.getProcessEngineConfiguration().getJdbcUsername());
17+
}
18+
19+
@Test
20+
public void givenXMLConfig_whenCreateDefaultConfiguration_thenGotProcessEngine() {
21+
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
22+
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
23+
assertNotNull(processEngine);
24+
assertEquals("root", processEngine.getProcessEngineConfiguration().getJdbcUsername());
25+
}
26+
27+
@Test
28+
public void givenDifferentNameXMLConfig_whenGetProcessEngineConfig_thenGotResult() {
29+
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("my.activiti.cfg.xml");
30+
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
31+
assertNotNull(processEngine);
32+
assertEquals("baeldung", processEngine.getProcessEngineConfiguration().getJdbcUsername());
33+
}
34+
35+
@Test
36+
public void givenDifferentBeanNameInXMLConfig_whenGetProcessEngineConfig_thenGotResult() {
37+
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
38+
.createProcessEngineConfigurationFromResource("my.activiti.cfg.xml", "myProcessEngineConfiguration");
39+
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
40+
assertNotNull(processEngine);
41+
assertEquals("baeldung", processEngine.getProcessEngineConfiguration().getJdbcUsername());
42+
}
43+
44+
@Test
45+
public void givenNoXMLConfig_whenCreateInMemProcessEngineConfig_thenCreated() {
46+
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
47+
ProcessEngine processEngine = processEngineConfiguration
48+
.setJdbcUrl("jdbc:h2:mem:my-own-in-mem-db;DB_CLOSE_DELAY=1000")
49+
.buildProcessEngine();
50+
assertNotNull(processEngine);
51+
assertEquals("sa", processEngine.getProcessEngineConfiguration().getJdbcUsername());
52+
}
53+
54+
@Test
55+
public void givenNoXMLConfig_whenCreateProcessEngineConfig_thenCreated() {
56+
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
57+
ProcessEngine processEngine = processEngineConfiguration
58+
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
59+
.setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
60+
.buildProcessEngine();
61+
assertNotNull(processEngine);
62+
assertEquals("sa", processEngine.getProcessEngineConfiguration().getJdbcUsername());
63+
}
64+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.example.activitiwithspring;
2+
3+
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.activiti.engine.ActivitiException;
9+
import org.activiti.engine.ProcessEngine;
10+
import org.activiti.engine.ProcessEngines;
11+
import org.activiti.engine.RepositoryService;
12+
import org.activiti.engine.RuntimeService;
13+
import org.activiti.engine.TaskService;
14+
import org.activiti.engine.runtime.ProcessInstance;
15+
import org.activiti.engine.task.Task;
16+
import static org.junit.Assert.assertNotNull;
17+
import static org.junit.Assert.assertTrue;
18+
import org.junit.Test;
19+
20+
public class ProcessExecutionTests {
21+
22+
@Test
23+
public void givenBPMN_whenDeployProcess_thenDeployed() {
24+
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
25+
RepositoryService repositoryService = processEngine.getRepositoryService();
26+
repositoryService.createDeployment()
27+
.addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml")
28+
.deploy();
29+
Long count = repositoryService.createProcessDefinitionQuery().count();
30+
assertTrue(count >= 1);
31+
}
32+
33+
@Test
34+
public void givenProcessDefinition_whenStartProcessInstance_thenProcessRunning() {
35+
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
36+
RepositoryService repositoryService = processEngine.getRepositoryService();
37+
repositoryService.createDeployment()
38+
.addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml")
39+
.deploy();
40+
41+
Map<String, Object> variables = new HashMap<String, Object>();
42+
variables.put("employeeName", "Kermit");
43+
variables.put("numberOfDays", new Integer(4));
44+
variables.put("vacationMotivation", "I'm really tired!");
45+
46+
RuntimeService runtimeService = processEngine.getRuntimeService();
47+
ProcessInstance processInstance = runtimeService
48+
.startProcessInstanceByKey("vacationRequest", variables);
49+
50+
Long count = runtimeService.createProcessInstanceQuery().count();
51+
assertTrue(count >= 1);
52+
}
53+
54+
@Test
55+
public void givenProcessInstance_whenCompleteTask_thenProcessExecutionContinues() {
56+
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
57+
RepositoryService repositoryService = processEngine.getRepositoryService();
58+
repositoryService.createDeployment()
59+
.addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml")
60+
.deploy();
61+
62+
Map<String, Object> variables = new HashMap<String, Object>();
63+
variables.put("employeeName", "Kermit");
64+
variables.put("numberOfDays", new Integer(4));
65+
variables.put("vacationMotivation", "I'm really tired!");
66+
67+
RuntimeService runtimeService = processEngine.getRuntimeService();
68+
ProcessInstance processInstance = runtimeService
69+
.startProcessInstanceByKey("vacationRequest", variables);
70+
71+
TaskService taskService = processEngine.getTaskService();
72+
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list();
73+
74+
Task task = tasks.get(0);
75+
76+
Map<String, Object> taskVariables = new HashMap<String, Object>();
77+
taskVariables.put("vacationApproved", "false");
78+
taskVariables.put("comments", "We have a tight deadline!");
79+
taskService.complete(task.getId(), taskVariables);
80+
81+
Task currentTask = taskService.createTaskQuery().taskName("Modify vacation request").singleResult();
82+
assertNotNull(currentTask);
83+
}
84+
85+
@Test(expected = ActivitiException.class)
86+
public void givenProcessDefinition_whenSuspend_thenNoProcessInstanceCreated() {
87+
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
88+
RepositoryService repositoryService = processEngine.getRepositoryService();
89+
repositoryService.createDeployment()
90+
.addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml")
91+
.deploy();
92+
93+
RuntimeService runtimeService = processEngine.getRuntimeService();
94+
repositoryService.suspendProcessDefinitionByKey("vacationRequest");
95+
runtimeService.startProcessInstanceByKey("vacationRequest");
96+
}
97+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
6+
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
7+
8+
<property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
9+
<property name="jdbcDriver" value="org.h2.Driver" />
10+
<property name="jdbcUsername" value="root" />
11+
<property name="jdbcPassword" value="" />
12+
13+
<property name="databaseSchemaUpdate" value="true" />
14+
</bean>
15+
16+
</beans>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
6+
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
7+
8+
<property name="jdbcUrl" value="jdbc:h2:mem:baeldung;DB_CLOSE_DELAY=1000" />
9+
<property name="jdbcDriver" value="org.h2.Driver" />
10+
<property name="jdbcUsername" value="baeldung" />
11+
<property name="jdbcPassword" value="" />
12+
13+
<property name="databaseSchemaUpdate" value="true" />
14+
</bean>
15+
16+
<bean id="myProcessEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
17+
18+
<property name="jdbcUrl" value="jdbc:h2:mem:baeldung;DB_CLOSE_DELAY=1000" />
19+
<property name="jdbcDriver" value="org.h2.Driver" />
20+
<property name="jdbcUsername" value="baeldung" />
21+
<property name="jdbcPassword" value="" />
22+
23+
<property name="databaseSchemaUpdate" value="true" />
24+
</bean>
25+
26+
</beans>

0 commit comments

Comments
 (0)