溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot2.0.1與flowable工作流引擎如何整合

發(fā)布時(shí)間:2021-11-25 09:20:33 來源:億速云 閱讀:153 作者:小新 欄目:編程語言

這篇文章主要介紹了SpringBoot2.0.1與flowable工作流引擎如何整合,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1.準(zhǔn)備依賴,pom.xml文件如下

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

<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>activiti.demo</groupId>

    <artifactId>activiti-demo</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <name>activiti-demo</name>

    <description>spring-activiti-demo</description>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.1.RELEASE</version>

        <relativePath/>

    </parent>

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>

    </properties>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.flowable</groupId>

            <artifactId>flowable-spring-boot-starter</artifactId>

            <version>6.3.0</version>

        </dependency>

        <!--MySQL 驅(qū)動(dòng)包,如果是其他庫的話需要換驅(qū)動(dòng)包-->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.35</version>

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

</project>

-----------------------------------------------------------------------------------------------------------------------

2.準(zhǔn)備流程文件,ExpenseProcess.bpmn20.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:flowable="http://flowable.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.flowable.org/processdef">

    <process id="Expense" name="ExpenseProcess" isExecutable="true">

        <documentation>報(bào)銷流程</documentation>

        <startEvent id="start" name="開始"></startEvent>

        <userTask id="fillTask" name="出差報(bào)銷" flowable:assignee="${taskUser}">

            <extensionElements>

                <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler">

                    <![CDATA[false]]></modeler:initiator-can-complete>

            </extensionElements>

        </userTask>

        <exclusiveGateway id="judgeTask"></exclusiveGateway>

        <userTask id="directorTak" name="經(jīng)理審批">

            <extensionElements>

                <flowable:taskListener event="create"

                                       class="com.springboot.demo.handler.ManagerTaskHandler"></flowable:taskListener>

            </extensionElements>

        </userTask>

        <userTask id="bossTask" name="老板審批">

            <extensionElements>

                <flowable:taskListener event="create"

                                       class="com.springboot.demo.handler.BossTaskHandler"></flowable:taskListener>

            </extensionElements>

        </userTask>

        <endEvent id="end" name="結(jié)束"></endEvent>

        <sequenceFlow id="directorNotPassFlow" name="駁回" sourceRef="directorTak" targetRef="fillTask">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='駁回'}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="bossNotPassFlow" name="駁回" sourceRef="bossTask" targetRef="fillTask">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='駁回'}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="flow1" sourceRef="start" targetRef="fillTask"></sequenceFlow>

        <sequenceFlow id="flow2" sourceRef="fillTask" targetRef="judgeTask"></sequenceFlow>

        <sequenceFlow id="judgeMore" name="大于500元" sourceRef="judgeTask" targetRef="bossTask">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${money > 500}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="bossPassFlow" name="通過" sourceRef="bossTask" targetRef="end">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='通過'}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="directorPassFlow" name="通過" sourceRef="directorTak" targetRef="end">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='通過'}]]></conditionExpression>

        </sequenceFlow>

        <sequenceFlow id="judgeLess" name="小于500元" sourceRef="judgeTask" targetRef="directorTak">

            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${money <= 500}]]></conditionExpression>

        </sequenceFlow>

    </process>

    <bpmndi:BPMNDiagram id="BPMNDiagram_Expense">

        <bpmndi:BPMNPlane bpmnElement="Expense" id="BPMNPlane_Expense">

            <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">

                <omgdc:Bounds height="30.0" width="30.0" x="285.0" y="135.0"></omgdc:Bounds>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="fillTask" id="BPMNShape_fillTask">

                <omgdc:Bounds height="80.0" width="100.0" x="405.0" y="110.0"></omgdc:Bounds>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="judgeTask" id="BPMNShape_judgeTask">

                <omgdc:Bounds height="40.0" width="40.0" x="585.0" y="130.0"></omgdc:Bounds>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="directorTak" id="BPMNShape_directorTak">

                <omgdc:Bounds height="80.0" width="100.0" x="735.0" y="110.0"></omgdc:Bounds>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="bossTask" id="BPMNShape_bossTask">

                <omgdc:Bounds height="80.0" width="100.0" x="555.0" y="255.0"></omgdc:Bounds>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">

                <omgdc:Bounds height="28.0" width="28.0" x="771.0" y="281.0"></omgdc:Bounds>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">

                <omgdi:waypoint x="315.0" y="150.0"></omgdi:waypoint>

                <omgdi:waypoint x="405.0" y="150.0"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">

                <omgdi:waypoint x="505.0" y="150.16611295681062"></omgdi:waypoint>

                <omgdi:waypoint x="585.4333333333333" y="150.43333333333334"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="judgeLess" id="BPMNEdge_judgeLess">

                <omgdi:waypoint x="624.5530726256983" y="150.44692737430168"></omgdi:waypoint>

                <omgdi:waypoint x="735.0" y="150.1392757660167"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="directorNotPassFlow" id="BPMNEdge_directorNotPassFlow">

                <omgdi:waypoint x="785.0" y="110.0"></omgdi:waypoint>

                <omgdi:waypoint x="785.0" y="37.0"></omgdi:waypoint>

                <omgdi:waypoint x="455.0" y="37.0"></omgdi:waypoint>

                <omgdi:waypoint x="455.0" y="110.0"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="bossPassFlow" id="BPMNEdge_bossPassFlow">

                <omgdi:waypoint x="655.0" y="295.0"></omgdi:waypoint>

                <omgdi:waypoint x="771.0" y="295.0"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="judgeMore" id="BPMNEdge_judgeMore">

                <omgdi:waypoint x="605.4340277777778" y="169.56597222222223"></omgdi:waypoint>

                <omgdi:waypoint x="605.1384083044983" y="255.0"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="directorPassFlow" id="BPMNEdge_directorPassFlow">

                <omgdi:waypoint x="785.0" y="190.0"></omgdi:waypoint>

                <omgdi:waypoint x="785.0" y="281.0"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="bossNotPassFlow" id="BPMNEdge_bossNotPassFlow">

                <omgdi:waypoint x="555.0" y="295.0"></omgdi:waypoint>

                <omgdi:waypoint x="455.0" y="295.0"></omgdi:waypoint>

                <omgdi:waypoint x="455.0" y="190.0"></omgdi:waypoint>

            </bpmndi:BPMNEdge>

        </bpmndi:BPMNPlane>

    </bpmndi:BPMNDiagram>

