flowable流程启动时监听器

一、核心配置类

package com.magus.project.flow.config;

import com.google.common.collect.Maps;
import com.magus.project.flow.listener.ProcessStartedListener;
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;

/**
 * @Description flowable全局监听器配置类
 * @author: lxk
 * @Date 2020年6月17日14:44:33
 */
@Configuration
public class FlowableListenerConfig {

    /**
     * 任务节点前置监听
     * flowable监听级别参照 {@link FlowableEngineEventType} org.flowable.common.engine.api.delegate.event
     */
    private static final String CUSTOMER_LISTENER_PROCESS_STARTED = "PROCESS_STARTED";

    /**
     * 任务节点前置监听
     * 自己建立监听类实现FlowableEventListener接口
     */
    private final ProcessStartedListener taskBeforeListener;


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

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

    /**
     * 监听类配置 {@link FlowableEngineEventType} flowable监听器级别
     *
     * @param
     * @return java.util.Map>
     */
    private Map> customFlowableListeners() {
        Map> listenerMap = Maps.newHashMap();
        listenerMap.put(CUSTOMER_LISTENER_PROCESS_STARTED, new ArrayList<>(Collections.singletonList(taskBeforeListener)));
        return listenerMap;
    }

}
二、监听器

package com.magus.project.flow.listener;

import com.magus.framework.core.springbean.SpringContextUtils;
import com.magus.project.flow.constants.Constant;
import com.magus.project.flow.webBean.FormRelevant;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.impl.FlowableProcessEventImpl;
import org.springframework.stereotype.Component;

/**
 * 流程实例开始,监听处理类
 * @author: lxk
 * @create: 2020年6月17日14:47:52
 **/
@Slf4j
@Component
public class ProcessStartedListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent event) {
        log.error("----------前置监听器{},执行开始----------");
        FlowableProcessEventImpl eventImpl = (FlowableProcessEventImpl) event;
        RuntimeService runtimeService = SpringContextUtils.getBean(RuntimeService.class);
        runtimeService.setVariable(eventImpl.getExecutionId(), Constant.PROCESS_FORM_SKIP_EBABLE, true);

        //获取流程启动是设置的变量对象
        FormRelevant formRelevant = (FormRelevant) runtimeService.getVariable(eventImpl.getExecutionId(), Constant.PROCESS_FORM_RELEVANT_KEY);
       
        log.error("----------前置监听器{},执行结束----------");
    }

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

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

    @Override
    public String getOnTransaction() {
        return null;
    }
}

 

posted @ 2020-08-01 09:29  红尘沙漏  阅读(5271)  评论(0编辑  收藏  举报