全部开发者教程

企业级在线办公系统

本课程的emos-workflow项目是标准的SpringBoot工程,所以我们按照传统的方式来创建。注意项目的JDK版本,必须是大于1.8的。Activiti7.0不支持1.8的JDK。

图片描述

一、配置项目

1. 修改pom.xml文件

项目创建好之后,我们在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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>emos-workflow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>emos-workflow</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>15</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.4.2</spring-boot.version>
        <kie.version>7.51.0.Final</kie.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.14</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.13</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <version>7.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.13</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>${kie.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>${kie.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-templates</artifactId>
            <version>${kie.version}</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-spring</artifactId>
            <version>${kie.version}</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-internal</artifactId>
            <version>${kie.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-image-generator</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>cos_api</artifactId>
            <version>5.6.38</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.emos.workflow.EmosWorkflowApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

因为Activiti工作流程的数据是要持久化的,所以我在pom.xml文件中导入了MySQL的JDBC库。下面两个依赖库是Activiti用到的。

<!--Activiti主库-->
<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-spring-boot-starter</artifactId>
    <version>7.0.0.GA</version>
</dependency>
<!--Activiti生成动态BPMN图片的库-->
<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-image-generator</artifactId>
    <version>5.22.0</version>
</dependency>

2. 修改配置文件

application.properties文件改成yml后缀名,然后填写如下的配置:

server:
  jetty:
    threads:
      acceptors: 4
      selectors: 8
  port: 9090
  servlet:
    context-path: /emos-workflow
    multipart:
      max-request-size: 10MB
      max-file-size: 2MB

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://数据库IP:3306/emos?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
      username: admin
      password: abc123456
      initial-size: 2
      max-active: 4
      min-idle: 4
      max-wait: 60000
      test-while-idle: false
      test-on-borrow: true
      test-on-return: false
  redis:
    database: 0
    host: localhost
    port: 6379
    password: abc123456
    jedis:
      pool:
        max-active: 1000
        max-wait: -1ms
        max-idle: 16
        min-idle: 8
  activiti:
    database-schema-update: true
    historyLevel: audit
    db-history-used: true
  quartz:
    job-store-type: jdbc
  mail:
    default-encoding: UTF-8
    host: smtp.aliyun.com
    username: 阿里云邮箱用户名
    password: 阿里云邮箱密码

mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.example.emos.workflow.db.pojo
  configuration:
    map-underscore-to-camel-case: true

logging:
  level:
    root: info
    com.example.demo.workflow.db.dao : error
  pattern:
    console: "%d{HH:mm:ss}  %-5level  %msg%n"

3. 导入SQL文件

按理说我们应该在MySQL里面创建逻辑库,然后导入Activiti的SQL文件,但是本课程的emos逻辑库里面已经包含了工作流用到的数据表,所以就不用再创建了。这些数据表都是act_开头的。

4. 运行项目

EmosWorkflowApplication类中,我们添加两个注解,分别是:异步线程和日志注解。这两个注解跟工作流关系不大,但是在项目的具体业务中会用到。

@SpringBootApplication
@EnableAsync
@Slf4j
public class EmosWorkflowApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmosWorkflowApplication.class, args);
    }

}

如果项目启动没有任何报错信息,说明工作流项目就已经搭建成功了。

二、解决BPMN乱码

在本课程中,我们在Web页面上看到的动态BPMN图片是由Activiti引擎生成的,刚才我们导入的依赖中还有依赖库。如果Activiti项目在Windows上面运行的时候,生成的BPMN图片里面中文是正常的;但是换做Linux系统就出问题了,生成的BPMN图片中文部分全都是乱码。这是因为Linux系统中没有Activiti需要的中文字体,所有才会出现乱码。

图片描述
有的同学觉得在Linux系统上面安装上相关的中文字体不就行了么,可以,但是太麻烦了。Linux有那么多发行版,每种发行版安装字体的命令你是不是都得知道,这无形给部署项目增加了压力。所以我的做法是把中文字体放在Java项目中,以后直接打包进JAR文件里面。Activiti运行的时候,引用项目中的字体,不去使用操作系统字体,于是就不会有乱码出现。

首先你到百度上面下载simsun.ttc文件,这是中文宋体文件。然后你把该文件放在项目的resources之下的font文件夹中。

接下来我们要修改主类代码,在SpringBoot项目启动的时候就加载这个宋体文件。重新启动项目,如果没有异常信息,说明该字体已经加载到项目中了。
@SpringBootApplication
@EnableAsync
@Slf4j
public class EmosWorkflowApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(EmosWorkflowApplication.class, args);
        Resource resource = ctx.getResource("classpath:font/simsun.ttc");

        try (InputStream in = resource.getInputStream()) {
            Font font = Font.createFont(Font.TRUETYPE_FONT, in);
            GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
        } catch (Exception e) {
            log.error("执行异常", e);
        }
    }
}