activiti 的hello demo,zip初始化数据库,查询办理,查看流程图,

建立springboot项目,2.4.4

activiti 5.22

建立 activiti.cfg.xml在/src/main/resources

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    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="jdbcDriver" value="com.mysql.cj.jdbc.Driver"></property>
      <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti_demo"></property>
      <property name="jdbcUsername" value="root"></property>
      <property name="jdbcPassword" value="xxxxx"></property>
   
      <property name="databaseSchemaUpdate" value="true"></property>
  </bean>      
</beans>

 

建立pom.xml

<?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.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.taoao</groupId>
    <artifactId>demo_activiti</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo_activiti</name>
    <description>demo_activiti</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- xuyong -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
        
         <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.activiti/activiti-engine -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>5.22.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>5.23.0</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

初始化数据库

部署流程

// 初始化数据库
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //@Test void deployProcess() { Deployment deploy = this.processEngine.getRepositoryService().createDeployment() .addClasspathResource("MyProcess.bpmn") .addClasspathResource("MyProcess.png").deploy(); }

部署流程用zip模式

@Test
    void deployProcess2() {
        // 加/ 在resource 里找,不加 / 在当前类包下找
        // HelloWorld的名字,在re_deployment,而压缩内容的名字(myProcess),在re_procdef 里,版本号+1
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/HelloWorld.zip");
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        ZipInputStream zipInputStream = new ZipInputStream(resourceAsStream);
        Deployment deploy = repositoryService.createDeployment().name("helloworld001").addZipInputStream(zipInputStream)
         .deploy();
        System.err.println("部署成功 "+deploy);
    }

 

启动流程

/**
     * 开始启动该流程
     */
    //@Test
    void startProcess() {
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        //ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey("myProcess");
        ProcessInstance startProcessInstanceById = runtimeService.startProcessInstanceById("myProcess:1:42504");
        
        System.err.println("start doing " + startProcessInstanceById.getId());
        
        /**
         * 常用:
         */
        //runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
        //runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey,variables);
        
    }

查询任务

@Test
    void queryTask() {
        
        TaskService taskService = this.processEngine.getTaskService();
        List<Task> list= taskService.createTaskQuery().taskAssignee("王老五")
                //.deploymentId(deploymentId)
                //.deploymentIdIn(deploymentIds)
                //.processDefinitionId(processDefinitionId)
                //.processDefinitionKey(processDefinitionKey)
                //.executionId(executionId)
                .orderByTaskCreateTime().asc()
                .list();
        for (Task task : list) {
            System.err.println("task id: "+ task.getId());
            System.err.println("task name: "+task.getName());
            System.err.println("task getExecutionId: "+task.getExecutionId());
            System.err.println("task time: "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(task.getCreateTime()));
            System.err.println("task processInstanceId: "+ task.getProcessInstanceId());
            System.err.println("#################------------");
        }
        
        System.err.println("size: "+list.size());
    }

查询流程部署 和流程定义

 

/***
     * 流程部署查询,模糊查询时,需要加%符号。
     */
    @Test
    void queryRepositoryService() {
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        //请假单模糊查询,必须前后加%,否则是精准查询
//      List<Deployment> list = repositoryService.createDeploymentQuery().deploymentNameLike("%请假单%").list();
//         for (Deployment deployment : list) {
//                System.err.println("deployment id is "+ deployment.getId());
//                System.err.println("deployment name is" + deployment.getName());
//                System.err.println("###########################");
//            }
//            System.err.println(list.size());
        
        Deployment singleResult = repositoryService.createDeploymentQuery().deploymentId("1").singleResult();
        System.err.println("singleResult category is "+ singleResult.getCategory());
        System.err.println("singleResult name is "+ singleResult.getName());
        System.err.println("singleResult time is "+ singleResult.getDeploymentTime());
    }
    
    /***
     * 流程定义查询
     */
     @Test
     void queryProcDef() {
         RepositoryService repositoryService = this.processEngine.getRepositoryService();
         //List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionNameLike("%My process%").list();
         List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionResourceNameLike("%MyProcess%").list();
         for (ProcessDefinition processDefinition : list) {
            System.err.println("name is ="+processDefinition.getName());
            System.err.println("流程定义id is ="+ processDefinition.getId());
            System.err.println("部署id is ="+ processDefinition.getDeploymentId());
            System.err.println("version is ="+processDefinition.getVersion());
            System.err.println("png name is ="+processDefinition.getDiagramResourceName());
            System.err.println("bpmn name is ="+processDefinition.getResourceName());
            System.err.println("#############################---------");
        }
         System.err.println("size is "+list.size());
     }

 

 

 

查看流程图

/**
      * 查看流程图,
      * 首先获得一个输入流
      * 创建一个输出流,输出到一个文件
      * 定义一个长度0的整数,然后一个字节数组
      * 循环,输入流的读取字节,若不为负一,
      * 输出流开始写,按字节写,从0开始,到len,len取值于输入流读取字节的长度
      * 输出流清理缓冲,关闭输入流,关闭输出流
      */
     @Test
     void viewProcessImg() {
         RepositoryService repositoryService = this.processEngine.getRepositoryService();
         //获得部署
         ProcessDefinition singleResult = repositoryService.createProcessDefinitionQuery().deploymentId("5001").singleResult();
         //获得定义id
         String id = singleResult.getId();
         System.err.println("定义id: "+id);
         InputStream inputStream = repositoryService.getProcessDiagram(id);
        // InputStream inputStream = repositoryService.getProcessDiagram("myProcess:8:32504");
         File file = new File("d:/bpmn.png") ;
         try {
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
             int len = 0;
             byte[] b = new byte[1024];
        
            while((len=inputStream.read(b))!=-1) {
                 bos.write(b, 0, len);
                 bos.flush();
             }
            bos.close();
            inputStream.close();
        } catch (IOException e) {
            
            e.printStackTrace();
        }
     }

查看最新部署lastversion

 /**
      * last 版本查询
      * 等同于上面的那个用map的存放key,然后再查询的方法。
      */
     
     @Test
     void queryLastDef() {
         RepositoryService repositoryService = this.processEngine.getRepositoryService();
         ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().latestVersion().singleResult();
         System.err.println("last pd version is "+pd.getVersion());
     }

删除流程定义

/***
      * 删除流程定义,传入的参数是  procDef 表中的 deployment id,后面加入true,是删除级联
      */
     @Test
     void deleteProcess() {
         RepositoryService repositoryService = this.processEngine.getRepositoryService();
         repositoryService.deleteDeployment("27510",true);
         
     }

 

删除key相同的流程定义

/***
      * 删除key相同的所有流程定义
      * 先查出流程定义,然后查出部署
      * 从部署中删除
      */
     @Test
     void deleteAllSameKey() {
         String processKey="myProcess";
         RepositoryService repositoryService = this.processEngine.getRepositoryService();
         //获得流程定义key相同的所有的流程定义
         List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey(processKey).list();
         if(list!=null&&list.size()>0) {
             for (ProcessDefinition processDefinition : list) {
                 //删除的是流程定义的部署id,不是流程id
                 repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true);
            }
         
         }
     }

 

办理完成任务

@Test
    void completeTask() {
        TaskService taskService = this.processEngine.getTaskService();
        //ru_task表中的 id
        String taskId="75004";
        taskService.complete(taskId);
        System.err.println("完成任务");
        //taskService.complete(taskId, variables);
        //taskService.complete(taskId, variables, localScope);
    }

判断任务是否结束

/**
     * 是否判断流程结束
     * 
     */
    @Test
    void isComplete() {
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
    
        // 或者是 ru_execution 表中的 proc_init_id
        String processInstanceId="50001";
        ProcessInstance singleResult = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if(singleResult!=null) {
            System.err.println("流程进行中....");
            System.err.println("流程实例id;现在进行到: "+ singleResult.getId());
            TaskService taskService = this.processEngine.getTaskService();
            Task singleResult2 = taskService.createTaskQuery().processInstanceId(singleResult.getId()).singleResult();
            System.err.println("现在进行到: "+singleResult2.getName());
        }else {
            System.err.println("流程已完成");
        }
        
    }

查询历史任务 和历史流程实例

/***
     * 查询历史任务
     * 单线程时,流程实例processInstance ID=流程执行execution ID,相同,
     * 多线流程时,execute有多个,但流程实例process 只有一个。
     */
    @Test
    void queryHistoryTask() {
        HistoryService historyService = this.processEngine.getHistoryService();
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().orderByProcessInstanceId().asc().list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.err.println("historicTaskInstance 办理人: "+ historicTaskInstance.getAssignee());
            System.err.println("historicTaskInstance id:  "+ historicTaskInstance.getId());
            System.err.println("historicTaskInstance name: "+ historicTaskInstance.getName());
            System.err.println("historicTaskInstance getProcessDefinitionId  流程定义ID: "+ historicTaskInstance.getProcessDefinitionId());
            System.err.println("historicTaskInstance getExecutionId 流程执行ID: "+ historicTaskInstance.getExecutionId());
            System.err.println("historicTaskInstance getProcessInstanceId 流程实例ID: "+ historicTaskInstance.getProcessInstanceId());
            System.err.println("historicTaskInstance 创建时间: "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(historicTaskInstance.getCreateTime()));
            System.err.println("historicTaskInstance 持续时间毫秒: "+ historicTaskInstance.getDurationInMillis());
            System.err.println("historicTaskInstance Owner: "+ historicTaskInstance.getOwner());
            System.err.println("#######################--------------------");
            
        }
    }
    /***
     * 查询历史流程实例
     */
    @Test
    void queryHistoryProcess() {
        HistoryService historyService = this.processEngine.getHistoryService();
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().orderByProcessInstanceStartTime().asc().list();
        for (HistoricProcessInstance historicProcessInstance : list) {
            System.err.println("historicProcessInstance 执行实例 getId: "+historicProcessInstance.getId());
            System.err.println("historicProcessInstance getName: "+historicProcessInstance.getName());
            System.err.println("historicProcessInstance 定义 getDeploymentId: "+historicProcessInstance.getDeploymentId());
            System.err.println("historicProcessInstance getProcessDefinitionKey: "+historicProcessInstance.getProcessDefinitionKey());
            System.err.println("historicProcessInstance getProcessDefinitionName: "+historicProcessInstance.getProcessDefinitionName());
            System.err.println("historicProcessInstance getProcessDefinitionVersion: "+historicProcessInstance.getProcessDefinitionVersion());
            System.err.println("historicProcessInstance getDescription: "+historicProcessInstance.getDescription());
            System.err.println("historicProcessInstance getDurationInMillis: "+historicProcessInstance.getDurationInMillis());
            System.err.println("historicProcessInstance getStartActivityId: "+historicProcessInstance.getStartActivityId());
            System.err.println("#########################---------------");
        }
        System.err.println("size: "+list.size());
    }

 

上一篇:activiti 和 flowable 哪个更主流,哪有用的人多,哪个好,区别是什么


下一篇:Activiti入门在idea中进行流程模型设计