Flowable源码注释(二十三)部署管理器

本文深入探讨Flowable引擎中DeploymentManager的角色,它位于org.flowable.dmn.engine.impl.persistence.deploy包下,主要负责流程部署管理。通过查看Flowable-6.7.2的源码注释,可以理解其在流程部署过程中的关键功能。

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

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

包路径 org.flowable.dmn.engine.impl.persistence.deploy

DeploymentManager 部署管理器

package org.flowable.dmn.engine.impl.persistence.deploy;

import java.util.List;
import java.util.Map;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
import org.flowable.common.engine.impl.persistence.deploy.DeploymentCache;
import org.flowable.dmn.api.DmnDecision;
import org.flowable.dmn.engine.DmnEngineConfiguration;
import org.flowable.dmn.engine.impl.DecisionQueryImpl;
import org.flowable.dmn.engine.impl.persistence.entity.DecisionEntity;
import org.flowable.dmn.engine.impl.persistence.entity.DecisionEntityManager;
import org.flowable.dmn.engine.impl.persistence.entity.DmnDeploymentEntity;
import org.flowable.dmn.engine.impl.persistence.entity.DmnDeploymentEntityManager;
import org.flowable.dmn.engine.impl.persistence.entity.DmnResourceEntity;

/**
 * @author Tijs Rademakers
 * @author Joram Barrez
 * @author Yvo Swillens
 */
public class DeploymentManager {

    protected DmnEngineConfiguration engineConfig;
    protected DeploymentCache<DecisionCacheEntry> decisionCache;

    protected List<Deployer> deployers;
    protected DecisionEntityManager decisionEntityManager;
    protected DmnDeploymentEntityManager deploymentEntityManager;

    public DeploymentManager(DeploymentCache<DecisionCacheEntry> decisionCache, DmnEngineConfiguration engineConfig) {
        this.decisionCache = decisionCache;
        this.engineConfig = engineConfig;
    }

    public void deploy(DmnDeploymentEntity deployment) {
        deploy(deployment, null);
    }

    public void deploy(DmnDeploymentEntity deployment, Map<String, Object> deploymentSettings) {
        for (Deployer deployer : deployers) {
            deployer.deploy(deployment, deploymentSettings);
        }
    }

    public DecisionEntity findDeployedDecisionById(String decisionId) {
        if (decisionId == null) {
            // 非法决策ID值:空值
            throw new FlowableException("Invalid decision id : null");
        }

        // 首先尝试缓存
        DecisionCacheEntry cacheEntry = decisionCache.get(decisionId);
        DecisionEntity decision = cacheEntry != null ? cacheEntry.getDecisionEntity() : null;

        if (decision == null) {
            decision = engineConfig.getDecisionEntityManager().findById(decisionId);
            if (decision == null) {
                throw new FlowableObjectNotFoundException("no decision found with id '" + decisionId + "'");
            }
            decision = resolveDecision(decision).getDecisionEntity();
        }
        return decision;
    }

    public DecisionEntity findDeployedLatestDefinitionByKey(String definitionKey) {
        DecisionEntity definition = decisionEntityManager.findLatestDecisionByKey(definitionKey);

        if (definition == null) {
            throw new FlowableObjectNotFoundException("no decisions deployed with key '" + definitionKey + "'");
        }
        definition = resolveDecision(definition).getDecisionEntity();
        return definition;
    }

    public DecisionEntity findDeployedLatestDefinitionByKeyAndTenantId(String definitionKey, String tenantId) {
        DecisionEntity definition = decisionEntityManager.findLatestDecisionByKeyAndTenantId(definitionKey, tenantId);

        if (definition == null) {
            throw new FlowableObjectNotFoundException("no decisions deployed with key '" + definitionKey + "' for tenant identifier '" + tenantId + "'");
        }
        definition = resolveDecision(definition).getDecisionEntity();
        return definition;
    }

    public DecisionEntity findDeployedLatestDecisionByKeyAndDeploymentId(String definitionKey, String deploymentId) {
        DecisionEntity definition = decisionEntityManager.findDecisionByDeploymentAndKey(deploymentId, definitionKey);

        if (definition == null) {
            throw new FlowableObjectNotFoundException("no decisions deployed with key '" + definitionKey +
                            "' for deployment id '" + deploymentId + "'");
        }
        definition = resolveDecision(definition).getDecisionEntity();
        return definition;
    }

    public DecisionEntity findDeployedLatestDecisionByKeyDeploymentIdAndTenantId(String definitionKey,
            String deploymentId, String tenantId) {
        DecisionEntity definition = decisionEntityManager.findDecisionByDeploymentAndKeyAndTenantId(deploymentId, definitionKey, tenantId);

        if (definition == null) {
            throw new FlowableObjectNotFoundException("no decisions deployed with key '" + definitionKey +
                            "' for deployment id '" + deploymentId + "' and tenant identifier " + tenantId);
        }
        definition = resolveDecision(definition).getDecisionEntity();
        return definition;
    }

    public DecisionEntity findDeployedDefinitionByKeyAndVersionAndTenantId(String definitionKey, int definitionVersion, String tenantId) {
        DecisionEntity definition = decisionEntityManager.findDecisionByKeyAndVersionAndTenantId(definitionKey, definitionVersion, tenantId);

        if (definition == null) {
            throw new FlowableObjectNotFoundException("no decision deployed with key = '" + definitionKey + "' and version = '" + definitionVersion + "'");
        }

        definition = resolveDecision(definition).getDecisionEntity();
        return definition;
    }

