How to Implement Activiti Event Listeners in Spring Boot: Task and Process Events
This tutorial demonstrates how to integrate Activiti workflow engine event listeners into a Spring Boot application, covering task assignment, task completion, and process completion events with code examples, testing steps, and important configuration details.
This article continues the previous two parts and focuses on using Activiti events within a Spring Boot application.
Event listeners
The main listener interfaces are TaskRuntimeEventListeners for task events and ProcessRuntimeEventListener for process events, both handling RuntimeEvent objects.
TaskAssignedEvent (task assignment) example:
<code>@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());
};
}
}
</code>The listener logs task assignment details; you can extend it for more complex business logic.
Testing the task assignment:
Start a task via /holidays/start?processDefinitionId=holiday:1:65d9157a-61e6-11eb-aa1f-00d861e5b732&userId=10000 , which assigns the task to user 10000.
Console output shows the logged assignment information, confirming the listener works.
Next, call /holidays/apply?days=3&mgr=10001&explain=生病&instanceId=306224db-61e7-11eb-aa1f-00d861e5b732 to move the task to the department manager.
TaskCompletedEvent (task completion) example:
<code>@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());
};
}
</code>Resulting console output after completing the task confirms the listener execution.
ProcessRuntimeEventListener for process events, e.g., ProcessCompletedEvent :
<code>@Bean
public ProcessRuntimeEventListener<ProcessCompletedEvent> processCompletedEvent() {
return processCompletedEvent -> {
ProcessInstance instance = processCompletedEvent.getEntity();
logger.info("任务发起人:{}", instance.getInitiator());
};
}
</code>This listener is invoked when a process finishes, logging the initiator of the workflow.
Note: If you start the process using the previous code, the initiator may be null . To set it correctly, modify the service method that starts the process:
<code>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());
}
</code>Here, Authentication.setAuthenticatedUserId sets the user who initiates the task, which is later available in the process completion listener.
After the workflow finishes, you can verify the data in the act_hi_procinst table, which now contains values for the completed process.
All listeners work as expected, and the workflow completes successfully.
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.