add 添加引擎调度监听

This commit is contained in:
gssong 2023-08-06 21:01:19 +08:00
parent dc3c86f0d7
commit bfe0423371
2 changed files with 95 additions and 0 deletions

View File

@ -3,8 +3,12 @@ package org.dromara.workflow.flowable.config;
import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.flowable.spring.SpringProcessEngineConfiguration; import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer; import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.Collections;
/** /**
* flowable配置 * flowable配置
* *
@ -13,8 +17,11 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> { public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
@Autowired
private GlobalFlowableListener globalFlowableListener;
@Override @Override
public void configure(SpringProcessEngineConfiguration processEngineConfiguration) { public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
processEngineConfiguration.setIdGenerator(IdWorker::getIdStr); processEngineConfiguration.setIdGenerator(IdWorker::getIdStr);
processEngineConfiguration.setEventListeners(Collections.singletonList(globalFlowableListener));
} }
} }

View File

@ -0,0 +1,88 @@
package org.dromara.workflow.flowable.config;
import cn.hutool.core.collection.CollUtil;
import org.dromara.workflow.common.enums.TaskStatusEnum;
import org.flowable.bpmn.model.*;
import org.flowable.common.engine.api.delegate.event.*;
import org.flowable.common.engine.impl.cfg.TransactionState;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 引擎调度监听
*
* @author may
*/
@Component
public class GlobalFlowableListener implements FlowableEventListener {
@Autowired
@Lazy
private TaskService taskService;
@Autowired
@Lazy
private RuntimeService runtimeService;
@Autowired
@Lazy
private RepositoryService repositoryService;
@Override
public void onEvent(FlowableEvent flowableEvent) {
if (flowableEvent instanceof FlowableEngineEvent flowableEngineEvent) {
FlowableEngineEventType engineEventType = (FlowableEngineEventType) flowableEvent.getType();
switch (engineEventType) {
case JOB_EXECUTION_SUCCESS:
jobExecutionSuccess((FlowableEngineEntityEvent) flowableEngineEvent);
break;
default:
break;
}
}
}
@Override
public boolean isFailOnException() {
return true;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return TransactionState.COMMITTED.name();
}
protected void jobExecutionSuccess(FlowableEngineEntityEvent event) {
Execution execution = runtimeService.createExecutionQuery().executionId(event.getExecutionId()).singleResult();
BpmnModel bpmnModel = repositoryService.getBpmnModel(event.getProcessDefinitionId());
FlowElement flowElement = bpmnModel.getFlowElement(execution.getActivityId());
if (flowElement instanceof BoundaryEvent) {
String attachedToRefId = ((BoundaryEvent) flowElement).getAttachedToRefId();
List<Execution> list = runtimeService.createExecutionQuery().activityId(attachedToRefId).list();
for (Execution ex : list) {
Task task = taskService.createTaskQuery().executionId(ex.getId()).singleResult();
if (task != null) {
List<Comment> taskComments = taskService.getTaskComments(task.getId());
if (CollUtil.isEmpty(taskComments)) {
taskService.addComment(task.getId(), task.getProcessInstanceId(), TaskStatusEnum.PASS.getStatus(), "自动审批!");
}
}
}
}
}
}