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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Implement Activiti Event Listeners in Spring Boot: Task and Process Events

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:

@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());
    };
  }
}

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:

@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());
  };
}

Resulting console output after completing the task confirms the listener execution.

ProcessRuntimeEventListener for process events, e.g., ProcessCompletedEvent :

@Bean
public ProcessRuntimeEventListener<ProcessCompletedEvent> processCompletedEvent() {
  return processCompletedEvent -> {
    ProcessInstance instance = processCompletedEvent.getEntity();
    logger.info("任务发起人:{}", instance.getInitiator());
  };
}

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:

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());
}

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.

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.

JavaworkflowSpring BootActivitievent-listener
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.