Flowable源码注释(三十)触发器定时事件作业处理器、流程事件作业处理器

本文深入探讨了Flowable中TriggerTimerEventJobHandler和ProcessEventJobHandler的实现。TriggerTimerEventJobHandler是触发器定时事件作业处理器,而ProcessEventJobHandler则作为流程事件作业处理器。这两者都是Flowable引擎实现的重要组成部分,它们负责处理不同类型的事件和任务。

摘要生成于 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

TriggerTimerEventJobHandler 触发器定时事件作业处理器

继承 JobHandler 作业处理器接口类

package org.flowable.engine.impl.jobexecutor;

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.persistence.entity.ExecutionEntity;
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;

/**
 * 触发器定时事件作业处理器
 *
 * @author Joram Barrez
 */
public class TriggerTimerEventJobHandler implements JobHandler {

    // 类型:触发器定时
    public static final String TYPE = "trigger-timer";

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

    // 执行作业,job工作实体,configuration配置,variableScope变量范围,commandContext命令上下文
    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        // 变量范围强制转换执行器实体类
        ExecutionEntity executionEntity = (ExecutionEntity) variableScope;
        // 通过命令上下文工具类获取议程,设置计划触发执行操作
        CommandContextUtil.getAgenda(commandContext).planTriggerExecutionOperation(executionEntity);
        // 根据命令上下文获取流程引擎配置
        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
        // 根据流程引擎配置获取Flowable事件调度器
        FlowableEventDispatcher eventDispatcher = processEngineConfiguration.getEventDispatcher();
        // 如果Flowable事件调度器存在且已开启
        if (eventDispatcher != null && eventDispatcher.isEnabled()) {
            // 调度执行事件
            eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.TIMER_FIRED, job),
                    processEngineConfiguration.getEngineCfgKey());
        }
    }
    
}

ProcessEventJobHandler 流程事件作业处理器

同样继承 JobHandler 作业处理器接口类

package org.flowable.engine.impl.jobexecutor;

import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.EventSubscriptionUtil;
import org.flowable.eventsubscription.service.EventSubscriptionService;
import org.flowable.eventsubscription.service.impl.persistence.entity.EventSubscriptionEntity;
import org.flowable.job.service.JobHandler;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;

/**
 * 流程事件作业处理器
 *
 * @author Daniel Meyer
 * @author Joram Barrez
 */
public class ProcessEventJobHandler implements JobHandler {

    // 类型:事件
    public static final String TYPE = "event";

    // 获取作业处理器类型
    @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);
        // 根据流程引擎配置获取事件订阅配置以及事件订阅服务类
        EventSubscriptionService eventSubscriptionService = processEngineConfiguration.getEventSubscriptionServiceConfiguration().getEventSubscriptionService();

        // 查找订阅:
        EventSubscriptionEntity eventSubscriptionEntity = eventSubscriptionService.findById(configuration);

        // 如果事件订阅为null,则忽略
        if (eventSubscriptionEntity != null) {
            // 事件订阅工具类,接收事件方法
            EventSubscriptionUtil.eventReceived(eventSubscriptionEntity, null, false);
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值