Activiti is the workflow of choice, and I hope this series of articles has helped

Activiti is the most detailed series of articles on the web.

Activiti basic operation explanation

1 Process deployment

Deploying the process defined above in the Designer into the Activiti database is what we call process deployment. Deploy two process-defined BPMN and PNG files one by one into Activiti by calling Activiti’s API, and you can deploy both files in a zip package.

1.1 Single File Deployment

Deploy the BPMN file and PNG image separately

    /** * Implements a single deployment of files */
    @Test
    public void test03(a){
        // 1. Get ProcessEngine object
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2. Obtain RepositoryService to deploy the repository
        RepositoryService service = engine.getRepositoryService();
        // 3. Use RepositoryService to perform the deployment
        Deployment deploy = service.createDeployment()
                .addClasspathResource("bpmn/evection.bpmn") // Add the BPMN resource
                .addClasspathResource("bpmn/evection.png") // Add a PNG resource
                .name("Business Trip Application Process")
                .deploy();// Deployment process
        // 4. Output process deployment information
        System.out.println("Process deployment ID :" + deploy.getId());
        System.out.println("Process deployment name:" + deploy.getName());
    }
Copy the code

The related output information is displayed in logs

1.2 Deploying the ZIP File

Package the BPMN file and PNG file into a ZIP file and upload them in a unified manner

    /** * Deploy operations */ from a zip file
    @Test
    public void test04(a){
        // Define the input stream for the zip file
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("bpmn/evection.zip");
        // Decorate the inputStream
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("Business Trip Application Process")
                .deploy();
        // 4. Output process deployment information
        System.out.println("Process deployment ID :" + deploy.getId());
        System.out.println("Process deployment name:" + deploy.getName());
    }

Copy the code

The data in the uploaded database is the same as that in a single file upload.

1.3 Operate the data table

The process defines three tables in activiti after deployment

Act_re_deployment: The process defines the deployment table and adds one record for each deploymentAct_re_procdef: Process definition table to which a record is added for each new process definition deployed

Act_ge_bytearray: Process resource table in which BPMN files and PNG images of process deployment are stored

2 Start the process instance

With process definition deployed in Activiti, business processes can be managed through workflow, which means that the travel application process deployed above is available. According to the process, start a process to launch a new travel application form, this is equivalent to Java classes and Java objects, the relationship between the class definition well wish to create a new object after use, can, of course, new out multiple objects, for business trip application process, zhang SAN can launch a travel application form need to start a process instance.

    /** * Start a process instance */
    @Test
    public void test05(a){
        // 1. Create ProcessEngine object
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2. Obtain the RuntimeService object
        RuntimeService runtimeService = engine.getRuntimeService();
        // 3. Start the process according to the id defined by the process
        String id= "evection";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(id);
        // 4. Output related process instance information
        System.out.println("Process definition ID:" + processInstance.getProcessDefinitionId());
        System.out.println("Process instance ID:" + processInstance.getId());
        System.out.println("Current active ID:" + processInstance.getActivityId());
    }
Copy the code

Output content:

Start the table structure involved in the process instance

Act_hi_actinst Process instance execution history ACT_hi_identitylink Process participator history act_hi_procinst Process instance history act_hi_taskinst Process task history Act_ru_execution process execution information ACT_ru_identitylink User information act_ru_task Task information

3 Task Search

After the process starts, the person in charge of a task can query the tasks that he or she can handle at present. The queried tasks are all the to-do tasks of the current user

    /** * Task query */
    @Test
    public void test06(a){
        String assignee ="zhansan";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // A task query requires a TaskService object
        TaskService taskService = engine.getTaskService();
        // Query tasks according to the process key and the task owner
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("evection")
                .taskAssignee(assignee)
                .list();
        // Outputs the tasks that the current user has
        for (Task task : list) {
            System.out.println("Process instance ID:" + task.getProcessInstanceId());
            System.out.println("The task id." + task.getId());
            System.out.println("Mission leader:" + task.getAssignee());
            System.out.println("Task Name:"+ task.getName()); }}Copy the code

The output

4 Process task handling

The task leader queries out the person to be done, selects the task for processing, and completes the task

    /** * Process tasks */
    @Test
    public void test07(a){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("evection")
                .taskAssignee("zhansan")
                .singleResult();
        // Complete the task
        taskService.complete(task.getId());
    }
Copy the code

After Zhangsan handles this operation, the process flows to LISi

Then different users log in, and then query the task and process the task until the task process is complete.

5 Process defined queries

