Activiti summary (3) listener

There are two types of Activiti listeners, TaskListeners and ExecutionListeners.

A, TaskListener

TaskListener is a TaskListener interface with a notify method. When setting the TaskListener, you can select four types of trigger events: create, assigin, delete, and all. When a task begins to execute an event, the code in the listener is automatically executed instead of being called manually.

When using Spring to integrate Activiti, it is important to note that the listener bean is not managed by Spring. To use the listener properly, the listener implementation class also needs to implement The JavaDelegate for Activiti. The code is as follows:

package com.oms.lisener;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.delegate.TaskListener;

import java.util.Map;

/** * Description: * Request for leave listener **@author Zhouyz
 * @createThe 2019-06-05 "* /

public class ApplyForRepairImpl implements JavaDelegate.TaskListener {

    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the maintenance request");
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        delegateTask.getOwner();
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" + delegateTask.getOwner() + "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- maintenance request -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- notify");
        Map varis = delegateTask.getVariables();
        // This is where the exception is handled
        delegateTask.setOwner(varis.get("owner").toString());
        delegateTask.setAssignee(varis.get("assignee").toString()); }}Copy the code

Second, the ExcutionListener

The use of an ExcutinerLister is similar to that of a TaskListener, but they listen for different events. An ExcutionListener listens for a change in the workflow state. There are three types of events that are triggered: start, end, or take. Implement the JavaDelegate interface as well when using it in Spring. The code is as follows:

package com.oms.lisener;

import org.activiti.engine.delegate.*;

/** * Description: * Leader approval task listener **@author Zhouyz
 * @createThe 2019-06-06 11:11 * /

public class LeaderCheck implements JavaDelegate.ExecutionListener {
    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("------------------do nothing--------------");
    }


    @Override
    public void notify(DelegateExecution delegateExecution) {
        System.out.println("----------- Process instance ID" + delegateExecution.getProcessInstanceId() + "-------------notify"); }}Copy the code

Use listeners

  • Add a Task or other structured listener

  • Select the listener implementation class that you just wrote

  • You’re done