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);
}
}
}