更多ruoyi-nbcio功能请看演示系统
gitee源代码地址
演示地址:RuoYi-Nbcio后台管理系统
上一节说到待办系统的监听器TaskCreateListener,需要在flowable全局监听配置里加入配置
1、GlobalEventListenerConfig.java文件如下:
package com.ruoyi.flowable.config;
import com.ruoyi.flowable.listener.GlobalEventListener;
import com.ruoyi.flowable.listener.ProcessCompleteListener;
import com.ruoyi.flowable.listener.TaskCreateListener;
import lombok.AllArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.engine.RuntimeService;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* flowable全局监听配置
*
* @author ssc
*/
@Configuration
@AllArgsConstructor
public class GlobalEventListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
private final GlobalEventListener globalEventListener;
private final RuntimeService runtimeService;
private final SpringProcessEngineConfiguration configuration;
private final TaskCreateListener taskCreateListener;
private final ProcessCompleteListener processCompleteListener;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
// 任务创建全局监听-待办消息发送
dispatcher.addEventListener(taskCreateListener, FlowableEngineEventType.TASK_CREATED, FlowableEngineEventType.TASK_ASSIGNED);
//任务创建全局监听-完成消息发送
dispatcher.addEventListener(processCompleteListener, FlowableEngineEventType.PROCESS_COMPLETED);
// 流程正常结束
runtimeService.addEventListener(globalEventListener, FlowableEngineEventType.PROCESS_COMPLETED);
}
}
2、还增加了一个流程结束的通知监听如下:
package com.ruoyi.flowable.listener;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.task.api.Task;
import org.flowable.variable.api.persistence.entity.VariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.service.CommonService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import lombok.RequiredArgsConstructor;
/**
* 全局监听-工作流完成消息提醒
*
* @author nbacheng
*/
//必须要用 AbstractFlowableEngineEventListener 用FlowableEventListener这个会出现问题,应该是已经完成了
@Component
@RequiredArgsConstructor
public class ProcessCompleteListener extends AbstractFlowableEngineEventListener {
private final TaskService taskService;
@Resource
private CommonService commonService;
@Autowired
protected HistoryService historyService;
@Resource
protected RepositoryService repositoryService;
@Override
protected void processCompleted(FlowableEngineEntityEvent event) {
System.out.println("进入流程结束监听器……");
String procInsId = event.getProcessInstanceId();
HistoricProcessInstance hi = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(procInsId)
.singleResult();
List<Task> listtask = taskService.createTaskQuery().processInstanceId(procInsId).active().list();
String taskId = "";
if(listtask !=null) {
taskId = listtask.get(0).getId();
}
String startUserId = hi.getStartUserId();
String businessKey = hi.getBusinessKey();
String deployId = hi.getDeploymentId();
String category = "";
if (StringUtils.isNotEmpty(startUserId)) {
// TODO: 发送提醒消息
if(((ExecutionEntityImpl)event.getEntity()).getVariableInstances().get("category") !=null) {
category = ((VariableInstance)((ExecutionEntityImpl)event.getEntity()).getVariableInstances().get("category")).getTextValue();
}
LoginUser loginUser = commonService.getLoginUser();
String taskMessageUrl;
if(StringUtils.isNotBlank(businessKey)) {
taskMessageUrl = "<a href=" + commonService.getBaseUrl() + "?procInsId=" + procInsId + "&deployId="
+ deployId + "&taskId=" + taskId + "&businessKey=" + businessKey + "&category=" + category
+ "&finished=false" + ">点击这个进行查看</a>" ;
}
else {
taskMessageUrl = "<a href=" + commonService.getBaseUrl() + "?procInsId=" + procInsId + "&deployId="
+ deployId + "&taskId=" + taskId + "&businessKey" + "&category=" + category + "&finished=false" + ">点击这个进行查看</a>" ;
}
String msgContent = "流程任务结束通知" + taskMessageUrl;
commonService.sendSysNotice(loginUser.getUsername(), startUserId, "流程任务结束通知", msgContent, Constants.MSG_CATEGORY_1);//setMsgCategory=1是通知
}
super.processCompleted(event);
}
@Override
protected void taskCompleted(FlowableEngineEntityEvent event) {
System.out.println("进入taskCompleted监听器……");
super.taskCompleted(event);
}
@Override
public void onEvent(FlowableEvent flowableEvent) {
System.out.println("进入taskCompleted监听器--onEvent……");
super.onEvent(flowableEvent);
}
}
当然这两个监听还需要根据实际进行个性化定制与修改。