SpringBoot integrated online Activiti Designer

table of Contents

 

Preface:

Source:

step:

(1) Create a new SpringBoot project, introduced Activti5.22.0 jar file, generate 23 Table workflow

(2) introducing the desired file 5.22.0 Activiti

(3) to start the project, the designer of the online access Activiti5.22.0 on the browser page 

Note (4) of the draw flowcharts

(5) create a leave of absence process

(6) leave the process running the query

(6) leave this review process

(7) to query the current state of the process leave

Precautions:

Reference article:


Preface:

Xiao Bian to do this project has some of the features used in the Activiti workflow, the current level of mastery of the basic workflow will use, I wrote before Activiti introductory chapter , the current article is part of Activiti real part, if you want to Activiti workflow is to know how to use, then it is as small series read on with it!

Source:

I will put all source network disk inside, welcome to download.

Link: https: //pan.baidu.com/s/18TentJlrLNflNAcgJDnAtQ 
extraction code: 9q19

step:

(1) Create a new project SpringBoot introduced Activti5.22.0 jar package generates 23 tables workflow

  • 1.1 pom file
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>activiti-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>activiti-demo</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <!--Spring data JPA-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- MySQL start -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

        <!-- spring boot start -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- activiti start -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>5.22.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>5.22.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-explorer</artifactId>
            <version>5.22.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-simple-workflow</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-codec</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-css</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-svg-dom</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-svggen</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- activiit end-->
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 1.2 introduction of generation of database files,
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!--配置流程引擎配置对象-->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
        <property name="jdbcDriver"  value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://192.168.20.81:3306/activiti_demo"/>
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="root" />
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
</beans>
  • 1.3 start this project, you can generate a list workflows

As shown below, I also created a the activiti_demo database 

New 23 table, the data will be added to the three in act_ge_property

(2) introducing the desired file 5.22.0 Activiti

  • 2.1 the project path as shown below:

   Under static folder is a static resource files of activiti5.22.0 

  • Interface 2.2 workflow model created
package com.example.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 工作流前端化的controller(新建)
 *
 * @Author: tanghh
 */

@RequestMapping("/activiti")
@RestController
public class ActivitiController {

    private static Logger logger=LoggerFactory.getLogger(ActivitiController.class);
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ObjectMapper objectMapper;



    @ResponseBody
    public String newMode() {
        return "spring-boot";
    }



    /**
     * 根据Model部署流程
     */
    @PostMapping(value = "deploy/{modelId}")
    public void deploy(@PathVariable("modelId") String modelId) {
        try {
            Model modelData = repositoryService.getModel(modelId);
            ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
            byte[] bpmnBytes = null;

            BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
            bpmnBytes = new BpmnXMLConverter().convertToXML(model);

            String processName = modelData.getName() + ".bpmn20.xml";
            List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionResourceName(processName).list();
            for (ProcessDefinition definition:list){
                //删除原有流程定义,正在使用的流程定义无法删除
                repositoryService.deleteDeployment(definition.getDeploymentId());
            }
            Deployment deployment = repositoryService.createDeployment().name(modelData.getName()).addString(processName, new String(bpmnBytes)).deploy();
            System.out.println("流程部署id----"+deployment.getId());
        } catch (Exception e) {
            logger.error("根据模型部署流程失败:modelId={}", modelId, e);
        }
    }

