activiti全局事件监听

环境:springboot2+activiti6

全局监听类

package com.cqemme.activitiservice.config;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqemme.activitiservice.dao.SchoolProDao;
import com.cqemme.activitiservice.dao.SchoolTaskDao;
import com.cqemme.activitiservice.entity.SchoolProEntity;
import com.cqemme.activitiservice.entity.SchoolTaskEntity;
import com.cqemme.activitiservice.feign.EsFeign;
import com.cqemme.activitiservice.util.EmptyChecker;
import com.cqemme.comm.po.PutDocPo;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

@Component
public class ComActivitiEventListener implements ActivitiEventListener, Serializable {

    @Autowired
    private SchoolProDao schoolProDao;

    @Autowired
    private SchoolTaskDao schoolTaskDao;

    @Autowired
    private EsFeign esFeign;

    //activiti全局事件接收器
    @Override
    public void onEvent(ActivitiEvent event) {
        switch (event.getType()) {
            //流程开始
            case PROCESS_STARTED:
                proStart(event);
                break;
            //流程结束
            case PROCESS_COMPLETED:
                proOver(event);
                break;
            //任务开始
            case TASK_CREATED:
                taskCreate(event);
                break;
            //任务完成
            case TASK_COMPLETED:
                taskOver(event);
                break;
            //流程结束
            case PROCESS_CANCELLED:
                proOver(event);
            default:
        }
    }

    /**
     * 这个是必须要的,监听异常处理策略
     * @return
     */
    @Override
    public boolean isFailOnException() {
        return false;
    }

    /**
     * 流程开始做记录
     *
     * @param event
     */
    public void proStart(ActivitiEvent event) {
        //这个不能直接通过@Autowried注入,会报错找不到,可能本类成为bean的时候RuntimeService还没成为bean
        RuntimeService runtimeService = GetSpringBean.getBean(RuntimeService.class);
        RepositoryService repositoryService = GetSpringBean.getBean(RepositoryService.class);

        String instanceId = event.getProcessInstanceId();
        String schoolId = runtimeService.getVariable(instanceId, "schoolId").toString();
        String defineId = event.getProcessDefinitionId();

        ProcessDefinition process = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(defineId)
                .singleResult();
        String proName = process.getName();

        //组装流程启动数据
        SchoolTaskEntity schoolTaskEntity = new SchoolTaskEntity();
        schoolTaskEntity.setScProId(insertSchoolPro(schoolId,defineId));
        schoolTaskEntity.setName(proName);
        schoolTaskEntity.setTaskId(instanceId);
        schoolTaskEntity.setInstanceId(instanceId);
        schoolTaskEntity.setType(1);
        schoolTaskEntity.setStatus(2);
        schoolTaskEntity.setCreateTime(new Date());
        schoolTaskDao.insert(schoolTaskEntity);

        //存es引擎
        PutDocPo send = new PutDocPo();
        String docName=(schoolId + defineId).replaceAll(":","_");
        send.setDocName(docName);
        send.setJson(JSONObject.toJSONString(schoolTaskEntity));

        esFeign.putdocAdd(send);
    }

    /**
     * 流程结束更新状态
     *
     * @param event
     */
    public void proOver(ActivitiEvent event) {
        RuntimeService runtimeService = GetSpringBean.getBean(RuntimeService.class);

        String instanceId = event.getProcessInstanceId();
        String schoolId = runtimeService.getVariable(instanceId, "schoolId").toString();
        String defineId = event.getProcessDefinitionId();

        //更新流程为完成状态
        SchoolTaskEntity schoolTaskEntity = new SchoolTaskEntity();
        //es凭这两个字段去更新状态
        schoolTaskEntity.setTaskId(instanceId);
        schoolTaskEntity.setInstanceId(instanceId);
        schoolTaskEntity.setStatus(1);
        schoolTaskEntity.setOverTime(new Date());
        schoolTaskDao.update(schoolTaskEntity, new QueryWrapper<SchoolTaskEntity>()
                .eq("instance_id", instanceId)
                .eq("task_id", instanceId)
        );


        //存es引擎
        PutDocPo send = new PutDocPo();
        String docName=(schoolId + defineId).replaceAll(":","_");
        send.setDocName(docName);
        send.setJson(JSONObject.toJSONString(schoolTaskEntity));

        esFeign.putdocAdd(send);
    }

