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

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

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

下面看一下部署方式。

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

DeploymentBuilder addInputStream(String resourceName, InputStream inputStream);
  DeploymentBuilder addClasspathResource(String resource);
  DeploymentBuilder addString(String resourceName, String text);
  DeploymentBuilder addZipInputStream(ZipInputStream zipInputStream);
  DeploymentBuilder addBpmnModel(String resourceName, BpmnModel bpmnModel);

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

addInputStream 根据流进行部署。

addClasspathResource 根据resource部署。

addString根据字符串部署。

addZipInputStream根据zip流进行部署。

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

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

1.1.1. addInputStream方式

流程定义如下所示:

 

<?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" 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">
  <process id="daling" name="name_daling" isExecutable="true" activiti:candidateStarterUsers="a,b,c,d">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="usertask1审批" activiti:candidateGroups="1,2"></userTask>
    <userTask id="usertask2" name="usertask2审批" activiti:candidateUsers="b,c"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_daling">
    <bpmndi:BPMNPlane bpmnElement="daling" id="BPMNPlane_daling">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="230.0" y="10.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="300.0" y="110.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="280.0" y="192.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="230.0" y="340.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="247.0" y="45.0"></omgdi:waypoint>
        <omgdi:waypoint x="352.0" y="110.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="352.0" y="165.0"></omgdi:waypoint>
        <omgdi:waypoint x="332.0" y="192.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="332.0" y="247.0"></omgdi:waypoint>
        <omgdi:waypoint x="247.0" y="340.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

程序代码如下所示:

InputStream inputStream=ProcessEnginesDemo.class.getClassLoader().getResourceAsStream("demo1.bpmn");
 Deployment deploy = repositoryService2.createDeployment().addInputStream("addInputStream", inputStream).deploy();
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方式

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

