Activiti is an open-source BPM (Business Process Management) framework that integrates with Spring to provide workflow and business process automation capabilities. To get started with Spring and Activiti, you can follow the steps below:
Step 1: Set Up a Spring Boot Project
-
Create a new Spring Boot project using Spring Initializr: Spring Initializr.
- Choose the desired project settings (e.g., Java version, packaging, dependencies).
- Add the "Spring Web" and "Spring Data JPA" dependencies.
-
Download the project and extract it.
Step 2: Add Activiti Dependency
-
Open the
pom.xml
file in your project. -
Add the Activiti dependency:
-
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-basic</artifactId> <version>7.1.0.M6</version> </dependency>
Ensure that you use the latest version of Activiti available
-
Create a new directory named
src/main/resources/processes
. -
Inside the
processes
directory, create a BPMN file (e.g.,simpleProcess.bpmn20.xml
). Here is an example BPMN process:<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Examples"> <process id="simpleProcess" name="Simple Process" isExecutable="true"> <startEvent id="startEvent" /> <sequenceFlow id="flow1" sourceRef="startEvent" targetRef="task1" /> <userTask id="task1" name="User Task 1"> <documentation>First User Task</documentation> </userTask> <sequenceFlow id="flow2" sourceRef="task1" targetRef="endEvent" /> <endEvent id="endEvent" /> </process> </definitions>
-
Create a Java class for starting the process. For example,
ProcessService.java
:import org.activiti.engine.RuntimeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ProcessService { @Autowired private RuntimeService runtimeService; public void startProcess() { runtimeService.startProcessInstanceByKey("simpleProcess"); } }
-
Open the main application class (the one with
@SpringBootApplication
) and add the@SpringBootApplication
annotation:import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ActivitiSpringBootApplication { public static void main(String[] args) { SpringApplication.run(ActivitiSpringBootApplication.class, args); } }
-
Run your Spring Boot application.
-
Use a tool like Postman or your web browser to make a POST request to
http://localhost:8080/actuator/activiti/process/start
. -
Check the console logs for any errors or successful process initiation.