</definitions>

-----------------------------------------------------------------------------------------------------------------------

3.準(zhǔn)備SpringBoot屬性文件,application.properties內(nèi)容如下

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xh_activiti?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=123456

spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.jpa.show-sql=true

server.port=8082

server.tomcat.uri-encoding=UTF-8

flowable.async-executor-activate=false

-----------------------------------------------------------------------------------------------------------------------

4.編寫流程中用到的處理類

包名:com.springboot.demo.handler

類名:ManagerTaskHandler

類內(nèi)容如下

package com.springboot.demo.handler;

import org.flowable.engine.delegate.TaskListener;

import org.flowable.task.service.delegate.DelegateTask;

/**

 * @ClassName ManagerTaskHandler

 * @Description TODO

 * @Author yunshuodeng

 * @Date 2019-05-05 09:53

 * @Version 1.0

 **/

public class ManagerTaskHandler implements TaskListener {

    @Override

    public void notify(DelegateTask delegateTask) {

        delegateTask.setAssignee("經(jīng)理");

    }

}

包名:com.springboot.demo.handler

類名:BossTaskHandler

類內(nèi)容如下

package com.springboot.demo.handler;

import org.flowable.engine.delegate.TaskListener;

import org.flowable.task.service.delegate.DelegateTask;

/**

 * @ClassName BossTaskHandler

 * @Description TODO

 * @Author yunshuodeng

 * @Date 2019-05-05 09:54

 * @Version 1.0

 **/

public class BossTaskHandler implements TaskListener {

    @Override

    public void notify(DelegateTask delegateTask) {

        delegateTask.setAssignee("老板");

    }

}

-----------------------------------------------------------------------------------------------------------------------

5.編寫控制器

包名:com.springboot.demo.controller

類名:ExpenseController

類內(nèi)容如下:

package com.springboot.demo.controller;

import org.flowable.bpmn.model.BpmnModel;

import org.flowable.engine.*;

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.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

/**

 * @ClassName ExpenseController

 * @Description TODO

 * @Author yunshuodeng

 * @Date 2019-05-05 09:57

 * @Version 1.0

 **/

@Controller

@RequestMapping(value = "expense")

public class ExpenseController {

    @Autowired

    private RuntimeService runtimeService;

    @Autowired

    private TaskService taskService;

    @Autowired

    private RepositoryService repositoryService;

    @Autowired

    private ProcessEngine processEngine;

    /**

     * 生成當(dāng)前流程圖

     * @param httpServletResponse

     * @param processId

     */

    @RequestMapping(value = "processDiagram")

