在Flowable工作流開(kāi)發(fā)中很常見(jiàn)問(wèn)題每個(gè)任務(wù)節(jié)點(diǎn)都有相同的操作或者一個(gè)流程結(jié)束后需要做什么操作此時(shí)都可以用到全局監(jiān)聽(tīng) 直接上代碼冠王!
// 全局監(jiān)聽(tīng)配置類
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 全局監(jiān)聽(tīng)器配置類
* @author: Lu Yang
*/
@Configuration
public class FlowableListenerConfig {
// flowable監(jiān)聽(tīng)級(jí)別參照 {@link FlowableEngineEventType} org.flowable.common.engine.api.delegate.event
/**
* 任務(wù)節(jié)點(diǎn)前置監(jiān)聽(tīng)
*/
private static final String FLOWABLE_TASK_NODE_REAR_LISTENER = "TASK_COMPLETED";
// 自己建立監(jiān)聽(tīng)類實(shí)現(xiàn)FlowableEventListener接口
/**
* 任務(wù)節(jié)點(diǎn)前置監(jiān)聽(tīng)
*/
private final TaskBeforeListener taskBeforeListener;
@Autowired
public FlowableListenerConfig(TaskBeforeListener taskBeforeListener) {
this.taskBeforeListener = taskBeforeListener;
}
/**
* 將自定義監(jiān)聽(tīng)器納入flowable監(jiān)聽(tīng)
* @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());
};
}
/**
* 監(jiān)聽(tīng)類配置 {@link FlowableEngineEventType} flowable監(jiān)聽(tīng)器級(jí)別
* @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;
/**
* 任務(wù)節(jié)點(diǎn)前置監(jiān)聽(tīng)處理類
* @author: Lu Yang
* @create: 2019-05-04 20:51
**/
@Component
public class TaskBeforeListener implements FlowableEventListener {
@Override
public void onEvent(FlowableEvent event) {
// 當(dāng)前節(jié)點(diǎn)任務(wù)實(shí)體
TaskEntity taskEntity = (TaskEntity) ((FlowableEntityEventImpl) event).getEntity();
// TODO 獲取到了taskEntity 自己做每個(gè)節(jié)點(diǎn)的前置操作
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}
https://gitee.com/cn-luyang/luyang-cloud.git
自己整了個(gè)項(xiàng)目体谒,期待小伙伴一起玩
歡迎大佬們提交PR
朋友們幫忙點(diǎn)個(gè)starter偶器,感謝了