    /**
     * 新建模型
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/create")
    public ModelAndView newModel() throws UnsupportedEncodingException {
//    	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//    	RepositoryService repositoryService = processEngine.getRepositoryService();
//    	ObjectMapper objectMapper = new ObjectMapper();
        //初始化一个空模型
        Model model = repositoryService.newModel();

        //设置一些默认信息,可以用参数接收
        int revision = 1;
        String key = "process";
        String name = "process";
        String description = "新建流程模型";

        //ModelEditorSource
        ObjectNode editorNode = objectMapper.createObjectNode();
        editorNode.put("id", "canvas");
        editorNode.put("resourceId", "canvas");
        ObjectNode stencilSetNode = objectMapper.createObjectNode();
        stencilSetNode.put("namespace","http://b3mn.org/stencilset/bpmn2.0#");
        editorNode.put("stencilset" , stencilSetNode);


        ObjectNode modelNode = objectMapper.createObjectNode();
        modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);
        modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
        modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);

        model.setName(name);
        model.setKey(key);
        model.setMetaInfo(modelNode.toString());

        repositoryService.saveModel(model);

        String id = model.getId();

        repositoryService.addModelEditorSource(id, editorNode.toString().getBytes("utf-8"));
        return new ModelAndView("redirect:/modeler.html?modelId=" + id);
    }
}

(3) to start the project, the designer of the online access Activiti5.22.0 on the browser page 

 Access this path in the browser:  HTTP: // localhost: 8005 / activiti / the Create

  

Note (4) of the draw flowcharts

  • Designer 4.1 as this is generally the case can drag and drop, as shown below me is the mouse onto the start of the event, onto the right side of the interface

 

 

  • 4.2 Click on the head I marked, you can create a box to initiate the process leave as I left that kind of 

 

  • 4.3 draw flowcharts time when the mouse arrow to align the red dots, do not leave gaps, or create a process instance will complain

Arrow is best can be more drag, or will report the following error. 

 

  • 4.4 How to give a task a name, can be noted in the figure below where I fill in

 

  • 4.5 definition of a reviewer

  •  4.6 both a jump condition (dismissed and agreed)

 

  • 4.7 The final draw out the process leave the figure below:

Xiao Bian painting is not ye good-looking, you can also follow your favorite tune

  •  Click Save this flow 4.8

Click on the icon out of the interface:

  • 4.9 of a point to note: 

Note: 1 This is the process id is unique, behind us create a process instance to be created based on this process id.

 

 

  •  4.10 to create a process to add data

Create a model of the time, will add data to a act_re_model in

act_ge_ bytearray added data

 

(5) create a leave of absence process

  • 5.1 Creating a leave of absence process instance:

Code:

TestActivitiController 类
    @Autowired
    private ActivitiService activitiService;
    /**
     * 1.创建一个流程实例
     */
    @GetMapping(value = "/createLeaveFlow")
    public void createLeaveFlow() throws IOException {
        //1.举个例子,soup_tang 需要请假 ,我在页面上添加请假申请,任务申请人是soup_tang,
        // 我选择审核人为汤总,请汤总给我审核这个请假流程。
        String checkPeople = "汤总";
        activitiService.createLeaveWorkFlow(checkPeople);
    }

 

ActivitiService 类
 @Override
    public void createLeaveWorkFlow(String checkPeople) throws IOException {
            //先判断这个流程文件有没有部署(只需部署一次)
            Model modelData = repositoryService.getModel("1");
            ObjectNode modelNode = (ObjectNode) new ObjectMapper()
                    .readTree(repositoryService.getModelEditorSource(modelData.getId()));
            byte[] bpmnBytes = null;
            BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
            bpmnBytes = new BpmnXMLConverter().convertToXML(bpmnModel);

            String processName = modelData.getName() + ".bpmn20.xml";

            Deployment deployment = repositoryService.createDeployment()
                    .name(modelData.getName()).addString(processName, new String(bpmnBytes, "UTF-8"))
                    .deploy();
            //获取流程定义
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
            //启动流程定义,返回流程实例
            Map<String,Object> map = new HashMap<>();
            map.put("checkPeople",checkPeople);
            ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId(),map);
            System.out.println("流程定义的ID:" + pi.getProcessDefinitionId());
            System.out.println("流程实例的ID:" + pi.getId());

    }

Access browser http: // localhost: 8005 / createLeaveFlow

It will generate data to the table in the running

Parameters are running 

 

(6) leave the process running the query

Code:

TestActivitiController

    /**
     * 3.查询正在运行的实例
     */
    @GetMapping(value = "/listLeaveFlow")
    public void listLeaveFlow()  {
        activitiService.queryExcution();
    }