    public void genProcessDiagram(HttpServletResponse httpServletResponse,String processId){

        try {

            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();

            if (StringUtils.isEmpty(processInstance)){

                return;

            }

            Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

            String instanceId = task.getProcessInstanceId();

            List<Execution> executionList = runtimeService.createExecutionQuery().processInstanceId(instanceId).list();

            List<String> activityIdList = new ArrayList<>();

            List<String> flowList = new ArrayList<>();

            for (Execution execution : executionList){

                List<String> idList = runtimeService.getActiveActivityIds(execution.getId());

                activityIdList.addAll(idList);

            }

            BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());

            ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();

            ProcessDiagramGenerator processDiagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();

            InputStream inputStream = processDiagramGenerator.generateDiagram(bpmnModel,"png",activityIdList,flowList,

                    processEngineConfiguration.getActivityFontName(),

                    processEngineConfiguration.getLabelFontName(),

                    processEngineConfiguration.getAnnotationFontName(),

                    processEngineConfiguration.getClassLoader(),

                    1.0);

            OutputStream out = null;

            byte[] buf = new byte[1024];

            int legth = 0;

            try {

                out = httpServletResponse.getOutputStream();

                while ((legth = inputStream.read(buf)) != -1) {

                    out.write(buf, 0, legth);

                }

            } finally {

                if (inputStream != null) {

                    inputStream.close();

                }

                if (out != null) {

                    out.close();

                }

            }

        }catch (Exception e){

            System.out.println(e.getMessage());

        }

    }

    /**

     * 審核-拒絕

     * @param taskId

     * @return

     */

    @RequestMapping(value = "reject")

    @ResponseBody

    public String reject(String taskId){

        HashMap<String,Object> map = new HashMap<>();

        map.put("outcome","駁回");

        taskService.complete(taskId,map);

        return "reject";

    }

    /**

     * 審核-通過

     * @param taskId

     * @return

     */

    @RequestMapping(value = "apply")

    @ResponseBody

    public String apply(String taskId){

        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();

        if (StringUtils.isEmpty(task)){

            throw new RuntimeException("流程不存在");

        }

        HashMap<String,Object> map = new HashMap<>();

        map.put("outcome","通過");

        taskService.complete(taskId,map);

        return "processed ok";

    }

    /**

     * 獲取審批列表

     * @param userId

     * @return

     */

    @RequestMapping(value = "list")

    @ResponseBody

    public Object list(String userId){

        String temp = "";

        List<Task> taskList = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();

        for (Task task : taskList){

            temp += task.toString();

            System.out.println(task.toString());

        }

        return temp;

    }

    /**

     * 開始報(bào)銷流程

     * @param userId 用戶ID

     * @param money 報(bào)銷金額

     * @param description 描述

     * @return

     */

    @RequestMapping(value = "add")

    @ResponseBody

    public String addExpense(String userId,Integer money,String description){

        HashMap<String,Object> map = new HashMap<>();

        map.put("taskUser",userId);

        map.put("money",money);

        map.put("description",description);

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Expense",map);

        return "提交成功,流程ID為"+processInstance.getId();

    }

}

-----------------------------------------------------------------------------------------------------------------------

6.啟動(dòng)服務(wù),沒有報(bào)錯(cuò)即可

7.分別訪問如下地址進(jìn)行流程的執(zhí)行操作

1)提交報(bào)銷申請(qǐng)

請(qǐng)求地址:http://localhost:8082/expense/add?userId=4&money=2000

返回結(jié)果:提交成功,流程ID為5043

說明:

http://localhost本地服務(wù)

8082表示本地服務(wù)對(duì)外提供訪問端口

expense表示控制器名稱

add表示控制器中的方法,該方法用于開始報(bào)銷流程,也就是提交報(bào)銷申請(qǐng)

userId=4表示用戶ID為4

money=2000表示報(bào)銷金額為2000

2)查看待辦列表

請(qǐng)求地址:http://localhost:8082/expense/list?userId=4

返回結(jié)果:Task[id=5050, name=出差報(bào)銷]

說明:

http://localhost本地服務(wù)

8082表示本地服務(wù)對(duì)外提供訪問端口

expense表示控制器名稱

list表示獲取待辦列表方法名

userId=4表示用戶ID為4,獲取用戶ID為4的待辦事項(xiàng)列表

3)對(duì)報(bào)銷申請(qǐng)進(jìn)行審核,且審核結(jié)果為通過

請(qǐng)求地址:http://localhost:8082/expense/apply?taskId=5050

返回結(jié)果:processed ok

說明:

http://localhost本地服務(wù)

8082表示本地服務(wù)對(duì)外提供訪問端口

expense表示控制器名稱

apply表示審核通過方法

taskId表示任務(wù)ID,些任務(wù)ID是查看待辦列表中的某個(gè)任務(wù)ID

4)生成當(dāng)前任務(wù)狀態(tài)圖

請(qǐng)求地址:http://localhost:8082/expense/processDiagram?processId=5043

返回結(jié)果:在瀏覽器中一張圖片

說明:

http://localhost本地服務(wù)

8082表示本地服務(wù)對(duì)外提供訪問端口

expense表示控制器名稱

processDiagram表示生成圖片方法

processId=5043表示流程ID,該流程ID是第1步提交報(bào)銷申請(qǐng)時(shí)返回的流程ID

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringBoot2.0.1與flowable工作流引擎如何整合”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI