How to create Activiti Parallel Tasks with Spring JPA + Spring Boot Example

This tutorial shows you a Spring Boot example which is integrated with Activiti Parallel Tasks and Spring JPA.

Related Articles:
Introduction to Activiti – a Java BPM Engine
How to start Activiti + Spring JPA with Spring Boot

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.3.RELEASE

II. Overview
1. Goal

To build a Spring Boot application that helps to manage process as below:
activiti-parallel-spring-boot

– We will create a group of persons and use Spring JPA to store their information.
– Then we assign:
+ Check Hardware and Do Meeting to John.
+ Check Software and Check Firmware to David.
+ Write Report to Katherin.

– John’s tasks and David’s tasks can be handled in parallel. David doesn’t need to wait for any task which is assigned to John be done.
– Katherin is the assignee of Write Report task only if Check Hardware, Check Software and Check Firmware are done.

– After report is done, ‘notification’ message will be shown in the System Console.

2. Project Structure

activiti-parallel-spring-boot-structure

– Process definition will be written under XML format in process/process.bpmn20.xml file.
MyService provides service methods for Controller such as: startProcess(), getTasks(), completeTask(). Those functions can be done with help of some @Autowired objects which are instances of these classes:
+ RepositoryService
+ RuntimeService
+ TaskService
+ PersonRepository
PersonRepository is an interface extends JpaRepository for implementing repository methods on Person entities.

SpringParallelActivitiApplication class contains Bean that calls MyService‘s creating persons method when starting the application.
MyController is a REST Controller which has request mapping methods for RESTful requests such as: /process, /tasks, /completetask.

pom.xml contains dependencies for:
+ Activiti Spring Boot Starter Basic, Activiti Spring Boot Starter JPA
+ Spring Boot Starter Web
+ H2 database
+ Groovy: scripting engine for running Script Task in process.

Notes: We must add Groovy Jar file (groovy-all-2.4.10.jar) to make it work.
3. Step to do

We will follow these steps to make things done:
– Create Spring Boot project
– Define Process
– Create DataModel Class and JPA Repository Interface
– Create Service Class
– Create Application Class
– Create Controller
– Run & Check Result

III. Practice
1. Create Spring Maven project

– Using Spring Tool Suite to create Project and add Dependencies to pom.xml file:

– Add Groovy Jar file (groovy-all-2.4.10.jar) as Referenced Library.

2. Define Process

Under src/main/resources, create processes folder and add simple-process.bpmn20.xml file:

We use ${hardwareChecker.name}, ${softwareChecker.name}, ${reporter.name} expressions and activiti:assignee attribute for assigning task.

3. Create DataModel Class and JPA Repository Interface

4. Create Service Class

5. Create Application Class

6. Create Controller

7. Run & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1: Assign tasks to John,David,Katherin
$ curl -H "Content-Type: application/json" -d '{"assignees": ["John","David","Katherin"]}' http://localhost:8080/process

Request 2: show tasks of David
$ curl http://localhost:8080/tasks/David

David has only one task at the present because he hasn’t done Check Software task.

Request 3: show tasks of John
http://localhost:8080/tasks/John

John has two tasks in parallel.

Request 4: show tasks of Katherin
http://localhost:8080/tasks/Katherin

[] >> No result.

Request 5.1: complete Check Hardware
$ curl http://localhost:8080/completetask?id=17

Request 5.2: complete Check Software
$ curl http://localhost:8080/completetask?id=19

Request 6: show tasks of David
$ curl http://localhost:8080/tasks/David

Request 7: complete Check Firmware
$ curl http://localhost:8080/completetask?id=24

Request 8: show tasks of Katherin
$ curl http://localhost:8080/tasks/Katherin

Request 7: complete Write Report
$ curl http://localhost:8080/completetask?id=27

And System Console shows:

IV. Source Code

SpringParallelActiviti

By JavaSampleApproach | May 14, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*