簡介
場景:對歷史任務進行關注,所以需要修改流程歷史任務的本地變量
方法包含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, 變量名, 變量值);