Getting started with SpringBoot 14, adding Activiti5.22 workflow

Keywords: Java Spring SpringBoot Database MyEclipse

For the basic configuration of the project, please refer to the introduction to Springboot I. use myEclipse to create a new Springboot project and myEclipse to create a new Springboot project. Now let's add an Activiti5.22 support to the project. Spring boot uses version 2.0.7 (later, trying to upgrade to 2.1.1 and 2.2.1 is a failure). The details are as follows:

  1. pom.xml adds the following configuration information
<!-- 1.Set up Springboot Edition -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.7.RELEASE</version>
    <relativePath />
</parent>
<! -- 2. Set common parameters -- >
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <! -- when Maven install, if the Chinese output in the Test environment @ Test is garbled, add this sentence and try -- >
    <argLine>-Dfile.encoding=UTF-8</argLine>
    <! -- set the activity version to be integrated -- >
    <activiti.version>5.22.0</activiti.version>   
</properties>
<!-- 3.Introduce Activiti Support -->
<dependencies>
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring-boot-starter-basic</artifactId>
        <version>${activiti.version}</version>
    </dependency>
</dependencies>
  1. Add the following configuration information to the configuration file
# Set up the activity table automatically when the system starts (this setting is not required in the version of Springboot1.x)
# Always is to always perform initialization, embedded only initializes the memory database (default value), such as h2, and never is not to perform initialization
spring.datasource.initialization-mode=always

# Activiti configuration
## Automatically build activity database table, set value="true", detect when starting, no table creation, skip with table; set value = "drop create"
## Create a new table every time you start (it takes effect in Springboot1.x, and it doesn't take effect in Springboot2.x, so it is closed)
#spring.activiti.database-schema-update=true
## Activiti auto deployment verification settings: true - on (default), false - off
spring.activiti.check-process-definitions=false
## Setting true will replace those old job executors
spring.activiti.async-executor-enabled=false
## Activiti timing scanning task, default: true (on), it is recommended to turn it off if it is not used, and it needs to be used with spring. Activiti. Async executor enabled = false, otherwise it will not work
spring.activiti.job-executor-activate=false

3. Create process file
Place the created file in the src/main/resources directory
SpringBoot,Activiti5.22

4. Create a test service

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ActivitiTestService {
    @Autowired  
    private RepositoryService repositoryService; 
    @Autowired  
    private RuntimeService runtimeService;  
    @Autowired  
    private TaskService taskService; 

    /**
     * @Function Description: process test
     *
     * @param flowType 
     */
    public void test(String flowType){
        //Deployment process, as long as it is an XML file conforming to BPMN 2 specification, can be deployed by ACTIVITI in theory
        Deployment deployment = repositoryService.createDeployment().name("Test process I").addClasspathResource("testProcess.bpmn").addClasspathResource("testProcess.png").deploy();
        //Get process definition
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        //Start process definition and return to process instance
        ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());
        String processId = pi.getId();
        System.out.println("Process created successfully, current process instance ID: "+processId);

        Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("Before the first execution, task name:"+task.getName());
        taskService.complete(task.getId());

        int count = 2;
        while(true){
            task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
            if (null == task) {
                System.out.println("task by null,Task completed:"+task);
                break;
            } else {
                System.out.println("The first"+count+++"Before execution, task name:"+task.getName());
                taskService.complete(task.getId());
            }
        }
    }
}

5. Create a test controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qfx.common.bean.MessageBean;
import com.qfx.common.controller.BaseController;
import com.qfx.workflow.service.ActivitiTestService;
import com.qfx.workflow.service.ModelService;

@Controller
@RequestMapping("activiti")
public class ActivitiController extends BaseController {

    @Autowired
    ActivitiTestService activitiService;

    @Autowired
    ModelService modelService;

    @RequestMapping("test")
    public String test(String flowType){
        // Process testing
        activitiService.test(flowType);

        MessageBean messageBean = new MessageBean();
        messageBean.setMessage("The test process is finished,See background output!");
        request.setAttribute("messageBean", messageBean);

        return "../index";
    }
}

6. Test call. The console shows that the call is successful
SpringBoot,Activiti5.22

Posted by barbs75 on Sat, 07 Dec 2019 02:52:56 -0800