Activiti监听器

文章目录

  • 学习链接
  • 任务监听器 TaskListener
    • 监听器监听的事件
    • 监听器委托类DelegateTask
    • 任务监听实现方式 — 类class
      • 绘制流程图
      • 自定义任务监听器
        • SiteReportUserTaskListener
      • 测试
    • 监听实现方式 — 表达式expression
      • 绘制流程图
      • 自定义 TaskListenerExpression
      • 测试
      • spring表达式
  • 执行监听器 ExecutionListener
  • 工作流程事件监听 ActivitiEventListener

学习链接

Activiti深入研究 - 专栏

  • 5.2 activiti任务监听器TaskListener
  • 5.1 activiti执行监听器ExecutionListener
  • 5.3 activiti工作流程事件监听ActivitiEventListener

Activiti7工作流从入门到实战(全网最好的)

  • Activiti7工作流引擎:基础篇(六) 任务监听器和流程监听器

Activiti

  • activiti学习(五)——执行监听器与任务监听器的基本使用
  • activiti学习(十一)——全局事件监听器的基本使用及其原理

程序员一灯-activiti监听器

任务监听器 TaskListener

任务监听器用于在特定的任务相关事件发生时,执行自定义的Java逻辑或表达式

  • 任务监听器是处理业务逻辑的重要的地方,当任务创建、设定负责人、完成任务时都可以监听的到,从而来处理自己的业务
  • 常用于监听Assignment事件,设置完负责人给负责人发一个消息来通知提示。注意:任务监听器只能用在UserTask上使用。

监听器监听的事件

  • String EVENTNAME_CREATE = “create”;创建):当任务已经创建,并且所有任务参数都已经设置时触发

  • String EVENTNAME_ASSIGNMENT = “assignment”;(指派):当任务已经指派给某人时触发。请注意:当流程执行到达用户任务时,create事件触发前,首先触发assignment事件。这看起来不是自然顺序,但是有实际原因的:当收到create事件时,我们通常希望查看任务的所有参数,包括办理人。

  • String EVENTNAME_COMPLETE = “complete”(完成):当任务已经完成,从运行时数据中删除前触发。

  • String EVENTNAME_DELETE = “delete”(删除):在任务即将被删除前触发。请注意当任务通过completeTask正常完成时也会触发

注意:assignment事件比create先执行。

监听器委托类DelegateTask

我们在监听方法中,能够拿到DelegateTask对象,因此,我们要熟悉这个对象的相关方法

package org.activiti.engine.delegate;
 
import java.util.Collection;
import java.util.Date;
import java.util.Set;
 
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.task.DelegationState;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.IdentityLinkType;
 
/**
 * @author Joram Barrez
 */
public interface DelegateTask extends VariableScope {
 
  /** DB id of the task. */
  String getId();
  
  /** Name or title of the task. */
  String getName();
  
  /** Change the name of the task. */
  void setName(String name);
 
  /** Free text description of the task. */
  String getDescription();
  
  /** Change the description of the task */
  void setDescription(String description);
  
  /** indication of how important/urgent this task is with a number between 
   * 0 and 100 where higher values mean a higher priority and lower values mean 
   * lower priority: [0..19] lowest, [20..39] low, [40..59] normal, [60..79] high 
   * [80..100] highest */
  int getPriority();
  
  /** indication of how important/urgent this task is with a number between 
   * 0 and 100 where higher values mean a higher priority and lower values mean 
   * lower priority: [0..19] lowest, [20..39] low, [40..59] normal, [60..79] high 
   * [80..100] highest */
  void setPriority(int priority);
  
  /** Reference to the process instance or null if it is not related to a process instance. */
  String getProcessInstanceId();
  
  /** Reference to the path of execution or null if it is not related to a process instance. */
  String getExecutionId();
  
  /** Reference to the process definition or null if it is not related to a process. */
  String getProcessDefinitionId();
 
  /** The date/time when this task was created */
  Date getCreateTime();
  
