Master Activiti: Build a Multi‑Level Approval Workflow with Spring Boot
This guide walks you through designing a multi‑level leave‑approval workflow using Activiti, from drawing the BPMN diagram and configuring the engine to deploying the process, handling tasks, setting variables, and visualizing execution history, all with concise Java code examples and practical tips.
Workflow approval is a core capability of office OA systems, and implementing it with Activiti eliminates the need for custom code to drive process flow and branch decisions.
First draw a process diagram
Use the ActiBPM plugin in IntelliJ to create a BPMN diagram (apply.mpmn) that models a leave request with two‑level supervisor approval.
Level‑1 supervisor approval
If the leave exceeds three days, trigger level‑2 supervisor approval
Deploy the process diagram
Deploy the BPMN file to the Activiti engine using the RepositoryService.
<code>ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = engine.getRepositoryService();
repositoryService.createDeployment().addClasspathResource("processes/apply.bpmn").deploy();</code>Start a process instance
Provide variables for the applicant and supervisors, then start the process by its key.
<code>Map<String, Object> variableMap = new HashMap<>();
variableMap.put("applyUser", "zhang3");
variableMap.put("supervisor", "li4");
variableMap.put("upperSupervisor", "wang5");
ProcessInstance instance = engine.getRuntimeService()
.startProcessInstanceByKey("apply_processor_1", variableMap);</code>Handle tasks
Query and complete tasks for each participant, setting result variables that the exclusive gateways later evaluate.
<code>// Applicant completes the request and sets the leave days
Task firstTask = engine.getTaskService().createTaskQuery().taskAssignee("zhang3").singleResult();
engine.getTaskService().complete(firstTask.getId(), Maps.newHashMap("day", 4));
// Level‑1 supervisor approves
Task secondTask = engine.getTaskService().createTaskQuery().taskAssignee("li4").singleResult();
engine.getTaskService().setVariable(secondTask.getId(), "result1", true);
engine.getTaskService().complete(secondTask.getId());
// Level‑2 supervisor (if needed) approves
Task thirdTask = engine.getTaskService().createTaskQuery().taskAssignee("wang5").singleResult();
if (thirdTask != null) {
engine.getTaskService().setVariable(thirdTask.getId(), "result2", true);
engine.getTaskService().complete(thirdTask.getId());
}
</code>Exclusive gateways drive branch logic
The engine checks the conditions on the gateways. For example, the first gateway uses
#{result1==true}to decide whether to continue to the second‑level approval, and the second uses
#{day>3}to decide if the second‑level path is taken.
Workflow engine API overview
ProcessEngine: entry point to obtain all services.
RepositoryService: deploy and manage process definitions.
RuntimeService: start, suspend, or delete process instances.
TaskService: query, claim, and complete user tasks.
HistoryService: query completed tasks, variables, and execution history.
Database tables used by Activiti
Activiti stores definitions, runtime data, history, and identity information in tables prefixed with
ACT_RE_,
ACT_RU_,
ACT_HI_, and
ACT_ID_respectively.
What is BPMN?
BPMN (Business Process Modeling Notation) is a standard visual language for describing business processes, enabling clear communication across departments.
Project basic configuration
Add Activiti dependency
<code><dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.23.0</version>
</dependency></code>Configure datasource (H2 for learning)
<code><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;mode=MySQL;"/>
</bean>
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="databaseSchemaUpdate" value="true"/>
</bean></code>Further learning directions
Event types and listeners
Task types: user, service, script
Form management
Parallel gateways and other flow controls
Performance, ID generation, sharding
Conclusion
Activiti is ideal for multi‑user approval processes.
Workflow development consists of designing the BPMN diagram, deploying it, and driving instances via code.
Adding new process templates requires no additional engine code—only front‑end pages and simple back‑end interfaces.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.