1.准备依赖,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 驱动包,如果是其他库的话需要换驱动包-->

        <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.准备流程文件,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>报销流程</documentation>

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

        <userTask id="fillTask" name="出差报销" 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="经理审批">

            <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="结束"></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.准备SpringBoot属性文件,application.properties内容如下

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

类内容如下

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("经理");

    }

}

包名:com.springboot.demo.handler

类名:BossTaskHandler

类内容如下

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

类内容如下:

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;

    /**

     * 生成当前流程图

     * @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;

    }

    /**

     * 开始报销流程

     * @param userId 用户ID

     * @param money 报销金额

     * @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.启动服务,没有报错即可

7.分别访问如下地址进行流程的执行操作

1)提交报销申请

请求地址:http://localhost:8082/expense/add?userId=4&money=2000

返回结果:提交成功,流程ID为5043

说明:

http://localhost本地服务

8082表示本地服务对外提供访问端口

expense表示控制器名称

add表示控制器中的方法,该方法用于开始报销流程,也就是提交报销申请

userId=4表示用户ID为4

money=2000表示报销金额为2000

2)查看待办列表

请求地址:http://localhost:8082/expense/list?userId=4

返回结果:Task[id=5050, name=出差报销]

说明:

http://localhost本地服务

8082表示本地服务对外提供访问端口

expense表示控制器名称

list表示获取待办列表方法名

userId=4表示用户ID为4,获取用户ID为4的待办事项列表

3)对报销申请进行审核,且审核结果为通过

请求地址:http://localhost:8082/expense/apply?taskId=5050

返回结果:processed ok

说明:

http://localhost本地服务

8082表示本地服务对外提供访问端口

expense表示控制器名称

apply表示审核通过方法

taskId表示任务ID,些任务ID是查看待办列表中的某个任务ID

4)生成当前任务状态图

请求地址:http://localhost:8082/expense/processDiagram?processId=5043

返回结果:在浏览器中一张图片

说明:

http://localhost本地服务

8082表示本地服务对外提供访问端口

expense表示控制器名称

processDiagram表示生成图片方法

processId=5043表示流程ID,该流程ID是第1步提交报销申请时返回的流程ID