  /** The id of the activity in the process defining this task or null if this is not related to a process */
  String getTaskDefinitionKey();
 
  /** Indicated whether this task is suspended or not. */
  boolean isSuspended();
 
  /** The tenant identifier of this task */
  String getTenantId();
 
  /** The form key for the user task */
  String getFormKey();
 
  /** Change the form key of the task */
  void setFormKey(String formKey);
  
  /** Returns the execution currently at the task. */
  DelegateExecution getExecution();
  
  /** Returns the event name which triggered the task listener to fire for this task. */
  String getEventName();
 
  /** The current {@link org.activiti.engine.task.DelegationState} for this task. */
  DelegationState getDelegationState();
  
  /** Adds the given user as a candidate user to this task. */
  void addCandidateUser(String userId);
  
  /** Adds multiple users as candidate user to this task. */
  void addCandidateUsers(Collection<String> candidateUsers);
  
  /** Adds the given group as candidate group to this task */
  void addCandidateGroup(String groupId);
  
  /** Adds multiple groups as candidate group to this task. */
  void addCandidateGroups(Collection<String> candidateGroups);
 
  /** The {@link User.getId() userId} of the person responsible for this task. */
  String getOwner();
  
  /** The {@link User.getId() userId} of the person responsible for this task.*/
  void setOwner(String owner);
  
  /** The {@link User.getId() userId} of the person to which this task is delegated. */
  String getAssignee();
  
  /** The {@link User.getId() userId} of the person to which this task is delegated. */
  void setAssignee(String assignee);
  
  /** Due date of the task. */
  Date getDueDate();
  
  /** Change due date of the task. */
  void setDueDate(Date dueDate);
  
  /** The category of the task. This is an optional field and allows to 'tag' tasks as belonging to a certain category. */
  String getCategory();
	
  /** Change the category of the task. This is an optional field and allows to 'tag' tasks as belonging to a certain category. */
  void setCategory(String category);
  
  /**
   * Involves a user with a task. The type of identity link is defined by the given identityLinkType.
   * @param userId id of the user involve, cannot be null.
   * @param identityLinkType type of identityLink, cannot be null (@see {@link IdentityLinkType}).
   * @throws ActivitiObjectNotFoundException when the task or user doesn't exist.
   */
  void addUserIdentityLink(String userId, String identityLinkType);
  
  /**
   * Involves a group with group task. The type of identityLink is defined by the given identityLink.
   * @param groupId id of the group to involve, cannot be null.
   * @param identityLinkType type of identity, cannot be null (@see {@link IdentityLinkType}).
   * @throws ActivitiObjectNotFoundException when the task or group doesn't exist.
   */
  void addGroupIdentityLink(String groupId, String identityLinkType);
  
  /**
   * Convenience shorthand for {@link #deleteUserIdentityLink(String, String)}; with type {@link IdentityLinkType#CANDIDATE}
   * @param userId id of the user to use as candidate, cannot be null.
   * @throws ActivitiObjectNotFoundException when the task or user doesn't exist.
   */
  void deleteCandidateUser(String userId);
  
  /**
   * Convenience shorthand for {@link #deleteGroupIdentityLink(String, String, String)}; with type {@link IdentityLinkType#CANDIDATE}
   * @param groupId id of the group to use as candidate, cannot be null.
   * @throws ActivitiObjectNotFoundException when the task or group doesn't exist.
   */
  void deleteCandidateGroup(String groupId);
  
  /**
   * Removes the association between a user and a task for the given identityLinkType.
   * @param userId id of the user involve, cannot be null.
   * @param identityLinkType type of identityLink, cannot be null (@see {@link IdentityLinkType}).
   * @throws ActivitiObjectNotFoundException when the task or user doesn't exist.
   */
  void deleteUserIdentityLink(String userId, String identityLinkType);
  
