Flowable源码注释(二十九)作业处理器、定时启动事件作业处理器

本文深入探讨Flowable中JobHandler接口和TimerStartEventJobHandler类,详细注释了Flowable-6.7.2版本的相关源码,分析其在流程执行中的作用。

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

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

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

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

JobHandler 作业处理器接口类

package org.activiti.engine.impl.jobexecutor;

import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.job.api.Job;
/**
 * 作业处理器接口类
 *
 * @author Tom Baeyens
 */
public interface JobHandler {

    // 获取作业处理器类型
    String getType();

    // 执行作业
    void execute(Job job, String configuration, ExecutionEntity execution, CommandContext commandContext);
}

TimerStartEventJobHandler 定时启动事件作业处理器

继承 JobHandler 作业处理器接口类

package org.flowable.engine.impl.jobexecutor;

import org.flowable.bpmn.model.FlowElement;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.delegate.event.impl.FlowableEventBuilder;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.cmd.StartProcessInstanceCmd;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.engine.impl.util.ProcessInstanceHelper;
import org.flowable.job.service.JobHandler;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 定时启动事件作业处理器
 * 可以在开始事件中设置定时启动,这样当流程部署完成后,超过预定时间,流程实例自动启动
 * */
public class TimerStartEventJobHandler extends TimerEventHandler implements JobHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(TimerStartEventJobHandler.class);

    // 类型:定时启动事件
    public static final String TYPE = "timer-start-event";

    // 获取作业处理器类型
    @Override
    public String getType() {
        return TYPE;
    }

    // 执行作业,configuration配置,variableScope变量范围,commandContext命令上下文
    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {

        ProcessDefinitionEntity processDefinitionEntity = ProcessDefinitionUtil
                .getProcessDefinitionFromDatabase(job.getProcessDefinitionId()); // From DB -> need to get latest suspended state
        if (processDefinitionEntity == null) {
            throw new FlowableException("找不到定时器启动事件所需的进程定义");
        }

        try {
            if (!processDefinitionEntity.isSuspended()) {
                ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
                FlowableEventDispatcher eventDispatcher = processEngineConfiguration.getEventDispatcher();
                if (eventDispatcher != null && eventDispatcher.isEnabled()) {
                    eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.TIMER_FIRED, job),
                            processEngineConfiguration.getEngineCfgKey());
                }

                // 找到与信号启动事件匹配的初始流程元素
                org.flowable.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(job.getProcessDefinitionId());
                String activityId = TimerEventHandler.getActivityIdFromConfiguration(configuration);
                if (activityId != null) {
                    FlowElement flowElement = process.getFlowElement(activityId, true);
                    if (flowElement == null) {
                        // 找不到与activityId匹配的流程元素
                        throw new FlowableException("Could not find matching FlowElement for activityId " + activityId);
                    }
                    ProcessInstanceHelper processInstanceHelper = processEngineConfiguration.getProcessInstanceHelper();
                    processInstanceHelper.createAndStartProcessInstanceWithInitialFlowElement(processDefinitionEntity, null, null, null, flowElement, process
                            , null, null, true);
                } else {
                    new StartProcessInstanceCmd(processDefinitionEntity.getKey(), null, null, null, job.getTenantId()).execute(commandContext);
                }

            } else {
                // 忽略挂起的进程定义{}的定时器
                LOGGER.debug("ignoring timer of suspended process definition {}", processDefinitionEntity.getName());
            }
        } catch (RuntimeException e) {
            // 定时器执行期间出现异常
            LOGGER.error("exception during timer execution", e);
            throw e;
        } catch (Exception e) {
            // 定时器执行期间出现异常
            LOGGER.error("exception during timer execution", e);
            // // 定时器执行期间出现异常
            throw new FlowableException("exception during timer execution: " + e.getMessage(), e);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值