activiti 工作流的基本使用

activiti 工作流 使用

一、安装activiti插件

  • 由于idea中找不到actiBpmn 所以先下载好,现在导入本地的jar包

在这里插入图片描述

在这里插入图片描述

二、activiti的文件配置

  • maven文件的配置
 <properties>
        <slf4j.version>1.7.2</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- activiti start -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-converter</artifactId>
            <version>5.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>5.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>5.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-image-generator</artifactId>
            <version>5.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-process-validation</artifactId>
            <version>5.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>5.21.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-dbcp</artifactId>
                    <groupId>commons-dbcp</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-diagram-rest</artifactId>
            <version>5.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>5.21.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-collections</artifactId>
                    <groupId>commons-collections</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>


        <!-- LOGGING begin -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.4.2</version>
            <scope>compile</scope>
        </dependency>
        <!-- LOGGING end -->
    </dependencies>

三、配置对应的文件 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 -->
    <bean id="processEngineConfiguration"  class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="123456" />
        <!--这个是指创建完之后,会进行删除操作-->
<!--        <property name="databaseSchemaUpdate" value="create-drop" />-->
    </bean>
</beans>

四、activiti的实现流程

 @Test
    public void testDeployEmpLeave(){
        //创建工作流引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //管理流程文件
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //部署test.bpmn 文件到数据表
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("test2.bpmn").deploy();
        System.out.println("部署文件的ID"+deploy.getId());
    }

    //卸载部署流程文件
    @Test
    public void testDeleteBpmnFile(){
        //创建工作流引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //删除流程文件(卸载部署文件)
        repositoryService.deleteDeployment("2501");
        System.out.println("删除部署文件 id=2501");
    }


    //启动流程实例
    @Test
    public void StartInstance(){
        //获取工作流引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1");
        System.out.println("启动当前实例的id值="+processInstance.getId()); //对应的是act_ru_task 这个表
        procIntanceId = String.valueOf(processInstance.getId());
        System.out.println(procIntanceId);
    }

    //获取当前任务节点对象及完成此任务节点的是谁
    @Test
    public void testGetAndCompleteFirstUserTask(){
        //创建工作流引擎
        ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
        //TaskService:管理Task的节点
        TaskService taskService=processEngine.getTaskService();
        //获取当前流程实例id=7501的,当前任务节点对象
        Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
        System.out.println("第一个任务名="+task.getName());
        System.out.println("第一个任务Id="+task.getId());
        //完成此任务节点
        taskService.complete(task.getId());
    }



    //判断此实例流程是否结束
    @Test
    public void testFlowEnd() {
        //获取工作流引擎对象
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        TaskService taskService=processEngine.getTaskService();

        //获取实例id=5001,当前任务节点对象
        Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
        if(task==null) {
            System.out.println("此实例流程已经结束");
        }else
            System.out.println("此实例流程并没有结束处理");
    }

    /*
     * 加载参数实例(参数从一个节点传输到另一个节点)
     * */
    @Test
    public void testSetTaskVariable() {
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        TaskService taskService=processEngine.getTaskService();
        //根据当前的实例id,获取当前的任务节点
        Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
        //设置当前任务的变量值
        taskService.setVariable(task.getId(),"username","胡图图");;
        taskService.setVariable(task.getId(),"age",22);
    }


    /*查询上一阶段传下来的值*/
    @Test
    public void testGetTaskVariable() {
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        TaskService taskService=processEngine.getTaskService();
        //根据当前的实例id,获取当前的任务节点
        Task task=taskService.createTaskQuery().processInstanceId(procIntanceId).singleResult();
        System.out.println(taskService.getVariable(task.getId(), "username"));
        System.out.println(taskService.getVariable(task.getId(), "age"));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值