Activiti监听器的使用

监听器是发生对应任务相关事件时执行自定义的java逻辑或表达式
在使用Activiti时,通常是跟业务结合,而有些业务会比较的复杂,会出现以下的场景:

  • activiti人员的动态分配
  • 当前任务节点完成时,需要指定下一个节点任务的执行人
  • 任务节点完成时需要一些复杂的业务处理
  • 任务到达某一节点时,需要监控当前任务的以下信息或者日志
  • 当前任务执行人处理人物的时候,需要触发自定义的一些业务处理
  • 流程的开始和结束也有可能对应相关的业务处理
  • 不仅是节点触发业务,在连线上也可以自定义业务
    那么是怎么实现这些需求的呢?为了满足我们的业务,activiti提供了监听器,下面详细讲解activiti监听器的使用。
    任务相应时间包括:



    create:任务创建后触发
    assignment:任务分配后触发
    delete:任务完成后触发
    all:所有事件发生都触发
    表达式参考上篇的UEL表达式,这里主要是介绍监听器的使用


    image.png

在class中指定我们的代码中的监听器类

从activiti监听器的使用范围,可以分为三种:

  1. 全局的监听器
  2. 连线的监听器
  3. 节点的监听器

全局监听

全局监听可以监听流程启动,任务执行节点之间的连线和流程结束,
自定义实现全局监听的类需要实现ExecutionListener 接口,ExecutionListener 里定义的常量start用来判断流程的开始,end是用来判断流程的结束,take时判断节点任务之间连线使用,也就是上一个节点完成任务后可以触发。

public interface ExecutionListener extends Serializable {
    String EVENTNAME_START = "start";
    String EVENTNAME_END = "end";
    String EVENTNAME_TAKE = "take";

    void notify(DelegateExecution var1) throws Exception;
}

演示

流程图


image.png

流程定义的xml文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1596619898726" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
<process id="stuKcsqServiceExemptionApply" isClosed="false" isExecutable="true" name="申请" processType="None">
<startEvent id="startevent1" name="Start"/>
<userTask activiti:assignee="$ {name}" activiti:exclusive="true" id="apply" name="提交申请"/>//
<userTask activiti:candidateUsers="${js}" activiti:exclusive="true" id="jsCheckService" name="教师审核"/>
<userTask activiti:candidateUsers="${wm}" activiti:exclusive="true" id="wmCheckAdmin" name="秘书审核">
<extensionElements>
<activiti:taskListener event="create"/>
</extensionElements>
</userTask>
<endEvent id="endevent1" name="End">
<extensionElements>
<activiti:executionListener class="com.graduate.platform.cultivate.listener.stuExemptionApplyListener" event="end"/>
</extensionElements>
</endEvent>
<userTask activiti:candidateUsers="${yjs}" activiti:exclusive="true" id="yjsCheckAdmin" name="培养办审核"/>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="apply"/>
<sequenceFlow id="flow2" sourceRef="apply" targetRef="jsCheckService">
<extensionElements>
<activiti:executionListener class="com.graduate.platform.cultivate.listener.stuExemptionApplyListener" event="take"/>
</extensionElements>
</sequenceFlow>
<sequenceFlow id="flow3" name="通过" sourceRef="jsCheckService" targetRef="wmCheckAdmin">
<extensionElements>
<activiti:executionListener class="com.graduate.platform.cultivate.listener.stuExemptionApplyListener" event="take"/>
</extensionElements>
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${state==1}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4" name="通过" sourceRef="wmCheckAdmin" targetRef="yjsCheckAdmin">
<extensionElements>
<activiti:executionListener class="com.graduate.platform.cultivate.listener.stuExemptionApplyListener" event="take"/>
</extensionElements>
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${state==1}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow5" sourceRef="yjsCheckAdmin" targetRef="endevent1"/><sequenceFlow id="flow6" name="不通过" sourceRef="jsCheckService" targetRef="endevent1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${state==0}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow7" name="不通过" sourceRef="wmCheckAdmin" targetRef="endevent1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${state==0}]]></conditionExpression>
</sequenceFlow>

自定义监听器类

上面的流程定义中,我们在在结束跟每条连线上(除了不同意的分支,因为不同意分支流程会直接到达结束)设置了我们自定义的监听器类,自定义的监听器类负责的业务有,当前一个节点完成任务时监听器会根据“take”跟“end”来判断节点的位置,当连线处被触发时也就是当前任务节点完成了,可以动态的设置下一个节点任务的执行人,在结束节点处判断state状态来处理业务申请是否通过。

public class stuExemptionApplyListener implements TaskListener, ExecutionListener {

    //操作数据库需要
    ActivityService activityService = (ActivityService) ApplicationContextProvider.getBean("activityService");

    @Override
    public void notify(DelegateTask delegateTask) {
    }

