---
title: "[#0x0043] jBPM Tips (on updating)"
author: "Aoyi"
date: "2010-07-29T17:37:13Z"
tags: []
categories: ["企业架构"]
slug: "0x0043-jbpm-tips-on-updating"
draft: false
lastmod: "2010-08-13T23:00:51Z"
id: 724630
url: "https://aoyi.iteye.com/blog/724630"
---

**1.** **deploymentId和definitionId应该是一对一的关系**

　　部署的时候，deploy()返回的就是deploymentId。deploymentId虽然是个String，不过仅包含数字，目前我还不清楚它的生成策略。

　　每次部署，还会产生一个definitionId。definitionId的生成与process.jpdl.xml文件有关：

```
<?xml version="1.0" encoding="UTF-8"?>

<process name="ProcessName" key="ProcessKey"  version="1024" xmlns="http://jbpm.org/4.3/jpdl">
	……
</process>
```

definitionId == **key**-**version**，如上面这个jpdl，部署后生成的definitionId就是"ProcessKey-1024"。不过要注意两点：

　　(1) 如果没有写&lt;process key="xxx"&gt;，那么默认key == name &gt;&gt;&gt; ①（特殊情况见章节**3.**）

　　(2) 如果写了&lt;process version="xxx"&gt;，那么这个jpdl只能部署一次，因为version指定了，key-version就定死了。若不写&lt;process version="xxx"&gt;，可以把这个jpdl部署多次，且jvm可以帮你实现version的自增1

 

　　deploymentId和definitionId是可以互查的：

```
/** 根据deployId找definitionId */
String definitionId = processEngine.getRepositoryService().createProcessDefinitionQuery().deploymentId(deployId).uniqueResult().getId();
```

 

```
/** 根据definitionId找deployId */
String deployId = processEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionId(definitionId).uniqueResult().getDeploymentId();
```

（单位项目的需求中，没有解释deploymentId的概念，全部用definitionId代替，好在是一对一的关系，不然就大条了……）

 

**2. 流程定义zip包中的所有文件都是资源**

```
ZipInputStream zis = new ZipInputStream(new FileInputStream("process.jpdl.zip"));

processEngine.getRepositoryService().createDeployment().addResourcesFromZipInputStream(zis).deploy();
```

　　如果是部署的是zip包，那么zip包中的所有文件都是可用的资源。换句话说，你可以把杂七杂八的文件都放在zip包中一起部署。比如现在我的process.jpdl.zip里面不仅有process.jpdl.xml和process.png，还有一个resource.xml，那么这个resource.xml也是可以被获取到的，如：

```
Set<String> resourceSet = processEngine.getRepositoryService().getResourceNames(deployId);
for (String s : resourceSet)
	System.out.println(s); 
// Output: 
// resource.xml
// process.jpdl.xml
// process.png
```

 

 　　除了得到资源的InputStream外

```
ByteArrayInputStream bis = (ByteArrayInputStream)processEngine.getRepositoryService().getResourceAsStream(deployId, "resource.xml");
```

资源还可以被动态修改，不过要注意，动态修改资源这个方法在RepositoryServiceImpl中，RepositoryService接口中竟然没有这个方法，这个应该算是jBPM4.3的一个bug：

```
is2 = new FileInputStream("newResource.xml");

RepositoryServiceImpl rsi = (RepositoryServiceImpl)processEngine.getRepositoryService();
rsi.updateDeploymentResource(deployId, "resource.xml", is2);
```

这样就把newResource.xml的内容写入了resource.xml中（覆盖原有内容），此时再获取resource.xml的InputStream，输出出来的结果就是newResource.xml的内容

 

**3. &lt;process&gt;标签name属性中的短横线会变成下划线赋给key属性（补充①）**

　　假设我部署了一个jpdl.xml，&lt;process name="test-vehicle"&gt;，那么，查询出的ProcessDefinition的属性如下：

```
System.out.println(pd.getName());    // definitionName == "test-vehicle"
System.out.println(pd.getKey());       // definitionKey == "test_vehicle"
System.out.println(pd.getVersion()); // definitionVersion == "1"
System.out.println(pd.getId());         // definitionId == "test_vehicle-1"
```

 

　　这在创建流程实例和查询流程定义的时候要特别注意：

```
ExecutionService.startProcessInstanceById(“test_vehicle-1”);
ExecutionService.startProcessInstanceByKey(“test_vehicle”);
```

startProcessInstanceById()传的是definitionId（不是deploymentId），startProcessInstanceByKey()传的是definitionKey。查询的时候，还可以通过definitionName来查。

```
RepositoryService.createProcessDefinitionQuery().processDefinitionName("test-vehicle");
```

　　

　　在项目中有一个方法，是传入definitionName来创建实例，先RepositoryService.createProcessDefinitionQuery().processDefinitionName("test-vehicle")来确定时候有processDefinition存在，如果有，就ExecutionService.startProcessInstanceByKey("test-vehicle")。结果悲剧了。谨记。