Backend Development 25 min read

Designing and Implementing a Multi‑Level Approval Workflow with Activiti

This article explains how to design a two‑level leave‑approval process using the Activiti BPMN engine, covering diagram creation, Spring‑Boot configuration, deployment, task handling, exclusive gateways, database tables, API overview, and practical tips for extending the workflow.

Top Architect
Top Architect
Top Architect
Designing and Implementing a Multi‑Level Approval Workflow with Activiti

Workflow approval is a core capability of office OA systems, and a robust engine such as Activiti makes the implementation straightforward.

1. Create the BPMN diagram – draw a process that includes a start event, a leave‑request user task, a first‑level supervisor task, an exclusive gateway to decide approval, a second‑level supervisor task (triggered when the leave exceeds three days), and end events for approval or rejection. The diagram is stored as apply.bpmn and can be edited with the ActiBPM plugin in IntelliJ.

2. Deploy the diagram

ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = engine.getRepositoryService();
repositoryService.createDeployment()
    .addClasspathResource("processes/apply.bpmn")
    .deploy();

The deployment step is usually performed in the backend service during application startup.

3. Start a process instance

Map
variables = new HashMap<>();
variables.put("applyUser", "zhang3");
variables.put("supervisor", "li4");
variables.put("upperSupervisor", "wang5");
ProcessInstance instance = engine.getRuntimeService()
    .startProcessInstanceByKey("apply_processor_1", variables);

Variable ${applyUser} determines the applicant, while ${supervisor} and ${upperSupervisor} are used by the corresponding user tasks.

4. Handle tasks

// First‑level approval
Task firstTask = engine.getTaskService()
    .createTaskQuery().taskAssignee("zhang3").singleResult();
engine.getTaskService().complete(firstTask.getId(),
    Collections.singletonMap("day", 4));

// Supervisor approval
Task secondTask = engine.getTaskService()
    .createTaskQuery().taskAssignee("li4").singleResult();
engine.getTaskService().setVariable(secondTask.getId(), "result1", true);
engine.getTaskService().complete(secondTask.getId());

// Upper supervisor approval (if needed)
Task thirdTask = engine.getTaskService()
    .createTaskQuery().taskAssignee("wang5").singleResult();
if (thirdTask != null) {
    engine.getTaskService().setVariable(thirdTask.getId(), "result2", true);
    engine.getTaskService().complete(thirdTask.getId());
}

Exclusive gateways evaluate expressions such as #{result1==true} and #{day>3} to decide the next path, eliminating the need for custom branching code.

5. Database tables – Activiti creates several groups of tables: ACT_RE_* : process definitions and static resources. ACT_RU_* : runtime data (tasks, executions, variables). ACT_HI_* : historic data for audit. ACT_ID_* : identity and permission information.

6. Core API overview ProcessEngine : entry point to obtain services. RepositoryService : deploy and manage process definitions. RuntimeService : start, suspend, or delete process instances. TaskService : query, claim, and complete user tasks. HistoryService : retrieve completed tasks and execution history.

7. Comparison with other BPM engines – Camunda and Flowable are forks of Activiti; they share a similar development workflow (define BPMN, deploy, start instance, handle tasks, monitor).

8. Learning roadmap – explore event listeners, different task types, form management, parallel gateways, performance tuning, and database sharding.

Conclusion – Activiti enables rapid development of multi‑user approval processes with minimal code; most of the logic resides in the BPMN diagram, while developers only need to provide front‑end pages and simple service interfaces.

JavaBPMNSpring BootActivitiWorkflow EngineProcess Automation
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.