  /**
   * Removes the association between a group and a task for the given identityLinkType.
   * @param groupId id of the group to involve, cannot be null.
   * @param identityLinkType type of identity, cannot be null (@see {@link IdentityLinkType}).
   * @throws ActivitiObjectNotFoundException when the task or group doesn't exist.
   */
  void deleteGroupIdentityLink(String groupId, String identityLinkType);
 
  /**
   * Retrieves the candidate users and groups associated with the task.
   * @return set of {@link IdentityLink}s of type {@link IdentityLinkType#CANDIDATE}.
   */
  Set<IdentityLink> getCandidates();
}

任务监听实现方式 — 类class

绘制流程图

Activiti监听器_第1张图片

给经理审批节点设置如下任务监听器

Activiti监听器_第2张图片

自定义任务监听器

SiteReportUserTaskListener
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

/**
 * 任务监听器用于在特定的任务相关事件发生时,执行自定义的Java逻辑或表达式
 *
 * 任务监听器支持下列属性:
 *  event(事件)(必填):任务监听器将被调用的任务事件类型。可用的事件有:
 *         create(创建):当任务已经创建,并且所有任务参数都已经设置时触发。
 *         assignment(指派):当任务已经指派给某人时触发。请注意:当流程执行到达用户任务时,create事件触发前,首先触发
 *         assignment事件。这看起来不是自然顺序,但是有实际原因的:当收到create事件时,我们通常希望查看任务的所有参数,包括
 *         办理人。
 *         complete(完成):当任务已经完成,从运行时数据中删除前触发。
 *         delete(删除):在任务即将被删除前触发。请注意当任务通过completeTask正常完成时也会触发
 *
 *   class:需要调用的代理类。这个类必须实现 org.activiti.engine.delegate.TaskListener 接口
 *
 *
 *   expression:(不能与class属性一起使用):指定在事件发生时要执行的表达式。可以为被调用的对象传递 DelegateTask 对象与事件名(使用 task.eventName )作为参数
 *
 *
 *
 *   delegateExpression:可以指定一个能够解析为 TaskListener 接口实现类对象的表达式。与服务任务类似
 *
 *
 */
@Slf4j
public class SiteReportUserTaskListener implements TaskListener {

    /*
      启动流程时候(按顺序)
        收到事件通知: assignment
        收到事件通知: create
      完成经理审批任务时候(按顺序)
        收到事件通知: complete
        收到事件通知: delete
    */
    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("收到事件通知: {}", delegateTask.getEventName());
    }


}

测试

  1. 先部署该流程
  2. 然后,发起1个流程时,它会收到assignment、create
  3. 然后,部门经理完成该任务,它会收到complete、delete

监听实现方式 — 表达式expression

使用activiti:taskListener元素的expression属性来指定监听器

绘制流程图

Activiti监听器_第3张图片

自定义 TaskListenerExpression

@Slf4j
public class TaskListenerExpression implements Serializable {

    public void execute(DelegateTask delegateTask) {
        log.info("收到事件通知: {}", delegateTask.getEventName());
    }

}

测试

  1. 先部署该流程

  2. 然后,发起1个流程时,注意发起流程时,这里需要设置taskListenerExpression,然后它会收到assignment、create

    // 在流程执行到某个阶段,或者启动流程实例的时候,用下面代码调用
    HashMap<String, Object> variables = new HashMap<String, Object>();
    variables.put("taskListenerExpression", new TaskListenerExpression());
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("listener1", variables);
    
  3. 然后,部门经理完成该任务,它会收到complete、delete

spring表达式

在上面,我们在开启流程时,自己new了1个TaskListenerExpression,并且把它放入了流程变量中。在spring中,我们只需要将此bean定义在spring容器中即可,在启动流程时,不需要把它放入流程变量中,就可以启动流程了(注意:一定要把这个bean定义在容器中,否则会报错)。

执行监听器 ExecutionListener

工作流程事件监听 ActivitiEventListener

你可能感兴趣的:(工作流,java,activiti)