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<org.flowable.spring.SpringProcessEngineConfiguration>
     */
    @Bean
    public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> globalListenerConfigurer() {
        return engineConfiguration -> {
            engineConfiguration.setTypedEventListeners(this.customFlowableListeners());
        };
    }

    /**
     * 监听类配置 {@link FlowableEngineEventType} flowable监听器级别
     *
     * @param
     * @return java.util.Map<java.lang.String, java.util.List < org.flowable.common.engine.api.delegate.event.FlowableEventListener>>
     */
    private Map<String, List<FlowableEventListener>> customFlowableListeners() {
        Map<String, List<FlowableEventListener>> 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;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值