activiti快速入门

activiti快速入门--简单请假例子(1)

 

 

1.      新建一个maven项目

2.      pom.xml文件添加所需依赖:

 
  1. <dependencies>

  2.  
  3. <dependency>

  4.  
  5. <groupId>org.activiti</groupId>

  6.  
  7. <artifactId>activiti-engine</artifactId>

  8.  
  9. <version>5.21.0</version>

  10.  
  11. </dependency>

  12.  
  13. <dependency>

  14.  
  15. <groupId>org.activiti</groupId>

  16.  
  17. <artifactId>activiti-spring</artifactId>

  18.  
  19. <version>5.21.0</version>

  20.  
  21. </dependency>

  22.  
  23. <dependency>

  24.  
  25. <groupId>org.codehaus.groovy</groupId>

  26.  
  27. <artifactId>groovy-all</artifactId>

  28.  
  29. <version>2.4.3</version>

  30.  
  31. </dependency>

  32.  
  33. <dependency>

  34.  
  35. <groupId>org.slf4j</groupId>

  36.  
  37. <artifactId>slf4j-api</artifactId>

  38.  
  39. <version>1.7.6</version>

  40.  
  41. </dependency>

  42.  
  43. <dependency>

  44.  
  45. <groupId>org.slf4j</groupId>

  46.  
  47. <artifactId>slf4j-jdk14</artifactId>

  48.  
  49. <version>1.7.6</version>

  50.  
  51. </dependency>

  52.  
  53. <dependency>

  54.  
  55. <groupId>junit</groupId>

  56.  
  57. <artifactId>junit</artifactId>

  58.  
  59. <version>3.8.1</version>

  60.  
  61. <scope>test</scope>

  62.  
  63. </dependency>

  64.  
  65. <dependency>

  66.  
  67. <groupId>mysql</groupId>

  68.  
  69. <artifactId>mysql-connector-java</artifactId>

  70.  
  71. <version>5.1.38</version>

  72.  
  73. </dependency>

  74.  
  75. </dependencies>

 

3.  创建activiti数据表,具体操作如下:

1)     创建配置文件:activiti.cfg.xml,添加以下代码:(我用的是mysql数据库)

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

  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"

  4.  
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  6.  
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans

  8.  
  9. http://www.springframework.org/schema/beans/spring-beans.xsd">

  10.  
  11.  
  12.  
  13. <bean id="processEngineConfiguration"class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">

  14.  
  15. <property name="databaseSchemaUpdate" value="true"/>

  16.  
  17. <property name="jdbcUrl"value="jdbc:mysql://localhost/activiti?useUnicode=true&characterEncoding=UTF-8"/>

  18.  
  19. <property name="jdbcDriver"value="com.mysql.jdbc.Driver" />

  20.  
  21. <property name="jdbcUsername"value="root" />

  22.  
  23. <property name="jdbcPassword"value="" />

  24.  
  25.  
  26.  
  27. <!-- Databaseconfigurations -->

  28.  
  29. <!--<property name="databaseSchemaUpdate" value="true" />-->

  30.  
  31.  
  32.  
  33. <!-- jobexecutor configurations -->

  34.  
  35. <!--<property name="jobExecutorActivate" value="false" />-->

  36.  
  37. </bean>

  38.  
  39. </beans>

 

2)     写CreateTable.java创建表,代码如下:

 
  1. publicclass CreateTable {

  2.  
  3. @Test

  4.  
  5. publicvoid createTable(){

  6.  
  7. ProcessEngine processEngine =

  8.  
  9. ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml")

  10.  
  11. .buildProcessEngine();

  12.  
  13. System.out.println("processEngine="+processEngine);

  14.  
  15. }

  16.  
  17. }

 

3)     跑下上面代码就可以了,数据库中会多出25张数据表

4.  创建process.bpmn画流程图:(简单画一个请假例子,填写基本信息key和name,添加一个开始事件和结束事件Event:StartEvent、EndEvent,以及添加两个Task事件:UserTask)

 

点击对应task分配任务人assignee,在main config下填写:

 

该bpmn文件选择用xml形式打开的话,生成上面所画的代码如下:

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

  2.  
  3. <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="http://www.activiti.org/test">

  4.  
  5. <process id="HelloWorldKey" name="HelloWorldName" isExecutable="true">

  6.  
  7. <startEvent id="startevent1" name="Start">

  8.  
  9. <extensionElements>

  10.  
  11. <activiti:executionListener event="start" class="com.cwh.activiti.TaskListenerImpl"></activiti:executionListener>

  12.  
  13. </extensionElements>

  14.  
  15. </startEvent>

  16.  
  17. <userTask id="usertask1" name="申请" activiti:assignee="cwh">

  18.  
  19. <extensionElements>

  20.  
  21. <activiti:taskListener event="create" class="com.cwh.activiti.TaskListenerImpl"></activiti:taskListener>

  22.  
  23. </extensionElements>

  24.  
  25. </userTask>

  26.  
  27. <userTask id="usertask2" name="经理审批" activiti:assignee="menco">

  28.  
  29. <extensionElements>

  30.  
  31. <activiti:taskListener event="create" class="com.cwh.activiti.TaskListenerImpl"></activiti:taskListener>

  32.  
  33. </extensionElements>

  34.  
  35. </userTask>

  36.  
  37. <endEvent id="endevent1" name="End"></endEvent>

  38.  
  39. <sequenceFlow id="flow1" sourceRef="startevent1"targetRef="usertask1"></sequenceFlow>

  40.  
  41. <sequenceFlow id="flow2" sourceRef="usertask1"targetRef="usertask2"></sequenceFlow>

  42.  
  43. <sequenceFlow id="flow3" sourceRef="usertask2"targetRef="endevent1"></sequenceFlow>

  44.  
  45. </process>

  46.  
  47. <bpmndi:BPMNDiagram id="BPMNDiagram_HelloWorldKey">

  48.  
  49. <bpmndi:BPMNPlane bpmnElement="HelloWorldKey" id="BPMNPlane_HelloWorldKey">

  50.  
  51. <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">

  52.  
  53. <omgdc:Bounds height="35.0"width="35.0" x="260.0"y="40.0"></omgdc:Bounds>

  54.  
  55. </bpmndi:BPMNShape>

  56.  
  57. <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">

  58.  
  59. <omgdc:Bounds height="55.0"width="105.0" x="225.0"y="100.0"></omgdc:Bounds>

  60.  
  61. </bpmndi:BPMNShape>

  62.  
  63. <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">

  64.  
  65. <omgdc:Bounds height="55.0"width="105.0" x="225.0"y="200.0"></omgdc:Bounds>

  66.  
  67. </bpmndi:BPMNShape>

  68.  
  69. <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">

  70.  
  71. <omgdc:Bounds height="35.0"width="35.0" x="260.0"y="310.0"></omgdc:Bounds>

  72.  
  73. </bpmndi:BPMNShape>

  74.  
  75. <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">

  76.  
  77. <omgdi:waypoint x="277.0"y="75.0"></omgdi:waypoint>

  78.  
  79. <omgdi:waypoint x="277.0"y="100.0"></omgdi:waypoint>

  80.  
  81. </bpmndi:BPMNEdge>

  82.  
  83. <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">

  84.  
  85. <omgdi:waypoint x="277.0"y="155.0"></omgdi:waypoint>

  86.  
  87. <omgdi:waypoint x="277.0"y="200.0"></omgdi:waypoint>

  88.  
  89. </bpmndi:BPMNEdge>

  90.  
  91. <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">

  92.  
  93. <omgdi:waypoint x="277.0"y="255.0"></omgdi:waypoint>

  94.  
  95. <omgdi:waypoint x="277.0"y="310.0"></omgdi:waypoint>

  96.  
  97. </bpmndi:BPMNEdge>

  98.  
  99. </bpmndi:BPMNPlane>

  100.  
  101. </bpmndi:BPMNDiagram>

  102.  
  103. </definitions>

 

 

5. 好了下面开始真正的流程操作了,我们创建一个operate.java文件来操作流程,具体步骤如下:

         1)部署我们创建的bpmn流程如到数据库中:(运行后:在act_re_deployment流程部署表和act_re_procdef流程定义表中会有对应的数据信息)

 

 
  1. private static ProcessEngine processEngine =ProcessEngines.getDefaultProcessEngine();

  2.  
  3. @Test

  4.  
  5. public void delployFlow(){

  6.  
  7. Deploymentdeployment = processEngine.getRepositoryService()

  8.  
  9. .createDeployment()

  10.  
  11. .name("helloworld")

  12.  
  13. .addClasspathResource("Process.bpmn")

  14.  
  15. .addClasspathResource("Process.png")

  16.  
  17. .deploy();

  18.  
  19. System.out.println(deployment.getId());

  20.  
  21. System.out.println(deployment.getName());

  22.  
  23. }

 

 

补充下:

上面中的png文件,如果保存bpmn不会自动生成PNG文件的话可以以下操作:

Window->preferences->activity->saveaction->把create process这个选择框勾上;

解释下ProcessEngine这个类,在Activiti中最核心的类,其他的类都是由他而来,例如:

上面的processEngine.getRepositoryService()//管理流程定义

1.1 RuntimeService :是activiti的流程执行服务类,可以从这个服务类中获取很多关于流程执行的相关的信息。

1.2    TaskService是activiti的任务服务类。可以从这个类中获取任务的相关信息,如当前正在执行的个人待办和用户组待办任务。

1.3    HistoryService是activiti的查询历史信息的类,在一个流程执行完成后,这个对象为我们提供查询历史信息,可以跟踪流程实例对应所有待办节点的运行情况。

1.4    ProcessDefinition流程定义类,可以从这里获得资源文件等。

1.5    ProcessInstance代表流程定义的执行实例,当一个部署的流程图启动后,该流程只有一条流程实例数据,但是它的流程任务可以有多个,每个任务对应流程图中相应的流程节点。

2)接着上面后开启流程:

 

 
  1. @Test

  2.  
  3. publicvoid flowStart(){

  4.  
  5. RuntimeServiceruntimeService = processEngine.getRuntimeService();

  6.  
  7. //用key启动时按照最新的流程图版本定义启动

  8.  
  9. ProcessInstanceprocessInstance = runtimeService.startProcessInstanceByKey("HelloWorldKey");

  10.  
  11. }

 

 

补充:

其中runtimeService.startProcessInstanceByKey("HelloWorldKey")中的HelloWorldKey对应流程图中的ID值,在数据表中对应act_re_procdef流程定义表中的key字段

     启动完流程后在act_ru_execution表中会产生一条数据,这条数据为当前流程正在执行的任务,其中act_id_字段的值对应流程图节点的ID值在act_ru_task表中会产生一条任务数据,execution_id_对应act_ru_execution主键,proc_inst_id_为流程实例ID,name_值为流程节点名称,assignee_字段为该待办当前的处理人(也就是上面我们在画流程图中所设置的cwh)

3)查询下任务人所有的任务:

 

 
  1. @Test

  2.  
  3. publicvoid findMyPersonTask(){

  4.  
  5. Stringassignee = "cwh";

  6.  
  7. List<Task>taskList= processEngine.getTaskService()//获取任务service

  8.  
  9. .createTaskQuery()//创建查询对象

  10.  
  11. .taskAssignee(assignee)//指定查询人

  12.  
  13. .list();

  14.  
  15. if(taskList.size()>0){

  16.  
  17. for (Task task : taskList){

  18.  
  19. System.out.println("代办任务ID:"+task.getId());

  20.  
  21. System.out.println("代办任务name:"+task.getName());

  22.  
  23. System.out.println("代办任务创建时间:"+task.getCreateTime());

  24.  
  25. System.out.println("代办任务办理人:"+task.getAssignee());

  26.  
  27. System.out.println("流程实例ID:"+task.getProcessInstanceId());

  28.  
  29. System.out.println("执行对象ID:"+task.getExecutionId());

  30.  
  31. }

  32.  
  33. }

  34.  
  35. }


 

4)             提交任务到下一个任务人(也就是提交我们的申请到经理手中去)

 
  1. @Test

  2.  
  3. publicvoid completeTask(){

  4.  
  5. StringtaskId = "17504"; processEngine.getTaskService().complete(taskId);//完成任务

  6.  
  7. System.out.println("完成任务,任务ID"+taskId);

  8.  
  9. }

 

补充:taskId对应上面数据表act_ru_task中所生成的ID,也就是我们上面通过cwh查询出来的任务id,提交后该笔任务就到了下一个人手上(也就是经理,我们上面设置的assignee ‘menco’)那么我们查询下menco的任务信息,调用上面3)写的findMyPersonTask():改查询条件assignee为‘menco’

  

5)             经理拿到该条申请后,进行审批然后提交之后那么该笔任务流程也就结束了

 
  1. @Test

  2.  
  3. publicvoid completeTask1(){

  4.  
  5. Stringtaskid = "20002";

  6.  
  7. TaskServicetaskService = processEngine.getTaskService();

  8.  
  9. processEngine.getTaskService().complete(taskid);//完成任务

  10.  
  11. System.out.println("完成任务,任务ID"+taskid);

  12.  
  13.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值