Activiti- process definition

First, what is the process definition

  Bpmn2.0 standard process definition is to describe the vertebral line at the business process, usually activiti-explorer (web console) or plug activiti-eclipse-designer modeling business processes, both methods follow standard bpmn2.0. Use designer designer draws the process, two files are generated: .bpmn and .png

  

1, .bpmn file

  

2, .png file generation

  ① First .bpmn file instead .xml file

    

  ② Right then the xml file Diagrams ---> show BPMN 2.0 Designer can generate a flow chart Export the diagram can be saved to the corresponding directory project

    

    

  ③ .xml then change it back to .bpmn

    

note:

  Shall be provided prior to the encoding format and then restart generating flowchart IDEA! ! !

-Dfile.encoding=UTF-8

  

Second, the process definition deployment

1. What is the definition of the deployment process

  The process definition deployed to the line activiti database, which is to define the deployment process, by calling the api activiti of the process definition bpmn and png files one by one, add two to deploy activiti, the two files can also be labeled as ZIP bag deployment;

2, activiti.cfg.xml file

Copy the code
<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/contex
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.ORG / Schema / TX / Spring-tx.xsd "> 
    <-! Data Source ->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/activiti"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
    </bean>

    <!--配置Activiti使用的processEngine对象   默认命名为processEngineConfiguration-->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!--注入数据源-->
        <property name="the dataSource "REF =" the dataSource "/> 
        ! <- <Property name =" jdbcDriver "value =" com.mysql.jdbc.Driver "/> ->
        <-! configuration data sources two ways: ->

        <! - Specify the database generation strategy -> 
        <Property name = "databaseSchemaUpdate" value = "to true" /> 
    </ bean> 
</ Beans>
Copy the code

3, a single file deployment

  Respectively bpmn file and png image file deployment;  

Copy the code
com.zn Package; 

Import org.activiti.engine.ProcessEngine; 
Import org.activiti.engine.ProcessEngines; 
Import org.activiti.engine.RepositoryService; 
Import org.activiti.engine.repository.Deployment; 
Import org.junit.Test; 

{class ActivitiTest public 

    / * process deployment 
     * `act_ge_bytearray` resource information definition process, the process comprising bpmn png file information, and 
     *` act_re_deployment` process deployment process information includes the name, ID, key, etc. 
     * `act_re_procdef` flow definition information 
     * / 
    @Test 
    public void Deployment () { 
        // Get the object ProcessEngine default configuration file name: activiti.cfg.xml and configuration of Bean instance ID processEngineConfiguration 
        ProcessEngine processEngine ProcessEngines.getDefaultProcessEngine = (); 
        // Get deployment process object RepositoryService
        RepositoryService = processEngine.getRepositoryService the RepositoryService (); 
        // deployed, the corresponding process definition file to which the database generated, stored as records 
        the Deployment Deployment repositoryService.createDeployment = () 
                // file loading process 
                .addClasspathResource ( "bpmnFiles / holiday. BPMN ") 
                .addClasspathResource (" bpmnFiles / holiday.png ") 
                // set the name of the process 
                .name (" leave process ") 
                .key (" holidayKey ") 
                // deploy 
                the .deploy (); 
        // deployment information output 
        System.out .println ( "process name:" + deployment.getName ());  
        System.out.println ( "process ID:"+deployment.getId());
        System.out.println ( "process Key:" + deployment.getKey ()) ;
    }
}
Copy the code

4, compression bag deployment

  The bpmn file and png images into a zip package;

  

Copy the code
com.zn Package; 

Import org.activiti.engine.ProcessEngine; 
Import org.activiti.engine.ProcessEngines; 
Import org.activiti.engine.RepositoryService; 
Import org.activiti.engine.repository.Deployment; 
Import org.junit.Test; 

a java.io.InputStream Import; 
Import java.util.zip.ZipInputStream; 

public class ActivitiTest { 

    / ** 
     deployment process compressed manner * 
     * / 
    @Test 
    public void deploymentByZip () { 
        // Get the object processEngine 
        processEngine processEngine = ProcessEngines. getDefaultProcessEngine (); 
        // get a RepositoryService objects 
        RepositoryService repositoryService = processEngine.getRepositoryService ();
        // Define an input stream, Zip file loading 
        the InputStream inputStream = this.getClass () getClassLoader () the getResourceAsStream ( "bpmnFiles / holiday.zip");.. 
        // define a target ZIPInputStream 
        ZipInputStream ZipInputStream = new new ZipInputStream (inputStream); 
        / / processes for 
        the deployment deploy = repositoryService.createDeployment () 
                .addZipInputStream (ZipInputStream) 
                .name ( "request approval process") 
                .key ( "holidayKey") 
                the .deploy (); 
        System.out.println ( "process name:" + deploy .getName ()); 

    } 
}
Copy the code

  The effect of the implementation of the Operations Console

  

  Database show

  

5, process definition query

  Query process definition deployed;

Copy the code
com.zn Package; 

Import org.activiti.engine.ProcessEngine; 
Import org.activiti.engine.ProcessEngines; 
Import org.activiti.engine.RepositoryService; 
Import org.activiti.engine.repository.Deployment; 
Import org.activiti.engine. repository.ProcessDefinition; 
Import org.activiti.engine.repository.ProcessDefinitionQuery; 
Import org.junit.Test; 

Import a java.io.InputStream; 
Import java.util.List; 
Import java.util.zip.ZipInputStream; 

public class ActivitiTest { 

    / ** 
     * See flow definition information 
     * / 
    @Test 
    public void getDefaultProcessEngine(){
        //获取processEngine对象
        the processEngine processEngine ProcessEngines.getDefaultProcessEngine = (); 
        // get a target RepositoryService 
        the RepositoryService RepositoryService processEngine.getRepositoryService = (); 
        // get the process definition view object ProcessDefinitionQuery 
        ProcessDefinitionQuery processDefinitionQuery repositoryService.createProcessDefinitionQuery = (); 
        // process the specified view processDefinitionKey () key value in the designated process definition The sort process definition orderByProcessDefinitionVersion version 
        List <the processDefinition> processDefinitionQuery.processDefinitionKey = Holiday ( "Holiday") orderByProcessDefinitionVersion () desc () List ();... 
        for (the processDefinition processDefinition: Holiday) { 
            System.out.println ( "process the definition ID: "+ processDefinition.getId ()) ;
            System.out.println ( "process definition name:" + processDefinition.getName ()); 
            System.out.println ( "process definition Key:" + processDefinition.getKey ()); 
            System.out.println ( "Process definition version numbers: "+ processDefinition.getVersion ()); 
        } 
    } 
}
Copy the code

  The effect of the implementation of the Operations Console

  

6, process definition deleted

  Delete the successful deployment of process definitions;

Copy the code
com.zn Package; 

Import org.activiti.engine.ProcessEngine; 
Import org.activiti.engine.ProcessEngines; 
Import org.activiti.engine.RepositoryService; 
Import org.activiti.engine.repository.Deployment; 
Import org.activiti.engine. repository.ProcessDefinition; 
Import org.activiti.engine.repository.ProcessDefinitionQuery; 
Import org.junit.Test; 

Import a java.io.InputStream; 
Import java.util.List; 
Import java.util.zip.ZipInputStream; 

public class ActivitiTest { 

    / ** 
     * delete process: 
     * assume that when you delete, currently have the process instance, it will result in the deletion fails 
     * If mandatory, you can use repositoryService.deleteDeployment ( "1", true) ; true representatives cascade delete 
     * / 
    @Test
    void deleteDeployment public () { 
        // Get the object processEngine 
        the ProcessEngine processEngine ProcessEngines.getDefaultProcessEngine = (); 
        // get a target RepositoryService 
        RepositoryService RepositoryService processEngine.getRepositoryService = (); 
        // removal process 
        repositoryService.deleteDeployment ( "10001", true) ; 
    } 
}
Copy the code

  Description:

    1. Use RepositoryService delete process definition;

    2. If there is no process running under process definition, you can delete the ordinary;

    3. If there will be a process that is already running under process definition, use common deletion error, can cascade delete method removes all processes and related cascade;

Third, read the resource from the database file to a local

1, import-dependent

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

2, code implementation

Copy the code
com.zn Package; 

Import org.activiti.engine.ProcessEngine; 
Import org.activiti.engine.ProcessEngines; 
Import org.activiti.engine.RepositoryService; 
Import org.activiti.engine.repository.Deployment; 
Import org.activiti.engine. repository.ProcessDefinition; 
Import org.activiti.engine.repository.ProcessDefinitionQuery; 
Import org.apache.commons.io.IOUtils; 
Import org.junit.Test; 

Import the java.io. *; 
Import java.util.List; 
Import Java. util.zip.ZipInputStream; 

public class ActivitiTest { 

