activiti小程序

1.建表

/*
    * 生成activiti所需的25张表
    */
    @Test
    public void testCreateTable(){
        ProcessEngineConfiguration pec = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
        pec.setJdbcDriver("com.mysql.jdbc.Driver");
        pec.setJdbcUrl("jdbc:mysql://localhost:3306/activitiTest?useUnicode=true&characterEncoding=utf8&serverTimeZone=GMT%2b8");
        pec.setJdbcUsername("root");
        pec.setJdbcPassword("111111");
        /*
         * 配置模式true 自动创建和更新表
         */
        pec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        ProcessEngine pe = pec.buildProcessEngine();

    }
    /*
     * 使用配置文件生成activiti所需的25张表
     */
    @Test
    public void testCfgCreateTable(){
        ProcessEngineConfiguration pec = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine pe = pec.buildProcessEngine();
    }

其中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="jdbcUrl" value="jdbc:mysql://localhost:3306/activitiTest?useUnicode=true"/>
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUsername" value="root"/>
        <property name="jdbcPassword" value="111111"/>
     </bean>
</beans>

2.已有bpmn,流程启动

package com.tongyao.activiti.flow;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;

import java.util.List;

public class HelloWorldProcess {
    /*
     *获取默认流程引擎实例,会自动调用activiti.cfg.xml
    */
    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /*部署*/
    @Test
    public void deploy(){
        Deployment deployment = processEngine.getRepositoryService()//获取部署相关service
        .createDeployment()//创建部署
        .addClasspathResource("diagrams/HelloWorld.bpmn")//加载资源文件
        .deploy();//部署

        System.out.println("流程部署id="+deployment.getId());
        System.out.println("流程部署name="+deployment.getName());
    }

    /*启动流程实例*/
    @Test
    public void start(){
       ProcessInstance pi = processEngine.getRuntimeService()//获取运行service
       .startProcessInstanceByKey("myFirstProcess");//流程定义表procedef的key值

        System.out.println("流程实例ID:"+pi.getId());
        System.out.println("流程定义ID:"+pi.getProcessDefinitionId());
    }

    /*查看任务*/
    @Test
    public void findTask(){
        List<Task> taskList = processEngine.getTaskService()//获取任务service
        .createTaskQuery()//创建任务查询
        .taskAssignee("peimx")//指定某人
        .list();

        for(Task task:taskList){
            System.out.println("任务ID:"+task.getId());
            System.out.println("任务Name:"+task.getName());
            System.out.println("任务创建时间:"+task.getCreateTime());
            System.out.println("任务委派人:"+task.getAssignee());
            System.out.println("流程实例ID:"+task.getProcessInstanceId());
        }

    }

    /*完成任务*/
    @Test
    public void completeTask(){
        processEngine.getTaskService()//获取任务service
        .complete("2504");
    }
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页