Activiti 流程部署方式 activi 动态部署(高级源码篇)

来源:互联网 发布:淘宝评价怎么删除追加 编辑:程序博客网 时间:2024/05/18 08:27

http://blog.csdn.net/qq_30739519/article/details/51166993

欢迎加入我们的学习提升群523988350,里面有京东、美团网的技术人员,可以相互交流


Activiti的流程 部署方式有很多种方式,我们可以根据activit工作流引擎提供的ap方式进行部署。

当然了实际需求决定你要使用哪一种api操作,后面的总结详细介绍了使用场景。

下面看一下部署方式。

流程部署的方式在类org.activiti.engine.repository.DeploymentBuilder中定义的部署方接口式如下 :

[java] view plain copy
  1. DeploymentBuilder addInputStream(String resourceName, InputStream inputStream);  
  2.   DeploymentBuilder addClasspathResource(String resource);  
  3.   DeploymentBuilder addString(String resourceName, String text);  
  4.   DeploymentBuilder addZipInputStream(ZipInputStream zipInputStream);  
  5.   DeploymentBuilder addBpmnModel(String resourceName, BpmnModel bpmnModel);  


可以看出activit工作流引擎一共提供五种方式进行流程对的部署。 

addInputStream 根据流进行部署。

addClasspathResource 根据resource部署。

addString根据字符串部署。

addZipInputStream根据zip流进行部署。

addBpmnModel 根据BpmnModel进行部署。这种方式使用的场景就是我们自己设计一个流程设计器画布,自己去解析成bpmn规范文件。适合动态的拓展。自定义。

下面一一讲解如何使用api去进行部署。

1.1.1. addInputStream方式

流程定义如下所示:

 

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="daling">  
  3.   <process id="daling" name="name_daling" isExecutable="true" activiti:candidateStarterUsers="a,b,c,d">  
  4.     <startEvent id="startevent1" name="Start"></startEvent>  
  5.     <userTask id="usertask1" name="usertask1审批" activiti:candidateGroups="1,2"></userTask>  
  6.     <userTask id="usertask2" name="usertask2审批" activiti:candidateUsers="b,c"></userTask>  
  7.     <endEvent id="endevent1" name="End"></endEvent>  
  8.     <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>  
  9.     <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>  
  10.     <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>  
  11.   </process>  
  12.   <bpmndi:BPMNDiagram id="BPMNDiagram_daling">  
  13.     <bpmndi:BPMNPlane bpmnElement="daling" id="BPMNPlane_daling">  
  14.       <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">  
  15.         <omgdc:Bounds height="35.0" width="35.0" x="230.0" y="10.0"></omgdc:Bounds>  
  16.       </bpmndi:BPMNShape>  
  17.       <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">  
  18.         <omgdc:Bounds height="55.0" width="105.0" x="300.0" y="110.0"></omgdc:Bounds>  
  19.       </bpmndi:BPMNShape>  
  20.       <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">  
  21.         <omgdc:Bounds height="55.0" width="105.0" x="280.0" y="192.0"></omgdc:Bounds>  
  22.       </bpmndi:BPMNShape>  
  23.       <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">  
  24.         <omgdc:Bounds height="35.0" width="35.0" x="230.0" y="340.0"></omgdc:Bounds>  
  25.       </bpmndi:BPMNShape>  
  26.       <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">  
  27.         <omgdi:waypoint x="247.0" y="45.0"></omgdi:waypoint>  
  28.         <omgdi:waypoint x="352.0" y="110.0"></omgdi:waypoint>  
  29.       </bpmndi:BPMNEdge>  
  30.       <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">  
  31.         <omgdi:waypoint x="352.0" y="165.0"></omgdi:waypoint>  
  32.         <omgdi:waypoint x="332.0" y="192.0"></omgdi:waypoint>  
  33.       </bpmndi:BPMNEdge>  
  34.       <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">  
  35.         <omgdi:waypoint x="332.0" y="247.0"></omgdi:waypoint>  
  36.         <omgdi:waypoint x="247.0" y="340.0"></omgdi:waypoint>  
  37.       </bpmndi:BPMNEdge>  
  38.     </bpmndi:BPMNPlane>  
  39.   </bpmndi:BPMNDiagram>  
  40. </definitions>  


程序代码如下所示:

[java] view plain copy
  1. InputStream inputStream=ProcessEnginesDemo.class.getClassLoader().getResourceAsStream("demo1.bpmn");  
  2.  Deployment deploy = repositoryService2.createDeployment().addInputStream("addInputStream", inputStream).deploy();  
  3. System.out.println(deploy);  


程序的运行结果如下所示:


 

 

1.1.2. addClasspathResource方式

流程的定义还是第一种方式的流程图,程序如下所示:

 Deployment deploy2 = repositoryService2.createDeployment().addClasspathResource("demo1.bpmn").deploy();

addClasspathResource中的参数是根据classpath方式加载,如果demo1.bpmn路径在com.daling.ch1.ProcessEnginesDemo包下面则参数参入com/daling/ch1/ProcessEnginesDemo/demo1.bpmn