    / ** 
     * reads data from the resource file to the local 
     * / 
    @Test 
    public void the getResource () throws IOException { 
        // Get the object processEngine
        ProcessEngine = ProcessEngines.getDefaultProcessEngine the ProcessEngine (); 
        // get a RepositoryService objects 
        RepositoryService RepositoryService = processEngine.getRepositoryService (); 
        // get into the process the query object 
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery (); 
        // get resources to flow 
        processDefinitionQuery.processDefinitionKey ( "Holiday"); 
        // get a single result of 
        the processDefinition processDefinition processDefinitionQuery.singleResult = (); 
        ID process deployment // 
        String deploymentId processDefinition.getDeploymentId = (); 
        // acquired bpmnResource
        BpmnStream = repositoryService.getResourceAsStream the InputStream (deploymentId, processDefinition.getResourceName ()); 
        // acquired PNG 
        the InputStream pngStream = repositoryService.getResourceAsStream (deploymentId, processDefinition.getDiagramResourceName ()); 
        // build the output stream 
        bpmnOut = new FileOutputStream OutputStream ( "E : \\ accp \\ Y2 \\ complete set item \\ "+ processDefinition.getResourceName ()); 
        the OutputStream PNGOUT a FileOutputStream new new = (" E: \\ accp \\ Y2 \\ complete set item \\ "+ processDefinition.getDiagramResourceName ()); 
        // the output data to the disk which 
        IOUtils.copy (bpmnStream, bpmnOut); 
        IOUtils.copy (pngStream, PNGOUT); 
        // off flow 
        pngOut.close ();  
        bpmnOut.close ();
        pngStream.close ();
        bpmnStream.close();
    }
}
Copy the code

3, the effect

  

Fourth, business systems and processes associated with the system

  Primary key field identifies the data ID buessinessKey service system, and thus the current service table to perform a process which

1. code implementation

Copy the code
com.zn Package; 

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.task.Task; 
Import org.apache.commons.io.IOUtils; 
Import org.junit.Test; 

Import the java.io. *; 
Import java.util.List; 
Import java.util.zip.ZipInputStream; 

public ActivitiTest {class 

    / ** 
     * examples and business systems associated with the process start 
     * normal data should first add them to business table, the primary key ID to get the current data is added, start the process by example, the primary key ID to BuessniessKkey 
     * Representative systems and processes association 
     * / 
    @Test 
    public void startInstance () { 
        / **
         * Let Joe Smith submitted the page, and then get the ID 
         * / 

        / ** 
         * create an instance of the process leave Joe Smith 
         * / 
        // Get processEngine target 
        the ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine (); 
        RuntimeService runtimeService = processEngine.getRuntimeService (); 
        // generation process 
        runtimeService.startProcessInstanceByKey ( "Holiday", "1"); 

        / ** 
         * Joe Smith submitted for approval to leave 
         * / 
        // get a TaskService objects 
        TaskService TaskService = processEngine.getTaskService (); 
        // get John's mission 
        . Task task = taskService.createTaskQuery () taskAssignee ( "zhangsan") processDefinitionKey ( "holiday") singleResult ()..;
        // task approval 
        taskService.complete (task.getId ()); 
    } 
}
Copy the code

2. Database

  

Fifth, the process to activate and suspend

1, all processes

Copy the code
package com.zn;

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.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.zip.ZipInputStream;

public class ActivitiTest {

    /**
     * 所有流程定义的挂起
     */
    @Test
    public void allSuspend(){
        //获取processEngine对象
        ProcessEngine = ProcessEngines.getDefaultProcessEngine the ProcessEngine (); 
            the System .out.println ( "the activation process definition"); 
        } the else {
        // Get repositoryService objects 
        RepositoryService repositoryService = processEngine.getRepositoryService (); 
        // get the process definition 
        ProcessDefinition Holiday = repositoryService.createProcessDefinitionQuery () processDefinitionKey ( "Holiday") singleResult ();.. 
        // Get the current process definition whether the suspended state method suspended false to true is representative of the pause operation 
        Boolean suspended holiday.isSuspended = (); 
        IF (suspended) { 
            repositoryService.activateProcessDefinitionById (holiday.getId (), true, null);
            repositoryService.suspendProcessDefinitionById (holiday.getId (), to true, null); 
            System.out.println ( "suspend the process definition"); 
        } 
    } 
}
Copy the code

Console effects:

  

Implementation of the results again:

  

2, a single flow

Copy the code
package com.zn;

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.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.zip.ZipInputStream;

public class ActivitiTest {

    /**
     * 单个流程实例的挂起
     */
    @Test
    public void singleSuspend(){
        //获取processEngine对象
        ProcessEngine = ProcessEngines.getDefaultProcessEngine the ProcessEngine (); 
        // Get the object RuntimeService 
        RuntimeService runtimeService processEngine.getRuntimeService = (); 
        . RuntimeService.createProcessInstanceQuery the ProcessInstance the processInstance = () processInstanceId ( "2501") singleResult ();. 
        // Get the current process definition method suspended if the suspend state is true representative of the suspend operation is false 
        Boolean suspended processInstance.isSuspended = (); 
        iF (suspended) { 
            runtimeService.activateProcessInstanceById ( "2501"); 
            System.out.println ( "XX leave the activation process "); 
        } the else { 
            runtimeService.suspendProcessInstanceById (" 2501 "); 
            System.out.println ("xx leave of absence pending process ");
        }
    }
}
Copy the code

Console effects:

  

Implementation of the results again:

  

 

Guess you like

Origin www.cnblogs.com/mayuan01/p/12620996.html