背景
flowable-ui中可以通过xml对用户任务的任务监听器进行定义,包括任务指派、任务创建、任务完成等状态变化的时候,触发相应事件,相应监听器会执行相关代码。
在需求中,需要自定义事件,,然后通过按钮触发的方式主动触发,执行相应的监听器逻辑。
逻辑
通过查看flowable的源码,发现监听器的触发是通过该类实现的(org.flowable.engine.impl.bpmn.listener.ListenerNotificationHelper) ,所以只参考方法逻辑,进行调用即可。
参照flowable中方法执行的命令模式,自定义cmd类,注入相应参数执行即可
具体代码
任务事件触发执行类
package com.zxy.manage.flow.cmd;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.cmd.NeedsActiveTaskCmd;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
/**
* @date 2023/5/9 17:21
* 任务事件触发cmd
* 用于手动触发任务的相关事件,包括:
* 1、催办事件
*/
public class TaskEventNotifyCmd extends NeedsActiveTaskCmd<Void> {
protected String eventType;
private IOperationLogService operationLogService;
public TaskEventNotifyCmd(String taskId, String eventType, IOperationLogService operationLogService) {
super(taskId);
this.eventType = eventType;
this.operationLogService = operationLogService;
}
@Override
protected Void execute(CommandContext commandContext, TaskEntity task) {
ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration();
processEngineConfiguration.getListenerNotificationHelper().executeTaskListeners(task, eventType);
if (FlowConstant.TASK_EVENT_URGE_TASK.equals(eventType)) {
// 记录操作日志
operationLogService.saveTaskLog(task, OperationLogEnum.TASK_URGE);
}
return null;
}
}
cmd执行方法
@Autowired
private ManagementService managementService;
public void taskEventNotify(String taskId, String eventType) {
TaskEventNotifyCmd taskEventNotifyCmd = new TaskEventNotifyCmd(taskId, eventType, operationLogService);
managementService.executeCommand(taskEventNotifyCmd);
}
联系作者
如果有任何疑问,请邮件联系作者。
zhuxuanyong@163.com