程序的运行如下图所示:

 

1.1.3. addString方式

流程的定义还是第一种方式的流程图,程序如下所示:

[java] view plain copy
  1. String str=read("D:\\WorkSpace\\activitidemo\\src\\main\\resources/demo1.bpmn");  
  2. Deployment deploy2 = repositoryService2.createDeployment().addString("string", str).deploy();  
  3. public static String read(String filePath) {  
  4. // 读取txt内容为字符串  
  5. StringBuffer txtContent = new StringBuffer();  
  6. // 每次读取的byte数  
  7. byte[] b = new byte[8 * 1024];  
  8. InputStream in = null;  
  9. try {  
  10. // 文件输入流  
  11. in = new FileInputStream(filePath);  
  12. while (in.read(b) != -1) {  
  13. // 字符串拼接  
  14. txtContent.append(new String(b));  
  15. }  
  16. // 关闭流  
  17. in.close();  
  18. catch (FileNotFoundException e) {  
  19. // TODO Auto-generated catch block  
  20. e.printStackTrace();  
  21. catch (IOException e) {  
  22. // TODO Auto-generated catch block  
  23. e.printStackTrace();  
  24. finally {  
  25. if (in != null) {  
  26. try {  
  27. in.close();  
  28. catch (IOException e) {  
  29. // TODO Auto-generated catch block  
  30. e.printStackTrace();  
  31. }  
  32. }  
  33. }  
  34. return txtContent.toString();  
  35. }  


程序的运行效果如下图所示:


 

1.1.4. addZipInputStream方式

流程的定义还是第一种方式的流程图,只不过讲demo1.bpmn打包成zip文件,结构如下:


 

 

程序操作如下所示:

InputStream inputStream=ProcessEnginesDemo.class.getClassLoader().getResourceAsStream("demo1.zip");

ZipInputStream zipInputStream=new ZipInputStream(inputStream);

Deployment deploy2 = repositoryService2.createDeployment().addZipInputStream(zipInputStream).deploy();

程序的输出结果如下图所示:

 

 

1.1.5. addBpmnModel方式

这一种方式比较复杂需要自己去手动拼接对象,这里我们就写一个简单的吧。实际开发中如果不够用可以自己扩展根据这个demo 具体的实现方式如下:

[java] view plain copy
  1. ProcessEnginesDemo demo = new ProcessEnginesDemo();  
  2. RepositoryService repositoryService2 = demo.getRepositoryService();  
  3. BpmnModel bpmnModel=new BpmnModel();  
  4. StartEvent startEvent=new StartEvent();  
  5. startEvent.setId("start1shareniu");  
  6. startEvent.setName("start1shareniu");  
  7. UserTask userTask=new UserTask();  
  8. userTask.setId("userTask1shareniu");  
  9. userTask.setName("userTask1shareniu");  
  10. EndEvent endEvent=new EndEvent();  
  11. endEvent.setId("endEventshareniu");  
  12. endEvent.setName("endEventshareniu");  
  13. List<SequenceFlow> sequenceFlows=new ArrayList<SequenceFlow>();  
  14. List<SequenceFlow> toEnd=new ArrayList<SequenceFlow>();  
  15. SequenceFlow s1=new SequenceFlow();  
  16. s1.setId("starttouserTask");  
  17. s1.setName("starttouserTask");  
  18. s1.setSourceRef("start1shareniu");  
  19. s1.setTargetRef("userTask1shareniu");  
  20. sequenceFlows.add(s1);  
  21. SequenceFlow s2=new SequenceFlow();  
  22. s2.setId("userTasktoend");  
  23. s2.setName("userTasktoend");  
  24. s2.setSourceRef("userTask1shareniu");  
  25. s2.setTargetRef("endEventshareniu");  
  26. toEnd.add(s2);  
  27. startEvent.setOutgoingFlows(sequenceFlows);  
  28. userTask.setOutgoingFlows(toEnd);  
  29. userTask.setIncomingFlows(sequenceFlows);  
  30. endEvent.setIncomingFlows(toEnd);  
  31. Process process=new Process();  
  32. process.setId("process1");  
  33. process.addFlowElement(startEvent);  
  34. process.addFlowElement(s1);  
  35. process.addFlowElement(userTask);  
  36. process.addFlowElement(s2);  
  37. process.addFlowElement(endEvent);  
  38. bpmnModel.addProcess(process);  
  39. repositoryService2.createDeployment().addBpmnModel("bpmnModel", bpmnModel).deploy();  


程序的输出截下图所示:

 

总结:

五种方式的总结:

1.如果需要自己开发一套流程设计的话就使用addBpmnModel这种方法吧。这种方式更加灵活,缺点就是需要了解每一个对象的含义,需要对bpmnMode对象中的各个子对象都有所了解。

2.如果项目中的流程图是固定的但是一些候选组或者人或者名称不是固定的,需要从数据库中查询出来赋值在部署使用addString这种方法,配合velocity等叶面静态化工具一起使用。

3.如果需要用户自己上传文件部署的话,可以使用addInputStream和addZipInputStream这两种方式。


0 0
原创粉丝点击