ActivitiService

  /**
     * 查询正在运行的实例
     */
    @Override
    public void queryExcution() {
        //创建正在执行的流程查询对象
        List<Execution> executionList = runtimeService.createExecutionQuery()
                .list();  //查询出集合
        for(Execution execution: executionList){
            System.out.println("正在执行的流程对象的id: "+execution.getId());
            System.out.println("所属流程实例的id:"+execution.getProcessInstanceId());
            System.out.println("正在活动的节点的id: "+execution.getActivityId());
        }
    }

Access the browser: HTTP: // localhost: 8005 / listLeaveFlow

The more important is that this process instance id 2507 I will later use this parameter.

 

(6) leave this review process

Code:

TestActivitiController类
    /**
     * 2.执行完 第一个方法的创建流程以后,就要执行下面这个审核方法
     * @throws IOException
     */
    @GetMapping(value = "/assginLeaveFlow")
    public void assginLeaveFlow()  {
        //1.当汤总 登录系统的时候,他需要审核一些 审核人为他的 请假流程,当然了,我们在创建这个请假流程的时候,
        //我这个审核人就是可以动态赋值的
        //2.这个是流程实例id ,每次创建一个工作流,都会生成,在具体的生产环境中我们需要将这个流程实例id保存起来。(2507 可能需要替换喔 切记。)
        String processInstanceId = "2507";
        //3.这个是审核人是否同意当前这个流程。(0代表同意 1代表不同意)
        int isAcceppt = 0;
        //4.这个是当前审核人
        String checkPeople = "汤总";
        activitiService.assginLeaveWorkFlow(processInstanceId,isAcceppt,checkPeople);
    }

 

 ActivitiService class

 @Override
    public void assginLeaveWorkFlow(String processInstanceId,int isAccept,String checkPeople) {
        Map<String,Object> map = new HashMap<>();
        //得到当前实例下的task
        Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
        taskService.addComment(task.getId(), processInstanceId, "审核人是否同意该请假流程");
        if(isAccept == 0){
            System.out.println("审核人同意了该请求");
            map.put("accept","同意");
            task.setAssignee(checkPeople);
            map.put("checkPeople", checkPeople);
        }else{
            System.out.println("审核人驳回了该请求");
            map.put("accept","驳回");
            //审核驳回后
            task.setAssignee("soup_tang");
            map.put("auditor","soup_tang");
        }
        task.setDescription("请假的描述信息");
        //执行当前这个工作流任务
        taskService.saveTask(task);
        taskService.complete(task.getId(), map);
    }

Access the browser: HTTP: // localhost: 8005 / assginLeaveFlow

 

This time we can verify that the current process is already executed, let's look at the implementation process execution status inquiries.

(7) to query the current state of the process leave

Code:

TestActiviController

   /**
     * 3.查询正在运行的实例
     */
    @GetMapping(value = "/getLeaveFlowByProccessInstanceId")
    public void getLeaveFlowByProccessInstanceId()  {
        String proccessInstanceId = "2507";
        activitiService.queryProccessInstanceState(proccessInstanceId);
    }

ActivitiService

 

  @Override
    public void  queryProccessInstanceState(String proccessInstanceId) {
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(proccessInstanceId).singleResult();
        if (processInstance == null) {
            System.out.println("当前流程已经完成");
        } else {
            System.out.println("当前流程实例ID:" + processInstance.getId());
            System.out.println("当前流程所处的位置:" + processInstance.getActivityId());
        }
    }

Access the browser: HTTP: // localhost: 8005 / getLeaveFlowByProccessInstanceId

 

Precautions:

1. When I was testing, found that after a process executed, among the corresponding data act_ru_task will be emptied out, the data is saved to a table in the beginning of the hi.

2. The more important is that this process instance id 2507 I will later use this parameter.

3. I am currently using Activiti5.22.0 version.

4. This article was written no less, and the next article I will explain how to highlight a workflow instance, and query historical task, and how to suspend, activate, delete a process, so stay tuned.

Reference article:

Run the task of Activiti workflow / query / completed

SpringBoot Activiti integration of online designer hurricane snail _013d pringBoot integrated Activi the online designer

Detailed tables Activiti23

Published 68 original articles · won praise 55 · views 20000 +

Guess you like

Origin blog.csdn.net/tangthh123/article/details/105120702