Query information about a process, including the definition of a process, deployment of a process, and version of a process definition

    /** * Query flow definition */
    @Test
    public void test08(a){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        // Get a ProcessDefinitionQuery object for the query operation
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> list = processDefinitionQuery.processDefinitionKey("evection")
                .orderByProcessDefinitionVersion() // Install version sort
                .desc() / / reverse
                .list();
        // Outputs the information defined by the process
        for (ProcessDefinition processDefinition : list) {
            System.out.println("Process definition ID:" + processDefinition.getId());
            System.out.println("Process definition name:" + processDefinition.getName());
            System.out.println("Key for process definition :" + processDefinition.getKey());
            System.out.println("Version of process definition :" + processDefinition.getVersion());
            System.out.println("Process deployment ID :"+ processDefinition.getDeploymentId()); }}Copy the code

The output

ID of a process definition: EVection :1:12504 Name of a process definition: business trip application key: Version of an EVection process definition :1 ID of a process deployment :12501Copy the code

6 Process deletion

    /**
     * 删除流程
     */
    @Test
    public void test09(a){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        // Delete the process definition, if the process definition already has a process instance started delete the error
        repositoryService.deleteDeployment("12501");
        // Set it to TRUE to delete the process definition in cascading mode. Even if the process has instances started, it can also be deleted. Set it to false to not delete the process in cascading mode.
        //repositoryService.deleteDeployment("12501",true);
    }
Copy the code

Note: The permission to delete intermediate links in project development is usually reserved for the super administrator.

7 Downloading flow resources

Now that our process resource files have been uploaded to the database, other users can download these resources locally from the database if they want to view them.

Solution:

  1. JDBC handles bloB data and reads cloB data.
  2. Use the Activiti API to do this.

To operate using activiti’s API we need to add a commons-io dependency

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
Copy the code

The implementation code

    /** * Read the resource file in the database */
    @Test
    public void test10(a) throws Exception{
        // 1. Get ProcessEngine object
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2. Get RepositoryService object
        RepositoryService repositoryService = engine.getRepositoryService();
        // 3. Get the query
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("evection")
                .singleResult();
        // 4. Obtain the process deployment ID
        String deploymentId = definition.getDeploymentId();
        // 5. Obtain the image information and BPMN information by repositoryService
        / / PNG images
        InputStream pngInput = repositoryService
                .getResourceAsStream(deploymentId, definition.getDiagramResourceName());
        // Stream of BPMN files
        InputStream bpmnInput = repositoryService
                .getResourceAsStream(deploymentId, definition.getResourceName());
        // 6. Save the file
        File filePng = new File("d:/evection.png");
        File fileBpmn = new File("d:/evection.bpmn");
        OutputStream pngOut = new FileOutputStream(filePng);
        OutputStream bpmnOut = new FileOutputStream(fileBpmn);

        IOUtils.copy(pngInput,pngOut);
        IOUtils.copy(bpmnInput,bpmnOut);

        pngInput.close();
        pngOut.close();
        bpmnInput.close();
        bpmnOut.close();
    }
Copy the code

8 Viewing process history information

Even after the process definition has been removed, the instance information of the process execution is still stored in Activiti’s act_hi_* related table structure from the previous analysis, so we can still query the history of the process execution, which can be viewed through the HistoryService

    /** * View process history information */
    @Test
    public void test11(a){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // To view history information, we need to use HistoryService
        HistoryService historyService = engine.getHistoryService();
        // Obtain the query object of the actinst table
        HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery();
        instanceQuery.processDefinitionId("evection:1:12504");
        instanceQuery.orderByHistoricActivityInstanceStartTime().desc();
        List<HistoricActivityInstance> list = instanceQuery.list();
        // Output the result of the query
        for (HistoricActivityInstance hi : list) {
            System.out.println(hi.getActivityId());
            System.out.println(hi.getActivityName());
            System.out.println(hi.getActivityType());
            System.out.println(hi.getAssignee());
            System.out.println(hi.getProcessDefinitionId());
            System.out.println(hi.getProcessInstanceId());
            System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); }}Copy the code

The output

Usertask3 Gm approval userTask Wangwu evection:1:12504 15001 ----------------------- userTask2 Manager approval userTask Lisi 1:12504 15001 ----------------------- userTask1 userTask zhansan evection:1:12504 15001 ----------------------- startevent1 Start startEvent null evection:1:12504 15001 -----------------------Copy the code

The basic operations of Activiti are covered here, and the next chapter will show you how to use them in detail. Welcome to focus on zan plus collection oh V_V