流程引擎应用及分析

工作流引擎目前比较热门的有Activiti、Flowable等,Flowable是Activiti(Alfresco持有的注册商标)的fork版本。下面就两种工作流引擎做一个比较和入门分析。
摘要由CSDN通过智能技术生成

介绍:工作流引擎目前比较热门的有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 引入引擎的依赖,这里数据库使用的是内存数据库

<!-- Standalone -->
<dependencies>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-engine</artifactId>
        <version>6.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.176</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>
</dependencies>

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

ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
        .setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
        .setJdbcUsername("sa")
        .setJdbcPassword("")
        .setJdbcDriver("org.h2.Driver")
        .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

ProcessEngine processEngine = cfg.buildProcessEngine();

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

实例文件:

<?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: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"
             xmlns:flowable="http://flowable.org/bpmn"
             typeLanguage="http://www.w3.org/2001/XMLSchema"
             expressionLanguage="http://www.w3.org/1999/XPath"
             targetNamespace="http://www.flowable.org/processdef">
    <process id="holidayRequest" name="Holiday Request" isExecutable="true">
        <startEvent id="startEvent"/>
        <sequenceFlow sourceRef="startEvent" targetRef="approveTask"  />
        <userTask id="approveTask" name="Approve or reject request" flowable:candidateGroups="managers" />
        <sequenceFlow sourceRef="approveTask" targetRef="decision"/>
        <exclusiveGateway id="decision"/>
        <sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
            <conditionExpression xsi:type="tFormalExpression">
                <![CDATA[
          ${approved}
        ]]>
            </conditionExpression>
        </sequenceFlow>
        <sequenceFlow  sourceRef="decision" targetRef="sendRejectionMail">
            <conditionExpression xsi:type="tFormalExpression">
                <![CDATA[
          ${!approved}
        ]]>
            </conditionExpression>
        </sequenceFlow>
        <serviceTask id="externalSystemCall" name="Enter holidays in external system"
                     flowable:class="org.flowable.CallExternalSystemDelegate"/>
        <sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask"/>
        <userTask id="holidayApprovedTask" name="Holiday approved" flowable:assignee="${employee}"/>
        <sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd"/>
        <serviceTask id="sendRejectionMail" name="Send out rejection email"
                     flowable:class="org.flowable.SendRejectionMail"/>
        <sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd"/>
        <endEvent id="approveEnd"/>
        <endEvent id="rejectEnd"/>
    </process>
</definitions>

        部署流程:

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

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

String employee = "dedu";
Integer holidayNumber = 5;
String reason = "tensorflow";

//根据process id 创建实例
String processId = "holidayRequest";
RuntimeService runtimeService = processEngine.getRuntimeService();
// 开启实例并传入参数
Map<String, Object> conditions = new HashMap<>();
conditions.put("employee", employee);
conditions.put("holidayNumber", holidayNumber);
conditions.put("reason", reason);
runtimeService.startProcessInstanceByKey(processId, conditions);

        2.5 使用TaskService查询并完成任务

// 查询任务
TaskService taskService = processEngine.getTaskService();
List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup("managers").list();
System.out.println("你有"+ taskList.size() + "个任务!");
Map<String, Object> variables = taskService.getVariables(taskList.get(0).getId());
System.out.println(variables);

// 完成任务
variables = new HashMap<>();
variables.put("approved", true);
taskService.complete(taskList.get(0).getId(), variables);

//查询 任务数量
List<Task> endTaskList = taskService.createTaskQuery().taskCandidateGroup("managers").list();
System.out.println("你有"+ endTaskList.size() + "个任务!");

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

package org.flowable;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class CallExternalSystemDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("任务完成后回调:");
        System.out.println(delegateExecution.getVariables());
    }
}

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

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

<properties>
    <java.version>1.8</java.version>
    <flowable.version>6.4.0</flowable.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.flowable</groupId>
      
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值