Flowable全局监听

3,289 阅读1分钟

前言:系统使用Flowable工作流引擎,最近项目有个需求:当有用户将单据提交/办理/退回审批时,要通知提醒下一个审核人。

解决:

1. 新增Listener 实现 FlowableEventListener,在OnEvent方法里写业务逻辑

package com.hgsoft.listener;
import com.hgsoft.wx.service.WxService;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.cfg.TransactionState;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Set;

@Component
public class TaskBeforeListener implements FlowableEventListener {


    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        TaskEntity taskEntity = (TaskEntity) ((FlowableEntityEventImpl) flowableEvent).getEntity();
        // TODO 获取到了taskEntity 自己做每个节点的前置操作
        Set<IdentityLink> candidates = taskEntity.getCandidates();//获取审批候选人
        String currentProcess = taskEntity.getName(); //流程名称
        for (IdentityLink candidate : candidates) {
            // TODO 执行业务逻辑:系统通知该候选人
        }
        //如果上面的候选组没人,就执行下面这句。指定通知这个人
        if(candidates.size()==0){
             // TODO 执行业务逻辑:系统通知指定分配人员
        }

    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        //事务提交后触发
        return TransactionState.COMMITTED.name();
    }
}

2.新增全局监听配置类FlowableListenerConfig

package com.hgsoft.listener;

import com.google.common.collect.Maps;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

// 全局监听配置类
@Configuration
public class FlowableListenerConfig {

    /**
     * 任务节点前置监听
     */
    private static final String FLOWABLE_TASK_NODE_BEFORE_LISTENER  = "TASK_CREATED";

    /**
     * 自己建立监听类实现FlowableEventListener接口
     */
    private final TaskBeforeListener taskBeforeListener;


    @Autowired
    public FlowableListenerConfig(TaskBeforeListener taskBeforeListener) {
        this.taskBeforeListener = taskBeforeListener;
    }

    /**
     * 将自定义监听器纳入flowable监听
     */
    @Bean
    public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> globalListenerConfigurer () {
        return engineConfiguration -> {
            engineConfiguration.setTypedEventListeners(this.customFlowableListeners());
        };
    }

    /**
     * 监听类配置 {@link FlowableEngineEventType} flowable监听器级别
     */
    private Map<String, List<FlowableEventListener>> customFlowableListeners () {
        Map<String, List<FlowableEventListener>> listenerMap = Maps.newHashMap();
        listenerMap.put(FLOWABLE_TASK_NODE_BEFORE_LISTENER, new ArrayList<>(Collections.singletonList(getTaskBeforeListener())));
        return listenerMap;
    }

    public TaskBeforeListener getTaskBeforeListener() {
        return taskBeforeListener;
    }
}