Flowable初体验

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: Flowable初体验

创建一个普通Maven项目

目录结构

 

一、依赖、配置

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.example</groupId>
    <artifactId>clowable_0811</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>clowable_0811</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <!--    flowable-->
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-engine</artifactId>
            <version>6.8.0</version>
        </dependency>
 
        <!--    mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
 
        <!--    测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
 
        <!--    日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.36</version>
        </dependency>
    </dependencies>
</project>

log4j.properties

log4j.rootLogger=DEBUG,CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n

二、定义的流程

<?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="同意或拒绝请假" flowable:assignee="zhangsan"/>
        <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"/>
        <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>

三、流程操作

SendRejectionMail.java


package org.flowable;
 
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
 
public class SendRejectionMail implements JavaDelegate {
    /**
     * flowable触发器
     *
     * @param execution
     */
    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("拒绝了任务");
    }
}
package org.minos;
 
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.task.api.Task;
import org.junit.Before;
import org.junit.Test;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Unit test for simple App.
 */
public class AppTest {
 
    private ProcessEngineConfiguration configuration;
 
    /**
     * 获取流程引擎对象
     */
    @Test
    public void testProcessEngine() {
        //获取ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration();
        //配置相关数据库的连接信息
        configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("123456");
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?serverTimezone=UTC");
        //如果书库中的表结构不存在就新建
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        //通过ProcessEngineConfiguration构建ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
    }
 
    ProcessEngineConfiguration configurati = null;
 
    @Before
    public void before() {
        //获取ProcessEngineConfiguration对象
        configuration = new StandaloneProcessEngineConfiguration();
        //配置相关数据库的连接信息
        configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("123456");
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?serverTimezone=UTC");
        configuration.setActivityFontName("宋体");
        configuration.setLabelFontName("宋体");
        configuration.setAnnotationFontName("宋体");
        //如果书库中的表结构不存在就新建
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
    }
 
    /**
     * 流程部署
     */
    @Test
    public void testDeploy() {
        //1.获取ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //2、获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3.完成流程部署操作
        Deployment deployed = repositoryService.createDeployment()
                .addClasspathResource("demo.bpmn20.xml")
                .name("请假流程")
                .deploy();
 
        System.out.println("deployed.getId()=" + deployed.getId());
 
        System.out.println("deployed.getName()=" + deployed.getName());
    }
 
    /**
     * 查询流程定义信息
     */
    @Test
    public void testDeployQuery() {
        ProcessEngine processEngine = configuration.buildProcessEngine();
 
        RepositoryService repositoryService = processEngine.getRepositoryService();
 
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId("1")
                .singleResult();
 
        System.out.println("processDefinition.getId() = " + processDefinition.getId());
        System.out.println("processDefinition.getName() = " + processDefinition.getName());
        System.out.println("processDefinition.getDescription() = " + processDefinition.getDescription());
    }
 
    /**
     * 删除部署流程
     */
    @Test
    public void testDeleteDeploy() {
        ProcessEngine processEngine = configuration.buildProcessEngine();
 
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //删除部署ID为1的流程,如果已经启动,删除失败
        //repositoryService.deleteDeployment("1");
        //删除部署ID为1的流程,如果已经启动,启动实例也删除
        repositoryService.deleteDeployment("2501", true);
    }
 
    /**
     * 启动流程实例
     */
    @Test
    public void testRunProcess() {
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RunTimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //构建流程变量
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", "zhangsan");
        variables.put("nrOfHolidays", 3);
        variables.put("desciption", "work hard");
        //启动流程实例
        ProcessInstance holidayRequest = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
 
        System.out.println("holidayRequest.getProcessDefinitionId() = " + holidayRequest.getProcessDefinitionId());
        System.out.println("holidayRequest.getActivityId() = " + holidayRequest.getActivityId());
        System.out.println("holidayRequest.getId() = " + holidayRequest.getId());
    }
 
