Activiti5 入門學習整理

項目結構java

pom.xml mysql

<dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>5.19.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-model</artifactId>
            <version>5.19.0.2</version>
        </dependency>

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

Activiti.cfg.xml --生成所需流程表spring

<?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/act" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="root" />

        <property name="databaseSchemaUpdate" value="true" />

    </bean>

</beans>

流程bpmn文件sql

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/testm1480857817152" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1480857817152" name="" targetNamespace="http://www.activiti.org/testm1480857817152" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="myProcess_1" isClosed="false" isExecutable="true" name="act1" processType="None">
    <startEvent id="_2" name="StartEvent"/>
    <userTask activiti:assignee="comp" activiti:exclusive="true" id="_3" name="actTask">
      <extensionElements>
        <activiti:executionListener event="start"/>
        <activiti:formProperty/>
      </extensionElements>
    </userTask>
    <endEvent id="_4" name="EndEvent"/>
    <sequenceFlow id="_5" sourceRef="_3" targetRef="_4"/>
    <sequenceFlow id="_6" sourceRef="_2" targetRef="_3"/>
  </process>
  
</definitions>

流程圖片express

測試方法測試

public class actCreateTable {

    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    // 建立流程所需表
    @Test
    public void createTableForAct(){

        // 引擎配置 建立activiti的25張表
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        // 獲取流程引擎對象
        ProcessEngine processEngine = configuration.buildProcessEngine();
    }

   // 部署流程
    @Test
    public void deploy(){
        Deployment deployment = processEngine.getRepositoryService()    // 獲取部署相關的service
                         .createDeployment()                            // 建立部署
                         .addClasspathResource("diagrams/myActPro.bpmn")// 加載資源文件
                         .name("actTask")
                         .deploy();                                     // 部署文件

        System.out.println("流程的ID:"+deployment.getId());
        System.out.println("流程的Name:"+deployment.getName());
    }

    //開始運行流程
    @Test
    public void runTask(){
        ProcessInstance processInstance = processEngine.getRuntimeService()
                .startProcessInstanceByKey("myProcess_1");
        System.out.println("流程實例ID:"+processInstance.getId());
        System.out.println("流程定義ID:"+processInstance.getProcessDefinitionId());


    }

    // 進行任務流程 走到任務節點
    @Test
    public void finishTask(){
       List<Task> list = processEngine.getTaskService()
            .createTaskQuery()
            .taskAssignee("comp")   // 指定委託人  查看當前委託人的全部流程
            .list();
        for(Task task : list){
            System.out.println("任務ID:"+task.getId());
            System.out.println("任務名稱:"+task.getName());
            System.out.println(String.format( "任務建立時間:" +task.getCreateTime(), "yyyy-MM-DD"));
            System.out.println("任務委託人:"+task.getAssignee());
            System.out.println("流程實例ID:"+task.getProcessDefinitionId());
        }
        System.out.println("任務ID:"+list);

    }

    // 走完流程 end
    @Test
    public void tory(){
        processEngine.getTaskService() // 任務相關Service
                .complete("7504"); // 指定要完成的任務ID
    }

}

流程的curdui

public class ActCurd {

    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    //經過流程部署id得到流程圖片
    @Test
    public void actQueryById() throws IOException {
        InputStream in = processEngine.getRepositoryService()
                // 經過流程ID和流程資源名稱得到輸入流
            .getResourceAsStream("5001","diagrams/myActPro.myProcess_1.png");
        // 將圖片存放在D盤中
        FileUtils.copyInputStreamToFile(in,new File("d://activit.png"));
    }

    // 查詢流程定義的集合 -- 查詢流程的最高版本
    @Test
    public void actQueryList(){
        List<ProcessDefinition> listPro = processEngine.getRepositoryService()  // 獲取service
                .createProcessDefinitionQuery()                                 //建立流程定義查詢
                .orderByProcessDefinitionVersion().asc()                        // 經過流程版本的升序來查詢
                .list();
        // 定義一個有序的map 相同的key 假如添加map的值 後面的值會覆蓋前面相同key的值
        Map<String,ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
        //遍歷集合 根據key來覆蓋前面的值 來保證最新的Key覆蓋前面的全部老的Key的值
        for (ProcessDefinition pro : listPro){
            map.put(pro.getKey(),pro);
        }
        // 定義一個有序的list 來保證最新的Key覆蓋前面的全部老的Key的值
        List<ProcessDefinition> list = new LinkedList<ProcessDefinition>(map.values());
        // 遍歷list
        for (ProcessDefinition pro : list){
            System.out.println("ID_:"+pro.getId());
            System.out.println("NAME_:"+pro.getName());
            System.out.println("KEY_:"+pro.getKey());
            System.out.println("VERSION_:"+pro.getVersion());
            System.out.println("===================");
        }
    }
    //流程定義的刪除
    @Test
    public void actDelete(){
        List<ProcessDefinition> listPro = processEngine.getRepositoryService()
            .createProcessDefinitionQuery()
            .processDefinitionKey("act2") // 經過key 來進行刪除
            .list();

        // 遍歷list
        for (ProcessDefinition pro : listPro){
            processEngine.getRepositoryService()
                    // true 設置級聯刪除,相關表數據所有刪除 經過部署id 來刪除
                    .deleteDeployment(pro.getDeploymentId(),true);
        }
    }


}
相關文章
相關標籤/搜索