You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不部署的情况下将流程XML文档存入act_ge_bytearray?

不部署Camunda流程却存储XML到act_ge_bytearray并保存流程定义的方法

Camunda默认仅在部署流程时才会将BPMN XML写入act_ge_bytearray并生成act_re_procdef中的流程定义记录,但可以通过调用其内部命令API绕过完整部署流程,实现你的需求。以下是具体实现方式:

核心思路

通过手动构造部署实体、解析流程XML,再调用Camunda的事务命令执行器,直接将相关数据持久化到对应表中,跳过部署流程的缓存同步、事件触发等环节,确保流程定义仅存储而不处于可执行状态。

Java代码实现

import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.impl.cmd.Command;
import org.camunda.bpm.engine.impl.cmd.CommandContext;
import org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ResourceEntity;
import org.camunda.bpm.engine.impl.persistence.deploy.DeploymentHandler;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

public class PersistProcessWithoutDeployment {
    public static void main(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        
        // 1. 构造部署实体
        DeploymentEntity deployment = new DeploymentEntity();
        deployment.setName("my-undeployed-process");
        // 若使用多租户,设置租户ID:deployment.setTenantId("your-tenant-id");
        
        // 2. 加载BPMN XML内容(替换为你的流程XML)
        String bpmnXml = """
                <?xml version="1.0" encoding="UTF-8"?>
                <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://camunda.org/examples">
                  <process id="my-process" name="My Undeployed Process" isExecutable="true">
                    <!-- 流程内容 -->
                  </process>
                </definitions>
                """;
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bpmnXml.getBytes(StandardCharsets.UTF_8));
        deployment.addResource("my-process.bpmn", inputStream);
        
        // 3. 执行事务命令,持久化数据
        processEngine.getProcessEngineConfiguration()
                .getCommandExecutorTxRequired()
                .execute((Command<Void>) commandContext -> {
                    // 保存部署记录到act_re_deployment
                    commandContext.getDeploymentManager().insertDeployment(deployment);
                    
                    // 解析并保存流程定义到act_re_procdef
                    ResourceEntity resource = deployment.getResources().get("my-process.bpmn");
                    ProcessDefinitionEntity processDefinition = DeploymentHandler.parseProcessDefinition(deployment, resource);
                    processDefinition.setDeploymentId(deployment.getId());
                    commandContext.getProcessDefinitionManager().insertProcessDefinition(processDefinition);
                    
                    // 保存XML字节数组到act_ge_bytearray
                    commandContext.getResourceManager().insertResource(resource);
                    
                    return null;
                });
    }
}

注意事项

  • 此方法依赖Camunda内部API,不同版本(如7.x和8.x)的实体类或命令逻辑可能存在差异,需根据你使用的Camunda版本调整代码。
  • 必须确保操作在事务中执行(示例中使用commandExecutorTxRequired),避免数据不一致。
  • 流程XML需符合BPMN规范,否则解析环节会抛出异常。
  • 这种方式生成的流程定义不会被Camunda引擎缓存,无法直接启动实例,若后续需要启用,可执行正常的部署流程。

内容的提问来源于stack exchange,提问作者shine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:05:21