Mastering Spring Boot & Activiti: Essential Workflow Operations

This guide demonstrates how to integrate Spring Boot with the Activiti workflow engine, covering common operations such as querying owner tasks, retrieving assigned tasks, suspending, activating, and deleting process instances, complete with controller examples and sample API calls.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring Boot & Activiti: Essential Workflow Operations

Explanation: This article builds on three previous posts; please read them first.

Springboot Integration with Activiti (Part 1)

Springboot Integration with Activiti (Part 2)

Springboot Integration with Activiti (Part 3)

Content: Common Activiti operations.

Query tasks owned by a specific user

/**
 * Query tasks owned by a given user
 * Time: 2021-01-31 11:17:49
 */
public List<HistoricProcessInstance> ownerTasks(String userId) {
    HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
    List<HistoricProcessInstance> list = query.involvedUser(userId).list();
    return list;
}

Controller:

@GetMapping("/{userId}")
public R myMission(@PathVariable("userId") String userId) {
    return R.success(holidayService.ownerTasks(userId));
}

API endpoint:

/holidays/10000

If the returned endTime is null, the task has no result.

My pending tasks

/**
 * Query tasks assigned to a user
 * Time: 2021-01-23 11:39:56
 */
public List<Task> queryTasks(String assignee) {
    TaskQuery query = taskService.createTaskQuery();
    return query.taskAssignee(assignee).orderByTaskCreateTime().asc().list();
}

Controller:

@GetMapping("/tasks")
public R myTasks(String userId) {
    List<Task> list = holidayService.queryTasks(userId);
    List<Map<String, Object>> result = list.stream().map(task -> {
        Map<String, Object> res = new HashMap<>();
        res.put("id", task.getId());
        res.put("assignee", task.getAssignee());
        res.put("createTime", task.getCreateTime());
        res.put("bussinessKey", task.getBusinessKey());
        res.put("category", task.getCategory());
        res.put("dueDate", task.getDueDate()); // due date
        res.put("desc", task.getDescription());
        res.put("name", task.getName());
        res.put("owner", task.getOwner());
        res.put("instanceId", task.getProcessInstanceId());
        res.put("variables", task.getProcessVariables());
        res.put("state", task.isSuspended());
        return res;
    }).collect(Collectors.toList());
    return R.success(result);
}

Suspend a process instance

@GetMapping("/suspend")
public R suspendProcessInstance(String processInstanceId) {
    holidayService.suspendProcess(processInstanceId);
    return R.success();
}

Controller method with Javadoc:

/**
 * Suspend process
 * Time: 2021-01-29 16:10:44
 */
@GetMapping("/suspend")
public R suspendProcessInstance(String processInstanceId) {
    holidayService.suspendProcess(processInstanceId);
    return R.success();
}

Testing steps include starting a process, executing suspend, and observing that a suspended task cannot be completed.

Activate a process instance

public void activateProcess(String processInstanceId) {
    runtimeService.activateProcessInstanceById(processInstanceId);
}

Controller:

@GetMapping("/activate")
public R activateProcessInstance(String processInstanceId) {
    holidayService.activateProcess(processInstanceId);
    return R.success();
}

Delete a process instance

/**
 * Delete process
 * Time: 2021-01-29 15:50:08
 */
public void stopProcess(String processInstanceId) {
    runtimeService.deleteProcessInstance(processInstanceId, "Approval error");
}

Controller:

@GetMapping("/remove")
public R removeProcessInstance(String processInstanceId) {
    holidayService.stopProcess(processInstanceId);
    return R.success();
}

Further methods for termination and rejection will be added later.

Finished!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

process managementSpring BootREST APIActivitiWorkflow Engine
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

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.