    /**
     * 任务创建做记录
     *
     * @param event
     */
    public void taskCreate(ActivitiEvent event) {
        RuntimeService runtimeService = GetSpringBean.getBean(RuntimeService.class);

        String instanceId = event.getProcessInstanceId();
        String schoolId = runtimeService.getVariable(instanceId, "schoolId").toString();
        String defineId = event.getProcessDefinitionId();
        ActivitiEntityEvent acEntiEv = (ActivitiEntityEvent) event;
        Map eventMap = JSONObject.parseObject(JSONObject.toJSONString(acEntiEv.getEntity()), Map.class);
        String name = eventMap.get("name").toString();
        String taskId = eventMap.get("id").toString();

        //封装任务数据
        SchoolTaskEntity schoolTaskEntity = new SchoolTaskEntity();
        schoolTaskEntity.setScProId(insertSchoolPro(schoolId,defineId));
        schoolTaskEntity.setName(name);
        schoolTaskEntity.setTaskId(taskId);
        schoolTaskEntity.setInstanceId(instanceId);
        schoolTaskEntity.setType(2);
        schoolTaskEntity.setStatus(2);
        schoolTaskEntity.setCreateTime(new Date());

        schoolTaskDao.insert(schoolTaskEntity);

        //存es引擎
        PutDocPo send = new PutDocPo();
        String docName=(schoolId + defineId + name).replaceAll(":","_");
        send.setDocName(docName);
        send.setJson(JSONObject.toJSONString(schoolTaskEntity));

        esFeign.putdocAdd(send);

    }

    /**
     * 任务完成做更新状态
     * @param event
     */
    public void taskOver(ActivitiEvent event){

        RuntimeService runtimeService = GetSpringBean.getBean(RuntimeService.class);

        String instanceId = event.getProcessInstanceId();
        String schoolId = runtimeService.getVariable(instanceId, "schoolId").toString();
        String defineId = event.getProcessDefinitionId();
        ActivitiEntityEvent acEntiEv = (ActivitiEntityEvent) event;
        Map eventMap = JSONObject.parseObject(JSONObject.toJSONString(acEntiEv.getEntity()), Map.class);
        String name = eventMap.get("name").toString();
        String taskId = eventMap.get("id").toString();


        //更新任务状态
        SchoolTaskEntity schoolTaskEntity = new SchoolTaskEntity();
        //es凭这两个字段去更新
        schoolTaskEntity.setTaskId(taskId);
        schoolTaskEntity.setInstanceId(instanceId);
        schoolTaskEntity.setStatus(1);
        schoolTaskEntity.setOverTime(new Date());

        schoolTaskDao.update(schoolTaskEntity,new QueryWrapper<SchoolTaskEntity>()
                .eq("instance_id", instanceId)
                .eq("task_id", taskId)
        );

        //存es引擎
        PutDocPo send = new PutDocPo();
        String docName=(schoolId + defineId + name).replaceAll(":","_");
        send.setDocName(docName);
        send.setJson(JSONObject.toJSONString(schoolTaskEntity));

        esFeign.putdocAdd(send);

    }

    /**
     * 插学校和流程图关联信息
     * @return
     */
    public int insertSchoolPro(String schoolId,String defineId){
        synchronized (ComActivitiEventListener.class) {
            //取学校和流程定义关联主键id
            SchoolProEntity schoolProOne = schoolProDao.selectOne(new QueryWrapper<SchoolProEntity>()
                    .eq("school_id", schoolId)
                    .eq("pro_define_id", defineId)
            );
            if (EmptyChecker.isEmpty(schoolProOne)) {
                //组装学校和流程定义数据
                SchoolProEntity schoolProEntity = new SchoolProEntity();
                schoolProEntity.setProDefineId(defineId);
                schoolProEntity.setSchoolId(schoolId);
                schoolProDao.insertOne(schoolProEntity);
                return schoolProEntity.getId();
            }
            return schoolProOne.getId();
        }
    }

}

注册监听类

package com.cqemme.activitiservice.config;


import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ActivitiConfig implements ProcessEngineConfigurationConfigurer {

    @Autowired
    private ComActivitiEventListener comActivitiEventListener;

    @Override
    public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {

        List<ActivitiEventListener> activitiEventListener=new ArrayList<ActivitiEventListener>();

        activitiEventListener.add(comActivitiEventListener);//配置全局监听器

        processEngineConfiguration.setEventListeners(activitiEventListener);

    }

}

普通类拿到容器bean

package com.cqemme.activitiservice.config;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class GetSpringBean implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}



上一篇:2020厦门大学845数据结构考研考试范围(大纲)和参考书目


下一篇:创建bpmn流程模型