Mastering Activiti: Build and Deploy a Multi‑Level Approval Workflow in Minutes
This article walks through designing, deploying, and executing a multi‑level leave‑approval workflow using the Activiti BPM engine, covering BPMN diagram creation, Spring Boot configuration, Java code for process initiation and task handling, database tables, key services, and best‑practice development steps.
Workflow Approval with Activiti
The workflow approval feature is a core capability of office OA systems. Using Activiti, you can model a leave‑approval process with multiple user tasks, exclusive gateways, and automatic branching without writing custom flow‑control code.
Designing the Process
First draw a BPMN diagram that includes a start event, a user task for the leave request, a supervisor approval task, an exclusive gateway for the first‑level result, a second‑level approval task (triggered when the leave exceeds three days), and end events for approval success or failure. The diagram can be created with the ActiBPM plugin in IntelliJ IDEA.
Deploying the Process
<code>ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = engine.getRepositoryService();
repositoryService.createDeployment()
.addClasspathResource("processes/apply.bpmn")
.deploy();</code>Deployment uploads the BPMN XML to the engine, which creates the necessary runtime tables automatically.
Starting a Process Instance
<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>The variables set the applicant and the two supervisors, allowing the engine to route tasks to the correct assignees.
Task Handling
<code>// Applicant submits request
Task firstTask = engine.getTaskService()
.createTaskQuery().taskAssignee("zhang3").singleResult();
engine.getTaskService().complete(firstTask.getId(), Maps.newHashMap("day", 4));
// First‑level supervisor approves
Task secondTask = engine.getTaskService()
.createTaskQuery().taskAssignee("li4").singleResult();
engine.getTaskService().setVariable(secondTask.getId(), "result1", true);
engine.getTaskService().complete(secondTask.getId());
// Second‑level 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>Each task query retrieves the pending task for the specific user, sets result variables, and completes the task, allowing the exclusive gateways to decide the next path.
Database Tables Used by Activiti
ACT_RE_* – stores process definitions and static resources.
ACT_RU_* – holds runtime data such as tasks, process instances, and variables.
ACT_HI_* – contains historic records for audit and reporting.
ACT_ID_* – manages users, groups, and permissions.
Key Services API
ProcessEngine – entry point to obtain other services.
RepositoryService – deploys and manages process definitions.
RuntimeService – starts, suspends, and deletes process instances.
TaskService – queries, claims, and completes user tasks.
HistoryService – queries completed tasks and historic process data.
BPMN Basics
BPMN (Business Process Modeling Notation) is a standard visual language for describing business processes, enabling clear communication between developers and business stakeholders.
Typical Development Steps
Define the BPMN diagram using a modeling tool.
Deploy the diagram to the workflow engine.
Start a new process instance with required variables.
Execute user tasks through the TaskService.
Register listeners to capture events such as task completion or process end.
Monitor and query process state and history via HistoryService.
Further Learning Topics
Event types and listeners.
Different task types: user, service, script.
Form management for dynamic input.
Gateways (exclusive, parallel) and conditional flows.
Performance, scaling, ID generation, and sharding strategies.
Conclusion
Activiti simplifies multi‑user workflow development: design a BPMN diagram, deploy it, and drive the process without writing extra flow‑control code. Adding new approval steps only requires updating the diagram and front‑end forms, while the engine handles routing and state management automatically.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.