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;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM