当前位置:   article > 正文

流程引擎应用及分析_check-process-definitions

check-process-definitions

介绍:工作流引擎目前比较热门的有Activiti、Flowable等,Flowable是Activiti(Alfresco持有的注册商标)的fork版本。下面就两种工作流引擎做一个比较和入门分析。

模块一 对比:

  1. Activiti现存文档比Flowable多,有大量与业务集成的文档及博客。相比之下,学习Flowable主要为官方的用户手册。
  2. Flowable是Activiti的fork版本,所以API使用类似,包括核心类、各种服务类和业务对象。
  3. Flowable目前最新版本为6.4.X,其插件设计器只支持Eclipse;Activiti目前最新版本为7.X,这个版本是基于6.X版本的Bug修改和API封装。
  4. 查看Flowable的发展史,发现Flowable的开发人员是原来Activiti的主要负责人员,所以可能Flowable为后起之秀。但目前与业务集成文档较少。

模块二 Flowable引擎使用:

  1. Flowable有几大引擎,分别用来管理不同部分
    1. ProcessEngine(重要):核心引擎,其下管理着各种Service
      1. RuntimeService:负责创建实例
      2. TaskService:负责操作任务,如查询任务
      3. RepositoryService: 负责操作流程,如加载bpmn文件部署流程、创建查询对象
    2. CmmnEngine
    3. DmnEngine:决策引擎
    4. FormEngine:表单引擎
    5. ContentEngine:内容引擎
    6. IdmEngine:身份识别引擎   

    2. 独立运行的Flowable引擎

        2.1 引入引擎的依赖,这里数据库使用的是内存数据库

  1. <!-- Standalone -->
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.flowable</groupId>
  5. <artifactId>flowable-engine</artifactId>
  6. <version>6.3.0</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.h2database</groupId>
  10. <artifactId>h2</artifactId>
  11. <version>1.3.176</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.slf4j</groupId>
  15. <artifactId>slf4j-api</artifactId>
  16. <version>1.7.21</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.slf4j</groupId>
  20. <artifactId>slf4j-log4j12</artifactId>
  21. <version>1.7.21</version>
  22. </dependency>
  23. </dependencies>

        2.2 创建引擎配置,并使用此配置生成引擎

  1. ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
  2. .setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
  3. .setJdbcUsername("sa")
  4. .setJdbcPassword("")
  5. .setJdbcDriver("org.h2.Driver")
  6. .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
  7. ProcessEngine processEngine = cfg.buildProcessEngine();

         2.3  使用RepositoryService部署一个流程,需提供BPMN或BPMN 2.0 XML文件

实例文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  5. xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
  6. xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
  7. xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
  8. xmlns:flowable="http://flowable.org/bpmn"
  9. typeLanguage="http://www.w3.org/2001/XMLSchema"
  10. expressionLanguage="http://www.w3.org/1999/XPath"
  11. targetNamespace="http://www.flowable.org/processdef">
  12. <process id="holidayRequest" name="Holiday Request" isExecutable="true">
  13. <startEvent id="startEvent"/>
  14. <sequenceFlow sourceRef="startEvent" targetRef="approveTask" />
  15. <userTask id="approveTask" name="Approve or reject request" flowable:candidateGroups="managers" />
  16. <sequenceFlow sourceRef="approveTask" targetRef="decision"/>
  17. <exclusiveGateway id="decision"/>
  18. <sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
  19. <conditionExpression xsi:type="tFormalExpression">
  20. <![CDATA[
  21. ${approved}
  22. ]]>
  23. </conditionExpression>
  24. </sequenceFlow>
  25. <sequenceFlow sourceRef="decision" targetRef="sendRejectionMail">
  26. <conditionExpression xsi:type="tFormalExpression">
  27. <![CDATA[
  28. ${!approved}
  29. ]]>
  30. </conditionExpression>
  31. </sequenceFlow>
  32. <serviceTask id="externalSystemCall" name="Enter holidays in external system"
  33. flowable:class="org.flowable.CallExternalSystemDelegate"/>
  34. <sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask"/>
  35. <userTask id="holidayApprovedTask" name="Holiday approved" flowable:assignee="${employee}"/>
  36. <sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd"/>
  37. <serviceTask id="sendRejectionMail" name="Send out rejection email"
  38. flowable:class="org.flowable.SendRejectionMail"/>
  39. <sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd"/>
  40. <endEvent id="approveEnd"/>
  41. <endEvent id="rejectEnd"/>
  42. </process>
  43. </definitions>

        部署流程:

  1. RepositoryService repositoryService = processEngine.getRepositoryService();
  2. Deployment deploy = repositoryService.createDeployment().addClasspathResource("processes/one-task-process.bpmn20.xml").deploy();
  3. ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
  4. System.out.println("流程定义: " + processDefinition.getName());

        2.4 使用RuntimeService开启一个流程实例任务

  1. String employee = "dedu";
  2. Integer holidayNumber = 5;
  3. String reason = "tensorflow";
  4. //根据process id 创建实例
  5. String processId = "holidayRequest";
  6. RuntimeService runtimeService = processEngine.getRuntimeService();
  7. // 开启实例并传入参数
  8. Map<String, Object> conditions = new HashMap<>();
  9. conditions.put("employee", employee);
  10. conditions.put("holidayNumber", holidayNumber);
  11. conditions.put("reason", reason);
  12. runtimeService.startProcessInstanceByKey(processId, conditions);

        2.5 使用TaskService查询并完成任务

  1. // 查询任务
  2. TaskService taskService = processEngine.getTaskService();
  3. List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup("managers").list();
  4. System.out.println("你有"+ taskList.size() + "个任务!");
  5. Map<String, Object> variables = taskService.getVariables(taskList.get(0).getId());
  6. System.out.println(variables);
  7. // 完成任务
  8. variables = new HashMap<>();
  9. variables.put("approved", true);
  10. taskService.complete(taskList.get(0).getId(), variables);
  11. //查询 任务数量
  12. List<Task> endTaskList = taskService.createTaskQuery().taskCandidateGroup("managers").list();
  13. System.out.println("你有"+ endTaskList.size() + "个任务!");

        2.6  可以配置任务完成后的回调,注意package名字需改成org.flowable并实现JavaDelegate接口:

  1. package org.flowable;
  2. import org.flowable.engine.delegate.DelegateExecution;
  3. import org.flowable.engine.delegate.JavaDelegate;
  4. public class CallExternalSystemDelegate implements JavaDelegate {
  5. @Override
  6. public void execute(DelegateExecution delegateExecution) {
  7. System.out.println("任务完成后回调:");
  8. System.out.println(delegateExecution.getVariables());
  9. }
  10. }

    3  整合SpringBoot的Flowable引擎,即由Spring管理Flowable,然后在使用各种Service时直接依赖注入即可。

        3.1 引入依赖,这里使用的Flowable引擎版本为6.4(引入的依赖为flowable-spring-boot-starter,即会加载所有引擎,如只需个别引擎,可单独引入)。这里使用MySQL数据库管理流程,所以引入了JPA。

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <flowable.version>6.4.0</flowable.vers
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/408160
推荐阅读
相关标签
  

闽ICP备14008679号