    /**
     * 解析决策获取DMN,解析它并将Dmn定义{@ link org.flowable.DMN.model.DmnDefinition }存储在内存中。
     */
    public DecisionCacheEntry resolveDecision(DmnDecision decision) {
        String decisionId = decision.getId();
        String deploymentId = decision.getDeploymentId();

        DecisionCacheEntry cachedDecision = decisionCache.get(decisionId);

        if (cachedDecision == null) {
            DmnDeploymentEntity deployment = engineConfig.getDeploymentEntityManager().findById(deploymentId);
            List<DmnResourceEntity> resources = engineConfig.getResourceEntityManager().findResourcesByDeploymentId(deploymentId);
            for (DmnResourceEntity resource : resources) {
                deployment.addResource(resource);
            }

            deployment.setNew(false);
            deploy(deployment, null);
            cachedDecision = decisionCache.get(decisionId);

            if (cachedDecision == null) {
                throw new FlowableException("deployment '" + deploymentId + "' didn't put decision '" + decisionId + "' in the cache");
            }
        }
        return cachedDecision;
    }

    public void removeDeployment(String deploymentId) {

        DmnDeploymentEntity deployment = deploymentEntityManager.findById(deploymentId);
        if (deployment == null) {
            throw new FlowableObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.");
        }

        // 从缓存中删除任何dmn定义
        List<DmnDecision> definitions = new DecisionQueryImpl().deploymentId(deploymentId).list();

        // 删除数据
        deploymentEntityManager.deleteDeployment(deploymentId);

        for (DmnDecision definition : definitions) {
            decisionCache.remove(definition.getId());
        }
    }

    public List<Deployer> getDeployers() {
        return deployers;
    }

    public void setDeployers(List<Deployer> deployers) {
        this.deployers = deployers;
    }

    public DeploymentCache<DecisionCacheEntry> getDecisionCache() {
        return decisionCache;
    }

    public void setDecisionCache(DeploymentCache<DecisionCacheEntry> decisionCache) {
        this.decisionCache = decisionCache;
    }

    public DecisionEntityManager getDecisionEntityManager() {
        return decisionEntityManager;
    }

    public void setDecisionEntityManager(DecisionEntityManager decisionEntityManager) {
        this.decisionEntityManager = decisionEntityManager;
    }

    public DmnDeploymentEntityManager getDeploymentEntityManager() {
        return deploymentEntityManager;
    }

    public void setDeploymentEntityManager(DmnDeploymentEntityManager deploymentEntityManager) {
        this.deploymentEntityManager = deploymentEntityManager;
    }
}

Flowable是一个强大的业务流程管理(BPM)引擎,它基于Java实现,支持BPMN 2.0标准。对于想要深入了解Flowable内部机制和实现原理的开发者来说,源码学习是一个非常重要的步骤。以下是一些关键点和步骤,帮助你更好地学习Flowable源码: ### 1. 准备工作 - **搭建开发环境**:确保你有一个适合的Java开发环境,推荐使用IntelliJ IDEA或Eclipse。 - **获取源码**:可以从GitHub上克隆Flowable源码仓库:`git clone https://github.com/flowable/flowable-engine.git` ### 2. 核心模块 - **flowable-engine**:这是Flowable的核心模块,包含了流程引擎的实现。 - **flowable-spring**:提供了与Spring框架的集成。 - **flowable-bpmn-converter**:负责将BPMN 2.0 XML文件转换为Flowable的内部模型。 - **flowable-jobexecutor**:负责执行定时任务和异步任务。 ### 3. 关键类和方法 - **ProcessEngine**:流程引擎的入口类,负责创建和管理流程实例。 - **RepositoryService**:提供对流程定义和部署的管理。 - **RuntimeService**:用于启动和管理流程实例。 - **TaskService**:用于管理和查询任务。 - **HistoryService**:用于查询历史数据。 ### 4. 源码阅读技巧 - **从入口开始**:从`ProcessEngine`类的初始化开始,逐步深入了解各个组件的初始化过程。 - **关注接口和实现**:Flowable大量使用了接口和实现类的设计模式,理解接口的设计和实现类的具体逻辑。 - **调试和日志**:通过调试和查看日志,可以更清晰地了解代码的执行流程。 ### 5. 学习资源 - **官方文档**:Flowable的官方文档提供了详细的API说明和使用示例。 - **社区和论坛**:加入Flowable的社区和论坛,与其他开发者交流经验和问题。 - **源码注释**:Flowable源码中有大量的注释,阅读这些注释可以帮助理解代码的意图。 ### 6. 示例代码 ```java import org.flowable.engine.ProcessEngine; import org.flowable.engine.ProcessEngineConfiguration; import org.flowable.engine.RepositoryService; import org.flowable.engine.RuntimeService; import org.flowable.engine.TaskService; public class FlowableExample { public static void main(String[] args) { // 创建流程引擎 ProcessEngine processEngine = ProcessEngineConfiguration .createStandaloneInMemProcessEngineConfiguration() .buildProcessEngine(); // 获取RepositoryService RepositoryService repositoryService = processEngine.getRepositoryService(); // 部署流程定义 repositoryService.createDeployment() .addClasspathResource("holiday-request.bpmn20.xml") .deploy(); // 获取RuntimeService RuntimeService runtimeService = processEngine.getRuntimeService(); // 启动流程实例 runtimeService.startProcessInstanceByKey("holidayRequest"); // 获取TaskService TaskService taskService = processEngine.getTaskService(); // 查询任务 taskService.createTaskQuery().list().forEach(task -> { System.out.println("Task available: " + task.getName()); }); // 关闭流程引擎 processEngine.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值