Flowable源码注释(三十五)定时器激活流程定义处理器、流程实例迁移作业处理器

本文深入探讨Flowable引擎的源码,重点解析TimerActivateProcessDefinitionHandler如何处理定时器激活流程定义,以及AbstractProcessInstanceMigrationJobHandler和ProcessInstanceMigrationJobHandler在流程实例迁移作业中的作用。通过链接可以查看详细注释源码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flowable源码地址:https://github.com/flowable/flowable-engine

Flowable-6.7.2 源码注释地址:https://github.com/solojin/flowable-6.7.2-annotated

包路径:org.flowable.engine.impl.jobexecutor

TimerActivateProcessDefinitionHandler 定时器激活流程定义处理器

/**
 * 定时器激活流程定义处理器
 *
 * @author Joram Barrez
 */
public class TimerActivateProcessDefinitionHandler extends TimerChangeProcessDefinitionSuspensionStateJobHandler {

    // 类型:激活流程定义
    public static final String TYPE = "activate-processdefinition";

    // 获取类型
    @Override
    public String getType() {
        return TYPE;
    }

    // 执行作业,job作业实体,configuration配置,variableScope变量范围,commandContext命令上下文
    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
        
        boolean activateProcessInstances = false;
        try {
            JsonNode configNode = processEngineConfiguration.getObjectMapper().readTree(configuration);
            activateProcessInstances = getIncludeProcessInstances(configNode);
        } catch (Exception e) {
            // 读取json值时出错
            throw new FlowableException("Error reading json value " + configuration, e);
        }

        String processDefinitionId = job.getProcessDefinitionId();
        ActivateProcessDefinitionCmd activateProcessDefinitionCmd = new ActivateProcessDefinitionCmd(processDefinitionId, null, activateProcessInstances, null, job.getTenantId());
        activateProcessDefinitionCmd.execute(commandContext);
    }

}

AbstractProcessInstanceMigrationJobHandler 抽象流程实例迁移作业处理器

/**
 * 抽象流程实例迁移作业处理器
 */
public abstract class AbstractProcessInstanceMigrationJobHandler implements JobHandler {

    // 批量结果状态标签
    public static final String BATCH_RESULT_STATUS_LABEL = "resultStatus";
    // 批量结果信息标签
    public static final String BATCH_RESULT_MESSAGE_LABEL = "resultMessage";

    protected static final String CFG_LABEL_BATCH_ID = "batchId";
    protected static final String CFG_LABEL_BATCH_PART_ID = "batchPartId";
    
    protected static String getBatchIdFromHandlerCfg(String handlerCfg) {
        try {
            JsonNode cfgAsJson = getObjectMapper().readTree(handlerCfg);
            if (cfgAsJson.has(CFG_LABEL_BATCH_ID)) {
                return cfgAsJson.get(CFG_LABEL_BATCH_ID).asText();
            }
            return null;
        } catch (IOException e) {
            return null;
        }
    }

    protected static String getBatchPartIdFromHandlerCfg(String handlerCfg) {
        try {
            JsonNode cfgAsJson = getObjectMapper().readTree(handlerCfg);
            if (cfgAsJson.has(CFG_LABEL_BATCH_PART_ID)) {
                return cfgAsJson.get(CFG_LABEL_BATCH_PART_ID).asText();
            }
            return null;
        } catch (IOException e) {
            return null;
        }
    }
    
    public static String getHandlerCfgForBatchId(String batchId) {
        ObjectNode handlerCfg = getObjectMapper().createObjectNode();
        handlerCfg.put(CFG_LABEL_BATCH_ID, batchId);
        return handlerCfg.toString();
    }

    public static String getHandlerCfgForBatchPartId(String batchPartId) {
        ObjectNode handlerCfg = getObjectMapper().createObjectNode();
        handlerCfg.put(CFG_LABEL_BATCH_PART_ID, batchPartId);
        return handlerCfg.toString();
    }

    protected static ObjectMapper getObjectMapper() {
        if (CommandContextUtil.getCommandContext() != null) {
            return CommandContextUtil.getProcessEngineConfiguration().getObjectMapper();
        } else {
            return new ObjectMapper();
        }
    }
}

ProcessInstanceMigrationJobHandler 流程实例迁移作业处理器

/**
 * 流程实例迁移作业处理器
 */
public class ProcessInstanceMigrationJobHandler extends AbstractProcessInstanceMigrationJobHandler {

    // 类型:流程迁移
    public static final String TYPE = "process-migration";

    @Override
    public String getType() {
        return TYPE;
    }

    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
        BatchService batchService = processEngineConfiguration.getBatchServiceConfiguration().getBatchService();
        ProcessInstanceMigrationManager processInstanceMigrationManager = processEngineConfiguration.getProcessInstanceMigrationManager();

        String batchPartId = getBatchPartIdFromHandlerCfg(configuration);
        BatchPart batchPart = batchService.getBatchPart(batchPartId);
        Batch batch = batchService.getBatch(batchPart.getBatchId());
        ProcessInstanceMigrationDocument migrationDocument = ProcessInstanceMigrationDocumentImpl.fromJson(batch.getBatchDocumentJson(processEngineConfiguration.getEngineCfgKey()));

        String exceptionMessage = null;
        try {
            processInstanceMigrationManager.migrateProcessInstance(batchPart.getScopeId(), migrationDocument, commandContext);
        } catch (FlowableException e) {
            exceptionMessage = e.getMessage();
        }

        String resultAsJsonString = prepareResultAsJsonString(exceptionMessage);
        
        if (exceptionMessage != null) {
            batchService.completeBatchPart(batchPartId, ProcessInstanceBatchMigrationResult.RESULT_FAIL, resultAsJsonString);
        } else {
            batchService.completeBatchPart(batchPartId, ProcessInstanceBatchMigrationResult.RESULT_SUCCESS, resultAsJsonString);
        }
    }

    protected static String prepareResultAsJsonString(String exceptionMessage) {
        ObjectNode objectNode = getObjectMapper().createObjectNode();
        if (exceptionMessage == null) {
            objectNode.put(BATCH_RESULT_STATUS_LABEL, ProcessInstanceBatchMigrationResult.RESULT_SUCCESS);
        } else {
            objectNode.put(BATCH_RESULT_STATUS_LABEL, ProcessInstanceBatchMigrationResult.RESULT_FAIL);
            objectNode.put(BATCH_RESULT_MESSAGE_LABEL, exceptionMessage);
        }
        return objectNode.toString();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值