流程的部署主要通过DeployCmd类进行:
查看部分核心代码:
public Deployment execute(CommandContext commandContext) { // Backwards compatibility with Activiti v5 //activiti5兼容开启 if (commandContext.getProcessEngineConfiguration().isActiviti5CompatibilityEnabled() && deploymentBuilder.getDeploymentProperties() != null && deploymentBuilder.getDeploymentProperties().containsKey(DeploymentProperties.DEPLOY_AS_ACTIVITI5_PROCESS_DEFINITION) && deploymentBuilder.getDeploymentProperties().get(DeploymentProperties.DEPLOY_AS_ACTIVITI5_PROCESS_DEFINITION).equals(Boolean.TRUE)) { return deployAsActiviti5ProcessDefinition(commandContext); } //执行部署 return executeDeploy(commandContext); } protected Deployment executeDeploy(CommandContext commandContext) { //获取刚构建的部署对象 DeploymentEntity deployment = deploymentBuilder.getDeployment(); //设置部署当前时间 deployment.setDeploymentTime(commandContext.getProcessEngineConfiguration().getClock().getCurrentTime()); //在部署时会检测已部署的相同文件的最后一条记录,如果内容相同,则不会部署 if (deploymentBuilder.isDuplicateFilterEnabled()) { //保存查到的集合 List existingDeployments = new ArrayList(); if (deployment.getTenantId() == null || ProcessEngineConfiguration.NO_TENANT_ID.equals(deployment.getTenantId())) {//TenantId不存在,通过部署名称查询数据库 //查ACT_RE_DEPLOYMENT该表 DeploymentEntity existingDeployment = commandContext.getDeploymentEntityManager().findLatestDeploymentByName(deployment.getName()); if (existingDeployment != null) { existingDeployments.add(existingDeployment); } } else {//TenantId存在 List deploymentList = commandContext.getProcessEngineConfiguration().getRepositoryService().createDeploymentQuery().deploymentName(deployment.getName()) .deploymentTenantId(deployment.getTenantId()).orderByDeploymentId().desc().list(); if (!deploymentList.isEmpty()) { existingDeployments.addAll(deploymentList); } } DeploymentEntity existingDeployment = null; if (!existingDeployments.isEmpty()) { existingDeployment = (DeploymentEntity) existingDeployments.get(0); } //查到且和要部署的一致,则直接返回查出来的 if ((existingDeployment != null) && !deploymentsDiffer(deployment, existingDeployment)) { return existingDeployment; } } deployment.setNew(true);//设置为一个新的定义 // Save the data //插入ACT_RE_DEPLOYMENT表(1条)和ACT_GE_BYTEARRAY表(png和bpmn定义) commandContext.getDeploymentEntityManager().insert(deployment); if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {//默认开启 //触发deployment entity被创建的事件(默认不执行任何操作,只是日志的输出等) commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, deployment)); } // Deployment settings Map deploymentSettings = new HashMap(); //bpmn2.0检查,默认开启 deploymentSettings.put(DeploymentSettings.IS_BPMN20_XSD_VALIDATION_ENABLED, deploymentBuilder.isBpmn20XsdValidationEnabled()); //process流程定义检查,默认开启 deploymentSettings.put(DeploymentSettings.IS_PROCESS_VALIDATION_ENABLED, deploymentBuilder.isProcessValidationEnabled()); // Actually deploy //执行真正的部署操作 commandContext.getProcessEngineConfiguration().getDeploymentManager().deploy(deployment, deploymentSettings); if (deploymentBuilder.getProcessDefinitionsActivationDate() != null) {//流程定义是否有激活日期 scheduleProcessDefinitionActivation(commandContext, deployment);//进行流程定义的挂起或定时激活 } if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {//默认开启 //触发entity被初始化事件 commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_INITIALIZED, deployment)); } return deployment; }主要做的操作:
1、获取部署对象设置部署时间
2、在部署时会检测已部署的相同文件的最后一条记录,如果内容相同,则不会部署
查到且和要部署的一致,则直接返回查出来的
否则:
2.1 设置为一个新的定义
2.2 插入ACT_RE_DEPLOYMENT表(1条)和ACT_GE_BYTEARRAY表(png和bpmn定义)
2.3设置bpmn2.0检查开启,process流程定义检查开启
3、执行真正的部署
4、流程定义是否有激活日期,有则进行流程定义的挂起或定时激活
分析部署代码:
commandContext.getProcessEngineConfiguration().getDeploymentManager().deploy(deployment, deploymentSettings);
在deploymentManager里调用了BpmnDeployer的deploy方法。查看BpmnDeployer的deploy方法://真正的流程部署
@Override public void deploy(DeploymentEntity deployment, Map deploymentSettings) { log.debug("Processing deployment {}", deployment.getName()); // The ParsedDeployment represents the deployment, the process definitions, and the BPMN // resource, parse, and model associated with each process definition. ParsedDeployment parsedDeployment = parsedDeploymentBuilderFactory .getBuilderForDeploymentAndSettings(deployment, deploymentSettings) .build(); //校验文件里的多个流程定义不能出现多个同样的key bpmnDeploymentHelper.verifyProcessDefinitionsDoNotShareKeys(parsedDeployment.getAllProcessDefinitions()); //通过deployment为流程定义赋