    /**
     * 查询任务
     */
    @Test
    public void testQueryProcess() {
        ProcessEngine processEngine = configuration.buildProcessEngine();
 
        TaskService taskService = processEngine.getTaskService();
 
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("holidayRequest")//指定查询的流程key
                .taskAssignee("zhangsan")//查询任务的处理人
                .list();
 
        for (Task task : list) {
            System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
            System.out.println("task.getName() = " + task.getName());
            System.out.println("task.getAssignee() = " + task.getAssignee());
            System.out.println("task.getDescription() = " + task.getDescription());
            System.out.println("task.getId() = " + task.getId());
        }
    }
 
    /**
     * 完成当前任务
     */
 
    @Test
    public void testCompleteTask() {
        ProcessEngine processEngine = configuration.buildProcessEngine();
 
        TaskService taskService = processEngine.getTaskService();
 
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("holidayRequest")
                .taskAssignee("zhangsan")
                .list();
        //创建流程变量
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("approved", false);
        for (Task task : list) {
            taskService.complete(task.getId(), variables);
        }
    }
 
    /**
     * 获取流程任务的历史数据
     */
    @Test
    public void testHistroy() {
        ProcessEngine processEngine = configuration.buildProcessEngine();
 
        HistoryService historyService = processEngine.getHistoryService();
 
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .processDefinitionId("holidayRequest:1:7503")
                .finished() //查询的历史记录的状态已完成
                .orderByHistoricActivityInstanceEndTime().asc() //指定排序字段和顺序
                .list();
        for (HistoricActivityInstance item : list) {
            System.out.print("item.getActivityName() = " + item.getActivityName());
            System.out.print("item.getAssignee() = " + item.getAssignee());
            System.out.print("item.getActivityId() = " + item.getActivityId());
            System.out.println("item.getDurationInMillis() = " + item.getDurationInMillis() + "毫秒");
        }
    }
 
}
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
数据可视化 前端开发 Java
SpringBoot 集成 Flowable + Flowable Modeler 流程配置可视化(图解)(一)
SpringBoot 集成 Flowable + Flowable Modeler 流程配置可视化(图解)
1093 0
|
5月前
|
数据可视化 Java 数据库
手把手实现springboot整合flowable,非常简单【附源码.视频】
手把手实现springboot整合flowable,非常简单【附源码.视频】
300 2
|
数据可视化 安全 前端开发
SpringBoot 集成 Flowable + Flowable Modeler 流程配置可视化(图解)(三)
SpringBoot 集成 Flowable + Flowable Modeler 流程配置可视化(图解)
385 0
|
XML 数据可视化 Java
SpringBoot 集成 Flowable + Flowable Modeler 流程配置可视化(图解)(二)
SpringBoot 集成 Flowable + Flowable Modeler 流程配置可视化(图解)
478 0
|
XML 存储 JSON
SpringBoot集成Flowable实践
实际上是定义一个表单form表,一个deploy_form表。form存放动态表单json,deploy_form表建立流程模型部署信息与表单信息的关系。 流程模型挂载表单实际上就是往deply_form存放模型挂载的表单信息。 目的为了当通过模型启动流程实例的时候,将流程模型(部署id)对应的动态表单json取出来。
479 0
SpringBoot集成Flowable实践
|
XML Java 程序员
真是没想到 Springboot + Flowable 开发工作流会这么简单
真是没想到 Springboot + Flowable 开发工作流会这么简单
391 0
真是没想到 Springboot + Flowable 开发工作流会这么简单
|
XML 存储 数据可视化
Flowable学习笔记(一、入门)
Flowable学习笔记(一、入门)
905 0
Flowable学习笔记(一、入门)
|
SQL 前端开发 JavaScript
JFinal上手demo(2)
JFinal上手demo
141 0
JFinal上手demo(2)
|
XML 前端开发 Java
JFinal上手demo(1)
JFinal上手demo
146 0
JFinal上手demo(1)