Java教程

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

本文主要是介绍Flowable源码注释(二十三)部署管理器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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源码注释(二十三)部署管理器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!