天天看点

SpringBoot 整合Activiti(一)

最近在学习Activiti工作流,学习过程整理下,方便以后回顾。

我这里用的Activiti版本是5.22,考虑后面应该会做在线绘制流程图,而6.0版本网上信息过少。。。(如下配置6.0也可试用)

开发工具使用Eclipse,Eclipse集成Activiti可参考:https://www.cnblogs.com/mingforyou/p/5347561.html

项目目录结构如下:

1、pom.xml配置SpringBoot和Activiti5.22

<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>workFlow</groupId>

    <artifactId>workFlow</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <properties>

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

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

    </properties>

    <parent>

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

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

        <version>1.5.8.RELEASE</version>

    </parent>

    <dependencies>

        <dependency>

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

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

        </dependency>

        <dependency>

            <groupId>org.activiti</groupId>

            <artifactId>activiti-spring-boot-starter-basic</artifactId>

            <version>5.22.0</version>

        </dependency>

        <dependency>

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

            <artifactId>spring-boot-starter-data-jpa</artifactId>

        </dependency>

        <dependency>

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

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

        </dependency>

        <dependency>

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

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

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

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

            <scope>runtime</scope>

        </dependency>

        <dependency>

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

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

        </dependency>

        <dependency>

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

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

        </dependency>

        <dependency>

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

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

            <scope>test</scope>

        </dependency>

        <dependency>

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

            <artifactId>spring-boot-devtools</artifactId>

            <optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

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

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

            </plugin>

        </plugins>

    </build>

</project> 

2、数据源配置(application.properties):

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activiti?characterEncoding=utf8&useSSL=true

spring.datasource.username=root

spring.datasource.password=root

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

spring.datasource.max-idle=10

spring.datasource.max-wait=10000

spring.datasource.min-idle=5

spring.datasource.initial-size=5

server.context-path=/workFlow

server.session.timeout=10

spring.http.encoding.force=true

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

server.tomcat.uri-encoding=UTF-8

//端口号

server.port=8081

3、绘制流程图如下:

右键以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:activiti="http://activiti.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.activiti.org/test">

  <process id="demo2" name="My process" isExecutable="true">

    <startEvent id="startevent1" name="Start"></startEvent>

    <userTask id="usertask1" name="请假"></userTask>

    <userTask id="usertask2" name="审批"></userTask>

    <endEvent id="endevent1" name="End"></endEvent>

    <sequenceFlow id="flow1" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>

    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>

    <sequenceFlow id="flow3" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>

  </process>

  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">

    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">

      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">

        <omgdc:Bounds height="35.0" width="35.0" x="130.0" y="180.0"></omgdc:Bounds>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">

        <omgdc:Bounds height="55.0" width="105.0" x="271.0" y="170.0"></omgdc:Bounds>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">

        <omgdc:Bounds height="55.0" width="105.0" x="471.0" y="170.0"></omgdc:Bounds>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">

        <omgdc:Bounds height="35.0" width="35.0" x="690.0" y="180.0"></omgdc:Bounds>

      </bpmndi:BPMNShape>

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

        <omgdi:waypoint x="576.0" y="197.0"></omgdi:waypoint>

        <omgdi:waypoint x="690.0" y="197.0"></omgdi:waypoint>

      </bpmndi:BPMNEdge>

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

        <omgdi:waypoint x="376.0" y="197.0"></omgdi:waypoint>

        <omgdi:waypoint x="471.0" y="197.0"></omgdi:waypoint>

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">

        <omgdi:waypoint x="165.0" y="197.0"></omgdi:waypoint>

        <omgdi:waypoint x="271.0" y="197.0"></omgdi:waypoint>

      </bpmndi:BPMNEdge>

    </bpmndi:BPMNPlane>

  </bpmndi:BPMNDiagram>

</definitions>

新创建的流程一定记得修改流程图id如下图,如若流程图id存在重复,则启动报错:

4、java代码:

SpringBoot会把常用的服务自动配置注入,使用时直接装配使用即可:

@SpringBootApplication

public class App {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(App.class, args);

    }

}

@RestController

@RequestMapping("/demo")

public class DemoController {

    @Autowired  

    private RepositoryService repositoryService; 

    @Autowired  

    private RuntimeService runtimeService;  

    @Autowired  

    private TaskService taskService;  

    @RequestMapping("/firstDemo")

    public void firstDemo() {

        //根据bpmn文件部署流程

        Deployment deployment = repositoryService.createDeployment().addClasspathResource("demo2.bpmn").deploy();

        //获取流程定义

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();

        //启动流程定义,返回流程实例

        ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());

        String processId = pi.getId();

        System.out.println("流程创建成功,当前流程实例ID:"+processId);

        Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();

        System.out.println("第一次执行前,任务名称:"+task.getName());

        taskService.complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId).singleResult();

        System.out.println("第二次执行前,任务名称:"+task.getName());

        taskService.complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId).singleResult();

        System.out.println("task为null,任务执行完毕:"+task);

    }

解决流程图汉字乱码问题:

@Component

public class ShareniuProcessEngineConfigurationConfigurer implements ProcessEngineConfigurationConfigurer  {

    public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {

        processEngineConfiguration.setActivityFontName("宋体");

        processEngineConfiguration.setLabelFontName("宋体");

        processEngineConfiguration.setAnnotationFontName("宋体");

        System.out.println("ShareniuProcessEngineConfigurationConfigurer#############");

        System.out.println(processEngineConfiguration.getActivityFontName());

    }

}

5、执行结果:

访问http://localhost:8081/workFlow/demo/firstDemo,控制台即可得到如下结果:

后面会继续更新Activiti相关内容。

继续阅读