有两种方案,一种不用监听器:
1、获取下一个任务节点是不是结束事件
/**
* 获取任务节点
*
* @param node 查询节点选择
* @param taskId 任务id
*/
public void nextFlowNode(String node, String taskId) {
Task task = processEngine().getTaskService().createTaskQuery().taskId(taskId).singleResult();
ExecutionEntity ee = (ExecutionEntity) processEngine().getRuntimeService().createExecutionQuery()
.executionId(task.getExecutionId()).singleResult();
// 当前审批节点
String crruentActivityId = ee.getActivityId();
BpmnModel bpmnModel = processEngine().getRepositoryService().getBpmnModel(task.getProcessDefinitionId());
FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(crruentActivityId);
// 输出连线
List<SequenceFlow> outFlows = flowNode.getOutgoingFlows();
for (SequenceFlow sequenceFlow : outFlows) {
//当前审批节点
if ("now".equals(node)) {
FlowElement sourceFlowElement = sequenceFlow.getSourceFlowElement();
System.out.println("当前节点: id=" + sourceFlowElement.getId() + ",name=" + sourceFlowElement.getName());
} else if ("next".equals(node)) {
// 下一个审批节点
FlowElement targetFlow = sequenceFlow.getTargetFlowElement();
if (targetFlow instanceof UserTask) {
System.out.println("下一节点: id=" + targetFlow.getId() + ",name=" + targetFlow.getName());
}
// 如果下个审批节点为结束节点
if (targetFlow instanceof EndEvent) {
System.out.println("下一节点为结束节点:id=" + targetFlow.getId() + ",name=" + targetFlow.getName());
}
}
}
}
---------------------
版权声明:本文为CSDN博主「FYRT」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36760953/article/details/91410038
2、第二种方法:实现监听器,生成监听器的bean类,写在model生成的xml文件中
监听器类:
@Component(value = "myListener")
public class TaskBusinessCallListener extends BusinessCallListener implements TaskListener {
/**
* dubbo的类名
*/
private FixedValue clazzName;
/**
* 方法名
*/
private FixedValue method;
/**
* 版本号
*/
private FixedValue version;
/**
* 参数 多个的话用分号隔开 实例 userCode:00004737;status:1
*/
private FixedValue params;
@Override
public void notify(DelegateTask delegateTask) {
String processInstanceId = delegateTask.getProcessInstanceId();
//执行回调
this.callBack(processInstanceId, clazzName.getExpressionText(), method.getExpressionText(), version.getExpressionText(), params.getExpressionText());
}
}
在xml文件中使用delegateExpression表达式
<userTask id="test" name="测试" flowable:assignee="${applyUserId}" flowable:formKey="testForm">
<extensionElements>
<!-- <flowable:taskListener event="create" class="cn.flowable.listener.MyListener"></flowable:taskListener> -->
<flowable:taskListener event="create" delegateExpression="${myListener}"></flowable:taskListener>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>
</userTask>
---------------------
版权声明:本文为CSDN博主「仰望星空的女孩」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011396199/article/details/76174466
上述方法实际上 不能实现流程结束后进行响应操作,这里的监听器应该加在结束事件上
3、那么有没有方法可以实现直接在bpmnModel上添加全局的结束事件监听器呢?
思路是 创建一个自定义的全局监听器,添加进flowable的监听器序列中去,然后在这个自定义的全局监听器的方法中判断当前的Event是不是EndEvent,如果是才进行相应的操作。
自定义全局监听器:
/**
* 任务节点前置监听处理类
* @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;
}
}
//引自:[https://www.jianshu.com/p/5d673e767356](https://www.jianshu.com/p/5d673e767356)
需要对这个全局监听器进行配置,纳入flowable监听
/**
* 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;
}
}
//引自:[https://www.jianshu.com/p/5d673e767356](https://www.jianshu.com/p/5d673e767356)
但是上述这个方法其实是有问题的,结束事件“执行”时,是不会调用这里的onEvent
方法的!最后一个userEvent执行完毕之后,这个流程就相当于自动结束了!
所以得在执行最后一个userEvent的时候就要进行处理,所以综合方法1和方法3,最终的全局监听器这么写:
/**
* 全局监听器,判断流程是不是运行到了最后一个EndEvent
* @author: YBY
* @create: 2019-05-04 20:51
**/
@Component
public class ProcessEndListener implements FlowableEventListener {
@Autowired
FlowableService flowableService;
@Override
public void onEvent(FlowableEvent event) {
// 当前节点任务实体,
TaskEntity taskEntity = (TaskEntity) ((FlowableEntityEventImpl) event).getEntity();
String taskId = taskEntity.getId();
String curActId = flowableService.getNodeId(taskId);
String procDefId = ProcUtils.getProcessDefinitionByTaskId(taskEntity.getId()).getId();
Process process = ProcessDefinitionUtil.getProcess(procDefId);
//遍历整个process,找到endEventId是什么,与当前taskId作对比
List<FlowElement> flowElements = (List<FlowElement>) process.getFlowElements();
for (FlowElement flowElement : flowElements) {
if (flowElement instanceof SequenceFlow) {
SequenceFlow flow = (SequenceFlow) flowElement;
FlowElement sourceFlowElement = flow.getSourceFlowElement();
FlowElement targetFlowElement = flow.getTargetFlowElement();
//如果当前边的下一个节点是endEvent,那么获取当前边
if(targetFlowElement instanceof EndEvent && sourceFlowElement.getId().equals(curActId))
{
System.out.println("下一个是结束节点!!");
}
}
}
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}
网友评论