Mastering Activiti Event Listeners in Spring Boot: Task and Process Runtime Events
This guide explains how to configure and use Activiti's TaskRuntimeEventListeners and ProcessRuntimeEventListeners in a Spring Boot application, covering TaskAssignedEvent, TaskCompletedEvent, and ProcessCompletedEvent with code examples, testing steps, and important authentication settings.
This article demonstrates how to integrate Activiti workflow engine event listeners into a Spring Boot project.
Task Runtime Event Listeners
The primary listener interface is TaskRuntimeEventListeners , which receives RuntimeEvent objects. Two commonly used events are TaskAssignedEvent (task assignment) and TaskCompletedEvent (task completion).
TaskAssignedEvent
@Configuration
public class EventListenerConfig {
private static Logger logger = LoggerFactory.getLogger(EventListenerConfig.class);
@Bean
public TaskRuntimeEventListener<TaskAssignedEvent> taskAssignedListener() {
return taskAssignedEvent -> {
logger.info("BusinessKey: {}", taskAssignedEvent.getBusinessKey());
Task task = taskAssignedEvent.getEntity();
logger.info("task, id: {}, name: {}, assignee: {}", task.getId(), task.getName(), task.getAssignee());
logger.info("ProcessDefinitionId: {}", taskAssignedEvent.getProcessDefinitionId());
};
}
}This listener simply logs assignment details; you can extend it with custom business logic.
TaskCompletedEvent
@Bean
public TaskRuntimeEventListener<TaskCompletedEvent> taskCompletedEvent() {
return taskCompletedEvent -> {
logger.info("BusinessKey: {}", taskCompletedEvent.getBusinessKey());
Task task = taskCompletedEvent.getEntity();
logger.info("task, id: {}, name: {}, owner: {}", task.getId(), task.getName(), task.getOwner());
};
}When a task finishes, this listener logs the relevant information.
Process Runtime Event Listener
For process-level events, use ProcessRuntimeEventListener with RuntimeEvent . The example below handles ProcessCompletedEvent :
@Bean
public ProcessRuntimeEventListener<ProcessCompletedEvent> processCompletedEvent() {
return processCompletedEvent -> {
ProcessInstance instance = processCompletedEvent.getEntity();
logger.info("任务发起人:{}", instance.getInitiator());
};
}This listener is triggered when a process instance ends.
Testing the Listeners
Start a task via the endpoint
/holidays/start?processDefinitionId=holiday:1:...&userId=10000, which assigns the task to the user with ID 10000. The console output shows the logged assignment details.
Subsequent calls such as
/holidays/apply?days=3&mgr=10001&explain=生病&instanceId=...assign the task to a manager, and the logs confirm the assignment.
Important Authentication Note
To ensure the task initiator is correctly recorded, set the authenticated user before starting the process:
public void startProcessInstanceAssignVariables(String processDefinitionId, Map<String, Object> variables) {
Authentication.setAuthenticatedUserId((String) variables.get("assignee"));
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, variables);
logger.info("流程定义ID: {}", processInstance.getProcessDefinitionId());
logger.info("流程实例ID: {}", processInstance.getId());
logger.info("BussinessKey: {}", processInstance.getBusinessKey());
}The assignee parameter corresponds to the user who initiates the task via the start endpoint.
After the process completes, the act_hi_procinst table contains the initiator information.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
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.
