Flowable源码注释(三十六)流程实例迁移状态作业处理器、BPMN历史清除作业处理器、外部工作者任务完成作业处理器

本文深入探讨Flowable引擎的三个关键作业处理器:流程实例迁移状态处理器、BPMN历史清除处理器和外部工作者任务完成处理器。通过源码注释,了解它们在流程执行和历史清理中的作用。
摘要由CSDN通过智能技术生成

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

ProcessInstanceMigrationStatusJobHandler 流程实例迁移状态作业处理器

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

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

    @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();
        
        String batchId = getBatchIdFromHandlerCfg(configuration);
        Batch batch = batchService.getBatch(batchId);
        
        List<BatchPart> batchParts = batchService.findBatchPartsByBatchId(batchId);
        int completedBatchParts = 0;
        int failedBatchParts = 0;
        for (BatchPart batchPart : batchParts) {
            if (batchPart.getCompleteTime() != null) {
                completedBatchParts++;
                
                if (ProcessInstanceBatchMigrationResult.RESULT_FAIL.equals(batchPart.getStatus())) {
                    failedBatchParts++;
                }
            }
        }
        
        if (completedBatchParts == batchParts.size()) {
            batchService.completeBatch(batch.getId(), ProcessInstanceBatchMigrationResult.STATUS_COMPLETED);
            job.setRepeat(null);
        
        } else {
            if (batchParts.size() == 0) {
                updateBatchStatus(batch, "No batch parts", batchService);
                job.setRepeat(null);
            
            } else {
                int completedPercentage = completedBatchParts / batchParts.size() * 100;
                updateBatchStatus(batch, completedPercentage + "% completed, " + failedBatchParts + " failed", batchService);
            }
        }
    }
    
    protected void updateBatchStatus(Batch batch, String status, BatchService batchService) {
        ((BatchEntity) batch).setStatus(ProcessInstanceBatchMigrationResult.STATUS_COMPLETED);
        batchService.updateBatch(batch);
    }

}

BpmnHistoryCleanupJobHandler BPMN历史清除作业处理器

package org.flowable.engine.impl.jobexecutor;

import org.flowable.batch.api.BatchQuery;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.job.service.JobHandler;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;

/**
 * BPMN历史清除作业处理器
 */
public class BpmnHistoryCleanupJobHandler implements JobHandler {

    // 类型:BPMN历史清除
    public static final String TYPE = "bpmn-history-cleanup";

    private static final String DEFAULT_BATCH_NAME = "Flowable BPMN History Cleanup";

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

    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);

        int batchSize = processEngineConfiguration.getCleanInstancesBatchSize();

        // 查询可清除的历史流程实例信息,并清除BPMN历史
        HistoricProcessInstanceQuery query = processEngineConfiguration.getHistoryCleaningManager().createHistoricProcessInstanceCleaningQuery();
        if (processEngineConfiguration.isCleanInstancesSequentially()) {
            query.deleteSequentiallyUsingBatch(batchSize, DEFAULT_BATCH_NAME);
        } else {
            query.deleteInParallelUsingBatch(batchSize, DEFAULT_BATCH_NAME);
        }

        // 批量查询,查询批量可清除信息,清除关联数据
        BatchQuery batchCleaningQuery = processEngineConfiguration.getHistoryCleaningManager().createBatchCleaningQuery();
        if (batchCleaningQuery != null) {
            batchCleaningQuery.deleteWithRelatedData();
        }
    }
    
}

ExternalWorkerTaskCompleteJobHandler 外部工作者任务完成作业处理器

package org.flowable.engine.impl.jobexecutor;

import java.util.List;

import org.flowable.common.engine.api.scope.ScopeTypes;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.bpmn.helper.ErrorPropagation;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.CountingEntityUtil;
import org.flowable.job.service.JobHandler;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;
import org.flowable.variable.service.VariableService;
import org.flowable.variable.service.impl.persistence.entity.VariableInstanceEntity;
/**
 * 外部工作者任务完成作业处理器
 * 
 * @author Filip Hrisafov
 */
public class ExternalWorkerTaskCompleteJobHandler implements JobHandler {

    // 类型:外部工作者完成
    public static final String TYPE = "external-worker-complete";

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

    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        ExecutionEntity executionEntity = (ExecutionEntity) variableScope;

        // 标记外部工作者完成状态,并清除运行时变量信息
        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
        VariableService variableService = processEngineConfiguration.getVariableServiceConfiguration().getVariableService();
        List<VariableInstanceEntity> jobVariables = variableService.findVariableInstanceBySubScopeIdAndScopeType(executionEntity.getId(), ScopeTypes.BPMN_EXTERNAL_WORKER);
        for (VariableInstanceEntity jobVariable : jobVariables) {
            executionEntity.setVariable(jobVariable.getName(), jobVariable.getValue());
            CountingEntityUtil.handleDeleteVariableInstanceEntityCount(jobVariable, false);
            variableService.deleteVariableInstance(jobVariable);
        }

        if (configuration != null && configuration.startsWith("error:")) {
            String errorCode;
            if (configuration.length() > 6) {
                errorCode = configuration.substring(6);
            } else {
                errorCode = null;
            }
            ErrorPropagation.propagateError(errorCode, executionEntity);
        } else {
            CommandContextUtil.getAgenda(commandContext).planTriggerExecutionOperation(executionEntity);
        }
    }

}
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值