简介
场景:对历史任务进行关注,所以需要修改流程历史任务的本地变量
方法包含2个类
1)核心方法,flowable command类:HistoricTaskSingleVariableUpdateCmd
2)执行command类:BpmProcessCommandService
然后springboot 执行方法即可: bpmProcessCommandService.executeCreateHistorySingleVariable(taskId, 变量名, 变量值);
注:@AllArgsConstructor 来自 lombok 注解,其中工具类来自 hutool;这里就不做过多介绍;
1 核心方法、 HistoricTaskSingleVariableUpdateCmd
实现 flowable command类
package com.flowable.core.cmd;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.HistoryService;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.flowable.variable.api.types.VariableType;
import org.flowable.variable.service.VariableServiceConfiguration;
import org.flowable.variable.service.impl.persistence.entity.HistoricVariableInstanceEntityImpl;
import org.flowable.variable.service.impl.persistence.entity.HistoricVariableInstanceEntityManager;
import org.flowable.variable.service.impl.persistence.entity.VariableInstanceEntityImpl;/*** 历史任务变量更新*/
@AllArgsConstructor
public class HistoricTaskSingleVariableUpdateCmd implements Command<Void> {// 任务编号protected String taskId;// 变量名protected String variableName;// 变量值protected Object variableValue;@Overridepublic Void execute(CommandContext commandContext) {// taskId 参数不能为空if (this.taskId == null) {throw new FlowableIllegalArgumentException("任务编号不能为空!");}// 1 获取运历史服务、历史变量管理器、变量管理器ProcessEngineConfigurationImpl procEngineConf = CommandContextUtil.getProcessEngineConfiguration(commandContext);HistoryService historyService = procEngineConf.getHistoryService();HistoricVariableInstanceEntityManager historicVariableInstanceEntityManager = procEngineConf.getVariableServiceConfiguration().getHistoricVariableInstanceEntityManager();VariableServiceConfiguration variableServiceConfiguration = procEngineConf.getVariableServiceConfiguration().getVariableServiceConfiguration();// 2 根据taskId查询历史任务HistoricTaskInstance historicTask = historyService.createHistoricTaskInstanceQuery().taskId(this.taskId).singleResult();// 3 查询是否存在历史变量是否存在HistoricVariableInstance hisVariable = historyService.createHistoricVariableInstanceQuery().processInstanceId(historicTask.getProcessInstanceId()).taskId(historicTask.getId()).variableName(variableName).singleResult();if (ObjectUtil.isNotNull(hisVariable)) {// 4 根据当前变量,修改历史变量的值HistoricVariableInstanceEntityImpl historicVariableImpl = (HistoricVariableInstanceEntityImpl) hisVariable;VariableInstanceEntityImpl variableInstanceEntity = createVariable(historicTask, variableServiceConfiguration);historicVariableInstanceEntityManager.copyVariableFields(historicVariableImpl, variableInstanceEntity, DateUtil.date());historicVariableInstanceEntityManager.update(historicVariableImpl);} else {// 5 新增历史变量VariableInstanceEntityImpl variableInstanceEntity = createVariable(historicTask, variableServiceConfiguration);historicVariableInstanceEntityManager.createAndInsert(variableInstanceEntity, DateUtil.date());}return null;}/*** 根据变量名 和变量值,构建变量** @param historicTask 历史人物* @param variableServiceConfiguration 变量管理器* @return 变量构造器*/private VariableInstanceEntityImpl createVariable(HistoricTaskInstance historicTask, VariableServiceConfiguration variableServiceConfiguration) {VariableInstanceEntityImpl variableInstanceEntity = new VariableInstanceEntityImpl();variableInstanceEntity.setName(variableName);variableInstanceEntity.setProcessInstanceId(historicTask.getProcessInstanceId());variableInstanceEntity.setExecutionId(historicTask.getExecutionId());variableInstanceEntity.setTaskId(historicTask.getId());VariableType variableType = variableServiceConfiguration.getVariableTypes().findVariableType(variableValue);variableInstanceEntity.setTypeName(variableType.getTypeName());variableInstanceEntity.setType(variableType);variableInstanceEntity.setValue(variableValue);return variableInstanceEntity;}
}
2 执行类方法 BpmProcessCommandService
执行command 需要注入 ManagementService
package com.flowable.core.service;import com.flowable.core.cmd.HistoricTaskSingleVariableUpdateCmd;
import jakarta.annotation.Resource;
import org.flowable.engine.ManagementService;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class BpmProcessCommandService {@Resourceprivate ManagementService managementService;/*** 执行更新任务历史变量** @param taskId 任务ID* @param variableName 变量名* @param variableValue 变量值*/public void executeCreateHistorySingleVariable(String taskId, String variableName, Object variableValue) {// 实例化创建历史流程变量 command 类HistoricTaskSingleVariableUpdateCmd historicTaskSingleVariableUpdateCmd = new HistoricTaskSingleVariableUpdateCmd(taskId, variableName, variableValue);// 通过 managementService 管理服务,执行创建历史流程变量 command 类managementService.executeCommand(historicTaskSingleVariableUpdateCmd);}
}
然后springboot 正常调用该方法即可
bpmProcessCommandService.executeCreateHistorySingleVariable(taskId, 变量名, 变量值);