Flowable入门

2019-04-13  本文已影响0人  蜗牛DWade

创建流程定义

  1. 添加maven依赖包
<dependencies>
  <dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-engine</artifactId>
    <version>6.4.1</version>
  </dependency>
  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.3.176</version>
  </dependency>
</dependencies>
  1. Flowable在内部使用SLF4J作为其日志框架

部署流程定义

  1. Flowable引擎期望以BPMN2.0格式定义流程

    • 从流程定义中,可以启动许多流程实例
      image
      (1)每个步骤(活动)都有一个id属性,在XML文件中为其提供唯一标识符
      (2)活动之间由一个序列流连接
      (3)离开网关(带X的菱形)的序列流两者都具有以表达式形式定义的条件
      • 批准的变量称为过程变量
  2. 将流程定义部署到引擎

RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
  .addClasspathResource("holiday-request.bpmn20.xml")
  .deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
  .deploymentId(deployment.getId())
  .singleResult();
System.out.println("Found process definition : " + processDefinition.getName());

启动流程实例

<process id="holidayRequest" name="Holiday Request" isExecutable="true">

RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("employee", employee);
variables.put("nrOfHolidays", nrOfHolidays);
variables.put("description", description);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);

后续流程分析

  1. 数据库事务在保证数据一致性和解决并发问题方面起着至关重要的作用
  1. 查看任务列表
int taskIndex = Integer.valueOf(scanner.nextLine());
Task task = tasks.get(taskIndex - 1);
//对比查看启动流程实例的参数
Map<String, Object> processVariables = taskService.getVariables(task.getId());
System.out.println(processVariables.get("employee") + " wants " +
    processVariables.get("nrOfHolidays") + " of holidays. Do you approve this?");
boolean approved = scanner.nextLine().toLowerCase().equals("y");
variables = new HashMap<String, Object>();
variables.put("approved", approved);
taskService.complete(task.getId(), variables);

该任务现在已完成,并且基于批准的过程变量选择离开专用网关的两个路径之一

  1. 编写JavaDelegate
<serviceTask id="externalSystemCall" name="Enter holidays in external system"
    flowable:class="org.flowable.CallExternalSystemDelegate"/>

创建类org.flowable.CallExternalSystemDelegate实现JavaDelegate接口并实现execute方法

  1. 使用历史数据
HistoryService historyService = processEngine.getHistoryService();
List<HistoricActivityInstance> activities =
  historyService.createHistoricActivityInstanceQuery()
   .processInstanceId(processInstance.getId())
   .finished()
   .orderByHistoricActivityInstanceEndTime().asc()
   .list();
for (HistoricActivityInstance activity : activities) {
  System.out.println(activity.getActivityId() + " took "
    + activity.getDurationInMillis() + " milliseconds");
}

演示代码的地址:https://github.com/yehuali/holidayRequest

主要参考Flowable的官方文档:https://www.flowable.org

上一篇 下一篇

猜你喜欢

热点阅读