在Flowable工作流开发中很常见问题每个任务节点都有相同的操作或者一个流程结束后需要做什么操作此时都可以用到全局监听 直接上代码!
// 全局监听配置类
package com.tlsq.service.workflow.config;
import com.google.common.collect.Maps;
import com.tlsq.service.workflow.monitor.SequenceListener;
import com.tlsq.service.workflow.monitor.TaskBeforeListener;
import com.tlsq.service.workflow.monitor.TaskRearListener;
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;
/**
* flowable 全局监听器配置类
* @author: Lu Yang
*/
@Configuration
public class FlowableListenerConfig {
// flowable监听级别参照 {@link FlowableEngineEventType} org.flowable.common.engine.api.delegate.event
/**
* 任务节点前置监听
*/
private static final String FLOWABLE_TASK_NODE_REAR_LISTENER = "TASK_COMPLETED";
// 自己建立监听类实现FlowableEventListener接口
/**
* 任务节点前置监听
*/
private final TaskBeforeListener taskBeforeListener;
@Autowired
public FlowableListenerConfig(TaskBeforeListener taskBeforeListener) {
this.taskBeforeListener = taskBeforeListener;
}
/**
* 将自定义监听器纳入flowable监听
* @author: Lu Yang
* @date: 2019/5/4 21:05
* @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监听器级别
* @author: Lu Yang
* @date: 2019/5/4 20:58
* @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(FLOWABLE_TASK_NODE_BEFORE_LISTENER, new ArrayList<>(Collections.singletonList(getTaskBeforeListener())));
}
public TaskBeforeListener getTaskBeforeListener() {
return taskBeforeListener;
}
}
package com.tlsq.service.workflow.monitor;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;
/**
* 任务节点前置监听处理类
* @author: Lu Yang
* @create: 2019-05-04 20:51
**/
@Component
public class TaskBeforeListener implements FlowableEventListener {
@Override
public void onEvent(FlowableEvent event) {
// 当前节点任务实体
TaskEntity taskEntity = (TaskEntity) ((FlowableEntityEventImpl) event).getEntity();
// TODO 获取到了taskEntity 自己做每个节点的前置操作
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}
网友评论