    @Override
    public void notify(DelegateExecution execution) throws Exception {
        // TODO Auto-generated method stub
        String eventName = execution.getEventName();
        //申请人学号
        String xh = execution.getVariable("name").toString();
        String lcId = execution.getProcessInstanceId();
        String names="";
        String teacherId="";
        if ("take".equals(eventName)) {
            StudentInfo studentInfo  = studentInfoService.selectInfoByXh(xh);
            //获取部门id
            String deptId=studentInfo.getYxsh();
            if(execution.getCurrentActivityId().equals("apply")){
                String teacherGh= execution.getVariable("js").toString();
                execution.setVariable(ActivityConstants.EXEMPTION_TEACHER, "admin,"+teacherGh);
            }
            if(execution.getCurrentActivityId().equals("jsCheckService")){
                teacherId= Constants.SECRETARY;
                //根据部门查询对应学院秘书用户 拼接用户名称和所属角色
                names = activityService.selectUsersByRole(teacherId, deptId);
                execution.setVariable("wm", names+",admin");
            }else if(execution.getCurrentActivityId().equals("wmCheckAdmin")){
                teacherId=Constants.CULTIVATEOFFICE;
                //培养办 拼接用户名称和所属角色
                names = activityService.selectUsersByRole(teacherId, "");
                execution.setVariable("yjs", names+",admin");//添加候选人,admin也可以做审核
            }
        }  else  if ("end".equals(eventName)) {
            String s=execution.getVariable("state")+"";
            int state = Integer.parseInt( s);
            StudentApplyService pyXskcsqService = (StudentApplyService ) ApplicationContextProvider.getBean("studentApplyService ");
            StudentApply studentApply =new StudentApply (lcId,state);
            //更新审核状态
            studentApplyService.updateByLcid(studentApply );
        }
    }

}

下面我们来分析一下DelegateExecution这个对象,因为上面的监听器类实现的方法中可以拿到DelegateExecution这个对象,那现在看看这个对象为我们提供了什么。

public interface DelegateExecution extends VariableScope {
    //流程id
    String getId();
    //流程实例id
    String getProcessInstanceId();
    // 用来获取 start、end、take
    String getEventName();
    // 获取业务id  不建议使用了 废弃
    /** @deprecated */
    String getBusinessKey();
    //获取业务id
    String getProcessBusinessKey();
    //获取流程定义的id
    String getProcessDefinitionId();
    // 获取父id,听说是并发的时候有用
    String getParentId();
    //获取 调用执行的id。
    String getSuperExecutionId();
    // 获取当前 ActivityId
    String getCurrentActivityId();
    // 获取 当前的 ActivityName
    String getCurrentActivityName();
    // 获取 TenantId 有多个 tenant 有用
    String getTenantId();
    //这个就不用说了,可以获取流程的所有核心service
    EngineServices getEngineServices();
}

节点监听器

自定义节点监听器类需要实现TaskListener接口,它跟全局监听器一样,定义了不同的监听事件,分别为:

  • create:任务被创建且所有的属性被设置好后可触发
  • assignment:当任务设置执行人后触发(注意 当流程到达一个任务节点时,会先触发assignment事件再触发create时间)
  • complete:在任务完成后,且在数据库中的运行时数据相关表删除数据之前
  • delete:在任务将要被删除之前发生。一般是completeTask完成任务时,它也会被执行
  • all:以上的事件都可以触发
public interface TaskListener extends Serializable {
    String EVENTNAME_CREATE = "create";
    String EVENTNAME_ASSIGNMENT = "assignment";
    String EVENTNAME_COMPLETE = "complete";
    String EVENTNAME_DELETE = "delete";
    String EVENTNAME_ALL_EVENTS = "all";

    void notify(DelegateTask var1);
}

使用方式跟全局监听器一样,这里就不多做代码演示,在节点监听器也可以动态的分配下一个任务的执行人,可以在任务的各种状态下处理系统的业务逻辑。
分析一下DelegateTask对象

public interface DelegateTask extends VariableScope {
  //数据库中的taskId主键
  String getId();
  
  // 任务名称 
  String getName();
  
  //修改任务名称 
  void setName(String name);
 
  //获取任务的描述信息
  String getDescription();
  
  //修改任务的描述信息
  void setDescription(String description);
  
  // lower priority: [0..19] lowest, [20..39] low, [40..59] normal, [60..79] high
  // [80..100] highest
  //任务处理的优先级范围是0-100
  int getPriority();
  
  //修改优先级
  void setPriority(int priority);
  
  // 获取流程实例id 
  String getProcessInstanceId();
  
  //获取执行id
  String getExecutionId();
  
  // 获取流程定义id
  String getProcessDefinitionId();
  // Adds the given user as a candidate user to this task.添加候选人
  void addCandidateUser(String userId);
  
  // 添加多个候选人
  void addCandidateUsers(Collection<String> candidateUsers);
  
  //添加候选组
  void addCandidateGroup(String groupId);
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354