String str=read("D:\\WorkSpace\\activitidemo\\src\\main\\resources/demo1.bpmn");
Deployment deploy2 = repositoryService2.createDeployment().addString("string", str).deploy();
public static String read(String filePath) {
// 读取txt内容为字符串
StringBuffer txtContent = new StringBuffer();
// 每次读取的byte数
byte[] b = new byte[8 * 1024];
InputStream in = null;
try {
// 文件输入流
in = new FileInputStream(filePath);
while (in.read(b) != -1) {
// 字符串拼接
txtContent.append(new String(b));
}
// 关闭流
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return txtContent.toString();
}

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

 

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具体的实现方式如下:

ProcessEnginesDemo demo = new ProcessEnginesDemo();
RepositoryService repositoryService2 = demo.getRepositoryService();
BpmnModel bpmnModel=new BpmnModel();
StartEvent startEvent=new StartEvent();
startEvent.setId("start1shareniu");
startEvent.setName("start1shareniu");
UserTask userTask=new UserTask();
userTask.setId("userTask1shareniu");
userTask.setName("userTask1shareniu");
EndEvent endEvent=new EndEvent();
endEvent.setId("endEventshareniu");
endEvent.setName("endEventshareniu");
List<SequenceFlow> sequenceFlows=new ArrayList<SequenceFlow>();
List<SequenceFlow> toEnd=new ArrayList<SequenceFlow>();
SequenceFlow s1=new SequenceFlow();
s1.setId("starttouserTask");
s1.setName("starttouserTask");
s1.setSourceRef("start1shareniu");
s1.setTargetRef("userTask1shareniu");
sequenceFlows.add(s1);
SequenceFlow s2=new SequenceFlow();
s2.setId("userTasktoend");
s2.setName("userTasktoend");
s2.setSourceRef("userTask1shareniu");
s2.setTargetRef("endEventshareniu");
toEnd.add(s2);
startEvent.setOutgoingFlows(sequenceFlows);
userTask.setOutgoingFlows(toEnd);
userTask.setIncomingFlows(sequenceFlows);
endEvent.setIncomingFlows(toEnd);
Process process=new Process();
process.setId("process1");
process.addFlowElement(startEvent);
process.addFlowElement(s1);
process.addFlowElement(userTask);
process.addFlowElement(s2);
process.addFlowElement(endEvent);
bpmnModel.addProcess(process);
repositoryService2.createDeployment().addBpmnModel("bpmnModel", bpmnModel).deploy();

程序的输出截下图所示:

 

总结:

五种方式的总结:

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

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

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

 

时间: 2024-09-20 12:12:54

Activiti 流程部署方式 activi 动态部署(高级源码篇)的相关文章

activiti 动态配置 activiti 监听引擎启动和初始化(高级源码篇)

1.1.1. 前言 用户故事:现在有这样一个需求,第一个需求:公司的开发环境,测试环境以及线上环境,我们使用的数据库是不一样的,我们必须能够任意的切换数据库进行测试和发布,对数据库连接字符串我们需要加密,保证我们的数据库连接不能被发现.必须确保我们的数据库不能暴露出去,第二个需求,我们需要监控activiti 工作流引擎,在流程启动的之前,我们要保证我们的所有属性都注入进去,如果有些属性没有注入进去,我们是不能让流程启动起来的.也就是进行必要饿属性检测,如果没有期望的属性,直接报错,在流程实例化

关于 Eclipse中的Web项目 部署的文件位置 查看jsp源码的部署位置

使用 eclipse 开发web项目 会默认 部署在 工作目录下: .metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps 在这里 我的工作目录是 F:\work 所以 web的项目 部署的位置应该是: F:\work\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps 在使用jsp 的时候 需要查看 jsp的 对应 Servlet 源码 需要查看 .m

C实现的非阻塞方式命令行端口扫描器源码_C 语言

该实例是一个C实现的基于命令行模式端口扫描代码,并且是以非阻塞方式来实现对IP和端口的连接测试.为了大家使用和学习方便,已在代码中尽可能多的地方加入了注释,相信对于帮助大家理解C端口扫描有很大帮助. 具体功能代码如下: #include <afxext.h> #include <winsock.h> // 编译时需使用的库 #pragma comment(lib,"wsock32.lib") // select()成员定义 #define ZERO (fd_se

源码篇——安卓启动流程

首先说到的是安卓的四层架构:  APPLICTION, APPLICATION FRAMEWORK, LIBRARICB+ANDROID RUNTIME, LINUX KERNEL android启动 当引导程序启动Linux内核后,会加载各种驱动和数据结构,当有了驱动以后,开始启动Android系统同时会加载用户级别的第一个进程init(system\core\init.c)代码如下: int main(int argc, char **argv) { // 创建文件夹 挂载 mount("t

这才是 TensorFlow 自带可视化工具 TensorBoard 的正确打开方式!(附项目源码)

  TensorBoard 如何更直观的观察数据在神经网络中的变化,或是已经构建的神经网络的结构.上一篇文章说到,可以使用 matplotlib 第三方可视化,来进行一定程度上的可视化.然而Tensorflow也自带了可视化模块Tensorboard,并且能更直观的看见整个神经网络的结构. 上面的结构图甚至可以展开,变成:   使用 结构图: with tensorflow .name_scope(layer_name): 直接使用以上代码生成一个带可展开符号的一个域,并且支持嵌套操作: wit

Apache Spark源码走读(七)Standalone部署方式分析&amp;sql的解析与执行

<一>Standalone部署方式分析 楔子 在Spark源码走读系列之2中曾经提到Spark能以Standalone的方式来运行cluster,但没有对Application的提交与具体运行流程做详细的分析,本文就这些问题做一个比较详细的分析,并且对在standalone模式下如何实现HA进行讲解. 没有HA的Standalone运行模式 先从比较简单的说起,所谓的没有ha是指master节点没有ha. 组成cluster的两大元素即Master和Worker.slave worker可以有

到底应该选择哪种Linux.NET的部署方式?

到底应该选择哪种Linux.NET的部署方式? 当前部署Linux.NET环境的方式可谓是五花八门,既有传统的源码编译的方式.又有各式各样的一键安装脚本.还有绿色包安装方式,而随着Mono官方的新站上线,更增加了采用RPM包的部署方式.那对于一名Linux.NET的初学者来说,我们又该如何选择?下面,本文将对这几种的安装方式进行优缺点的比较,从而协助各位读者选择出最佳的部署方式. 本文中,我们将对下列的部署方式展开讨论: 1.源码编译 2.一键安装脚本 3.RPM包 4.绿色包  一.源码编译

使用git checkout的方式进行轻量级部署

动机 很多脚本语言其实没有"编译"的过程,不需要真的输出一个"构建物".那么在CRP上是否有更加轻量的部署方式呢?答案就是直接使用git更新的方式.本文会详细讲述这一方式. 不必要的"打包" 前面我们发表两篇文章分别描述了Ruby和Java在CRP上的持续交付实践.两篇文章中都使用了下面的配置来对代码库进行编译打包: 不同之处是Java打的是一个war包,而Ruby打的其实是源码的压缩包,因为Ruby不需要真正的"编译"过程.

CentOS 6.7 源码搭建LNMP架构部署动态网站环境

源码搭建LNMP架构部署动态网站环境 Nginx 简介 Nginx是一款相当优秀的用于部署动态网站的服务程序,Nginx具有不错的稳定性.丰富的功能以及占用较少的系统资源等独特特性. Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器.Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日.其将源代码以类BSD许可证的形式发布,因它的稳定性.丰富的功能集.示例