Spring boot 2.0 + activiti 7 integration (2) — running a simple process

Time:2019-12-24

According to the previous article, springboot2.0 + activiti 7 integration (1) – get to know activiti and create database operation, and have created the database of activiti; next, run a simple official example. Official demo
To run an activiti demo, you need at least three parts:

  • API + springboot of activiti
  • Activiti’s database
  • BPMN file

1. View the task table of the database first

View the act Ru task table
The newly created database has no task.
Spring boot 2.0 + activiti 7 integration (2) -- running a simple process

2. Create BPMN file

In the resources directory, create a processes folder, and then create a bpnm file(one-task-process.bpmn20.xml) Be careful:(activiti supports bpmn2.0, and all BPMN file names must end with *. Bpmn20. XML)Because the default BPMN file of the paint tool plug-in of idea is the end of BPMN.

<?xml version="1.0" encoding="UTF-8"?>
<definitions
        xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
        xmlns:activiti="http://activiti.org/bpmn"
        targetNamespace="Examples">

    <process id="oneTaskProcess" name="The One Task Process">
        <startEvent id="theStart" />
        <sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
        <userTask id="theTask" name="my task" />
        <sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
        <endEvent id="theEnd" />
    </process>

</definitions>

3. Configure application.yml

Before the database was created, the version of MySQL was reduced. Now the database is created, and later calls can use the version of MySQL in the development process. I have upgraded the MySQL version here, using the
com.mysql.cj.jdbc.Driver

spring:
  datasource:
    #Basic configuration of data source
    username: root
    password: root
    url: jdbc:mysql://localhost/activiti?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&nullCatalogMeansCurrent=true&useSSL=false&useLegacyDatetimeCode=false
    driver-class-name: com.mysql.cj.jdbc.Driver

4. Create test class

The official document is written in the main function to run. I am used to using test classes:

package com.example.activitidemo2;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
class ActivitiDemo2ApplicationTests {

    @Resource
    RepositoryService repositoryService;

    @Resource
    RuntimeService runtimeService;

    @Resource
    TaskService taskService;

    @Test
    void contextLoads() {
        System.out.println("Number of process definitions : "
                + repositoryService.createProcessDefinitionQuery().count());
        System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
        runtimeService.startProcessInstanceByKey("oneTaskProcess");
        System.out.println("Number of tasks after process start: " + taskService.createTaskQuery().count());
    }

}

The operation results shall be as follows:

Number of process definitions : 1
Number of tasks : 0
Number of tasks after process start: 1

5. View the database again

View the act Ru task table
Spring boot 2.0 + activiti 7 integration (2) -- running a simple process
Run the test program several times, the number of tasks will continue to increase.

This process starts, and then we start to draw our own business process diagram and run it.