activiti入门

springboot项目

pom

<?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.liu</groupId>
    <artifactId>activiti</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>activiti</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.8</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>
            <dependency>
                <groupId>org.activiti.dependencies</groupId>
                <artifactId>activiti-dependencies</artifactId>
                <version>7.1.0.M6</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <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.liu.activiti.ActivitiApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="databaseType" value="mysql"></property>
        <property name="jdbcUrl" value="jdbc:mysql://10.20.16.15:5102/cpms?serverTimezone=Asia/Shanghai"></property>
        <property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="jdbcUsername" value="cpmsopr"></property>
        <property name="jdbcPassword" value="T077c19ec6d18#"></property>
        <!--是否生成表结构-->
        <property name="databaseSchemaUpdate" value="false"/>
    </bean>
</beans>

流程图

测试流程

package com.liu.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class ActivitiApplicationTests {

    /**
     * 创建默认数据表
     */
    @Test
    void creatTable() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }

    /**
     * 测试流程部署
     */
    @Test
    void testDeployment() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .name("请假审批流程")
                .addClasspathResource("processes/Process20220125.bpmn")
                .addClasspathResource("processes/Process20220125.png")
                .deploy();
        System.out.println("流程部署id:-" + deployment.getId());
        System.out.println("流程部署名字:-" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    void testStartProcess() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        ProcessInstance process_20220125 = runtimeService.startProcessInstanceByKey("Process_20220125");
        System.out.println("流程定义ID:-" + process_20220125.getProcessDefinitionId());
        System.out.println("流程实例ID:-" + process_20220125.getId());
        System.out.println("当前活动的ID:-" + process_20220125.getActivityId());
    }

    /**
     * 启动流程实例(传入BusinessKey启动,一般是业务主表的ID)
     */
    @Test
    void testStartProcessByKey() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        ProcessInstance process_20220125 = runtimeService.startProcessInstanceByKey("Process_20220125","RL20220126000125");
        System.out.println("流程定义ID:-" + process_20220125.getProcessDefinitionId());
        System.out.println("流程实例ID:-" + process_20220125.getId());
        System.out.println("当前活动的ID:-" + process_20220125.getActivityId());
    }

    /**
     * 查询待处理任务
     */
    @Test
    void testFindProcess() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        List<Task> process_20220125 = taskService.createTaskQuery().processDefinitionKey("Process_20220125").list();
        for (Task task : process_20220125) {
            System.out.println("流程实例ID:-"+task.getProcessInstanceId());
            System.out.println("任务DI:-"+task.getId());
            System.out.println("任务负责人:-"+task.getAssignee());
            System.out.println("任务名称:-"+task.getName());
        }
        }


    /**
     * 提交完成节点信息(提交经理审批)
     */
    @Test
    void testCompletProcess(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        Map<String, Object> variables = new HashMap<>();
        variables.put("managerStatus",true);
        //任务的ID  ru_task表中id
        taskService.complete("40680004",variables);
    }

    /**
     * 测试挂起所有流程
     * 修改的是ru_task等几张表的SUSPENSION_STATE_字段,标志是否挂起
     */
    @Test
    void testHangUp(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //查询Process_20220125的所有实例流程
        List<ProcessDefinition> process_20220125 = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("Process_20220125").list();
        process_20220125.stream().forEach(o ->{
            System.out.println("流程定义ID:-"+o.getId());
            String id = o.getId();
            boolean suspended = o.isSuspended();
            //挂起则激活,激活则挂起
            if (suspended){
                repositoryService.activateProcessDefinitionById(id,true, null);
                System.out.println("激活");
            }else{
                repositoryService.suspendProcessDefinitionById(id,true, null);
                System.out.println("挂起");
            }
        });
    }


    /**
     * 挂起单个实例流程
     */
    @Test
    void testSingleHangUp(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("40680000").singleResult();
        //该实例挂起状态
        boolean suspended = processInstance.isSuspended();
        String id = processInstance.getId();
        if (suspended){
            runtimeService.activateProcessInstanceById(id);
            System.out.println("流程实例:"+id+":激活");
        }else{
            runtimeService.suspendProcessInstanceById(id);
            System.out.println("流程实例:"+id+":挂起");
        }

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值