2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源

学习资料:《Activiti实战》

第五章 用户与组及部署管理(二)部署流程资源

内容概览:讲解流程资源的读取与部署。

5.2 部署流程资源

5.2.1 流程资源

流程资源常用的有以下几种:

1 流程定义文件:拓展名为bpmn20.xml和bpmn
2 流程定义的图片:拓展名为PNG
3 表单文件:拓展名为form
4 规则文件:拓展名为drl

部署流程资源的时候,要注意一点:

引擎会根据不同的拓展名进行不同的处理。bpmn或bpmn20.xml类型的文件,会在ACT_RU_PROCDEF(流程定义表)、ACT_GE_BYTEARRAY(字节流表)两个表中都插入一条数据。而png类型的文件,只会在ACT_GE_BYTEARRAY表中插入一条数据。

所以可以看到,下文中所有部署,assertEquals(int,processDefinitionQuery)中的int都没有包含png的数目在内。

5.2.2 部署流程资源

启动一个流程实例processInstance,需要首先部署流程定义processDefinition,流程定义由许多活动Activity组成。

部署流程定义的方法有很多种,包括:

1 classpath方式:addClasspathResource()
2 InputStream方式:addInputStream()
3 字符串方式:addString()
4 zip/bar方式:addZipInputStream()

(1)classpath方式

一般用在测试环节,真实的产品环境中很少用到这种方式。一般是管理页面手动部署或者设计完流程后直接部署到engine中。

项目中资源文件存放的位置:

2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源

示例代码(为了方便说明,没有使用链式编程,而是把每一步都分开写了):

 1 public class ClasspathDeploymentTest extends AbstractTest{
 2     @Test
 3     public void testClasspathDeployment throws Exception(){
 4         String bpmnClasspath = "chapter5/candidateUserInUserTask.bpmn";
 5         String pngClasspath = "chapter5/candidateUserInUserTask.bng";
 6 
 7         DeploymentBuilder db = repositoryService.createDeployment();
 8         db.addClasspathResource(bpmnClasspath);
 9         db.addClasspathResource(pngClasspath);
10         db.deploy();
11 
12         //验证部署是否成功
13         ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery();
14         long count = pdq.processDefinitionKey("candidateUserInUserTask").count;
15         assertEquals(1,count);
16 
17         //读取图片文件
18         ProcessDefinition pd = ProcessDefinitionQuery.singleResult();
19         String diagramResourceName = pd.getDiagramResourceName();
20         assertEquals(pngClasspath,diagramResourceName);
21     }    
22 }

 (2)InputStream方式

InputStream方式在产品环境中用的比较多,比如从web客户端接受一个文件对象,或者从URL中获取文件流,最后部署到engine中。

InputStream方式需要传入一个输入流资源的名称。输入流的来源不限,可以是绝对路径,可以是classpath,可以是网络获取。

(从绝对路径获取)示例代码:

 1 public class InputStreamDeploymentTest extends AbstractTest{
 2     @Test
 3     public void testInputStreamFromAbsoluteFilePath() throws Exception{
 4         String filePath = "/Users/henryyan/work/books/aia-books/aia-codes/bpmn20-example/src/test/
 5         resources/chapter5/userAndGroupInUserTask.bpmn";
 6 
 7         FileInputStream fis = new FileInputStream(filePath);
 8         repositoryService.createDeployment()
 9             .addInputStream("userAndGroupInUserTask.bpmn",fis)
10             .deploy();//用userAndGroupInUserTask.bpmn作为资源名称
11 
12         //验证部署是否成功
13         ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery();
14         long count = pdq.processDefinitionKey("userAndGroupInUserTask").count;
15         assertEquals(1,count);
16     }
17 }

(3)字符串方式

字符串方式是直接传入纯文本作为资源的来源。

示例代码:

 1 public class StringDeploymentTest extends AbstractTest{
 2     //完整的text内容略
 3     private String text = "<?xml version="1.0" encoding="UTF-8"?><definitions>...</definitions>";
 4     
 5     @Test
 6     public void testCharsDeployment(){//用candidateUserInUserTask.bpmn作为资源名称
 7         repositoryService.createDeployment().addString("candidateUserInUserTask.bpmn",text).deploy();
 8 
 9         //验证部署是否成功
10         ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery();
11         long count = pdq.processDefinitionKey("candidateUserInUserTask").count;
12         assertEquals(1,count);
13     }
14 }

(4)zip/bar格式压缩包方式

前面三种方式一次都只能部署一个资源。除非执行多次deployment.addXXX()方法。

zip/bar方式,允许用户将资源打包,一次性部署多个资源。这几个资源关联的部署ID相同,因为属于同一次部署。

将文件打包为bar或者zip均可:

2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源

示例代码:

这里的addZipInputStream()相当于for循环了n次addInputStream()。

且这四个资源关联的是同一个deploymentID。

 1 public class ZipStreamDeploymentTest extends AbstractTest{
 2     @Test
 3     public class testZipStreamFromAbsoluteFilePath(){
 4         InputStream zipStream = getClass().getClassLoader().getResourceAsStream("chapter5/chapter5-deployment.bar");
 7         repositoryService.createDeployment().addZipInputStream(new ZipInputStream(zipStream)).deploy();
10         
11         //验证部署是否成功
12         long count = repositoryService.createProcessDefinitionQuery().count();
13         assertEquals(2,count);//注意,不是4!!!
14 
15         //查询部署记录
16         Deployment deployment = repositoryService.createDeploymentQuery().singleResult();
17         assertNotNull(deployment);
18 
19         //验证四个文件均部署成功,且属于同一个部署ID
20         String deploymentID = deployment.getId();
21         assertNotNull(repositoryService.getResourceAsStream(deploymentID,"candidateUserInUserTask.bpmn"));
22         assertNotNull(repositoryService.getResourceAsStream(deploymentID,"candidateUserInUserTask.png"));
23         assertNotNull(repositoryService.getResourceAsStream(deploymentID,"userAndGroupInUserTask.bpmn"));
24         assertNotNull(repositoryService.getResourceAsStream(deploymentID,"userAndGroupInUserTask.png"));
25     }
26 }