How to start Activiti + Spring JPA with Spring Boot

In previous post, you was introduced BPMN and Activiti along with its API. This tutorial shows you a simple Spring Boot example which is integrated with Activiti and Spring JPA.

Related Articles:
Introduction to Activiti – a Java BPM Engine
Activiti Parallel Tasks + JPA with Spring Boot Example
Activiti REST API with Spring Boot Example

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-jpa-springboot-process

– We will create a group of persons and use Spring JPA to store their information.
– Then we assign Task 1 and Task 2 to one of them who is called assignee.
– After he finishes 2 tasks, ‘final Notice’ message will be shown in the System Console.

2. Project Structure

activiti-jpa-springboot

– Process definition will be written under XML format in process/simple-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.

SpringStarterActivitiApplication 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 ${person.name} expression 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 by open your browser and send request:

Request 1: Assign tasks to David
http://localhost:8080/process?assignee=David

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

David has only one Task at the present because he hasn’t done the Task 1.

Request 3: complete Task 1
http://localhost:8080/completetask?id=8

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

David now works on Task 2.

Request 5: complete Task 2
http://localhost:8080/completetask?id=11

And System Console shows:

IV. Source Code

SpringStarterActiviti

By grokonez | May 13, 2017.



Related Posts


10 thoughts on “How to start Activiti + Spring JPA with Spring Boot”

  1. Greetings,
    I tried to run this application but i get this following error :
    Description:

    Parameter 1 of method springProcessEngineConfiguration in org.activiti.spring.boot.JpaProcessEngineAutoConfiguration$JpaConfiguration required a bean of type ‘javax.persistence.EntityManagerFactory’ that could not be found.

    Action:

    Consider defining a bean of type ‘javax.persistence.EntityManagerFactory’ in your configuration.

    what should i do.

    thanks

    1. Hi Manish,

      Just change configuration in application.properties file for MySQL database 🙂

      Regards,
      JSA.

  2. Hello, your tests fail =/ with the following error
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’

  3. Hi nice.. I have 2 tables Emp and Role having some values in backend(Postgres).
    I want to create a simple flow ->
    Task 1 – Invoke process by fetching Employee IDs from Emp (1 / 2 / 3 /etc)..
    Task 2 – It will check in DB if user exists, if true proceed ahead else terminate.
    Task 3 – If user exists, then, check its dept.
    If dept -> IT – Task 4 to execute.
    If dept -> Finance – Task 5 to execute.
    If dept -> Not Assigned – Task 6 to execute.

    How to achieve this? I have already created some methods in ServiceLayer of SpringBoot, which I need to execute for every Task defined above. How to achieve this?

  4. Thank you so much for taking time out to produce this.
    This is the best, and yet simplified, introduction to embedding Activiti BPM into Java applications!
    Again, thank you for putting this together.
    You just made my work very easy and saved me countless trials!

  5. hi,
    it shows an error “The archive: E:/STS/groovy-all-2.4.10.jar which is referenced by the classpath, does not exist.”

    please help !

    Regards,
    Ankur

Got Something To Say:

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

*