Activiti6.0 learning practice core API: repository service storage service

Through the previous study, we learned that activiti obtained the ProcessEnigneConfiguration object through activiti.cfg.xml, which is responsible for managing the configuration related information of the process engine. Through this configuration management object, we can create the ProcessEnigne process engine object. Through process engine objects, we can get various service objects, such as:

  1. Repository service (process storage service)
  2. Runtime service (Management runtime)
  3. TaskService (manage tasks in the process)
  4. IdentityService (administrative rights)
  5. FormService,
  6. Historyservice (query function after running)
  7. Management service (basic management of process engine)
  8. DynamicBpmService (new service of dynamic management process-6.0)

1,RepositoryService

The main functions of process storage service repository service are as follows:

  1. Services for managing process definition files xml and static resources
  2. Suspension and activation of specific processes
  3. Process definition start permission management
  4. Deployment builder
  5. Deployment file querydeploymentquery
  6. Process definition query object ProcessDefinition
  7. BpmnModel: the java format of process definition

1.1 create a new module

Create a new module through the acitivi scaffold in idea

After creation, it is shown as follows:

1.2 create a repository service test class

Then we create a test class in the test directory

Let's build a prototype of this class

package com.study.activiti.coreapi;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Describe:
 *
 * @author cwqsolo
 * @date 2020/01/31
 */
public class RerostoryServiceTest {

    private static final Logger logger = LoggerFactory.getLogger(RerostoryServiceTest.class);

    @Rule
    public ActivitiRule  activitiRule = new ActivitiRule();

    @Test
    public void  test(){
        RepositoryService repositoryService = activitiRule.getRepositoryService();
    }


}

We can check the interfaces provided by the core api through Ctrl + repository service, as shown in the figure below. These interfaces provide the main functions mentioned above.

1.3 improve test code

The following is a supplement to the test code for deploying multiple process definition files at one time

@Test
public void  test(){
    RepositoryService repositoryService = activitiRule.getRepositoryService();

    //Create a process deployment object and deploy two process definition files
    DeploymentBuilder deployment = repositoryService.createDeployment();
    deployment.name("Test deployment resources")
            .addClasspathResource("comm2Level.bpmn20.xml")
            .addClasspathResource("my-process.bpmn20.xml");

    //Create deployment object
    Deployment deploy = deployment.deploy();
    logger.info("deploy ={}", deploy);

    //Create a deployment query object
    DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
    //Because it is a deployment, you can use singleResult
    Deployment deployment1 = deploymentQuery.deploymentId(deploy.getId()).singleResult();

    logger.info("deploy ={}", deployment1);

    //Query process definition
    List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery()
            .deploymentId(deploy.getId())
            .listPage(0, 100);

    for (ProcessDefinition processDefinition: processDefinitions) {
        logger.info("processDefinition = {}, version = {}, key={}, id={}",  processDefinition,
                processDefinition.getVersion(),
                processDefinition.getKey(),
                processDefinition.getId());
    }


}

In the above code, two process definition files are deployed at one time, and then the content of process definition is printed out. The execution is as follows:

1.4 suspension and restart of process

The storage service provides the following interface codes for suspending and restarting processes:

@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testSuspend() {
    RepositoryService repositoryService = activitiRule.getRepositoryService();
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
    logger.info("processDefinition.id={}", processDefinition.getId());

    //Suspend the process definition. At this time, if you start it, an exception will be reported
    repositoryService.suspendProcessDefinitionById(processDefinition.getId());

    try {
        logger.info("Start up");
        activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
        logger.info("Startup success");
    } catch (Exception e) {
        logger.info("Startup failure");
        logger.info(e.getMessage(),e);
    }
    //Reactivate and start it. At this time, it can start successfully
    repositoryService.activateProcessDefinitionById(processDefinition.getId());
    try {
        logger.info("Reactivate and start");
        activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
        logger.info("Startup success");
    } catch (Exception e) {
        logger.info("Startup failure");
        logger.info(e.getMessage(),e);
    }


}

The execution log is as follows:

1.5 multiple deployments

Then we can deploy the two process definition files multiple times. The code is as follows:

@Test
public void test() {
    RepositoryService repositoryService = activitiRule.getRepositoryService();

    //Create a process deployment object and deploy two process definition files
    DeploymentBuilder deployment = repositoryService.createDeployment();
    deployment.name("Test deployment resources")
            .addClasspathResource("comm2Level.bpmn20.xml")
            .addClasspathResource("my-process.bpmn20.xml");
    //Create deployment object
    Deployment deploy = deployment.deploy();
    logger.info("deploy ={}", deploy);

    //Create a second process deployment object and deploy two process definition files
    DeploymentBuilder deployment2 = repositoryService.createDeployment();
    deployment2.name("Test deployment resource 2")
            .addClasspathResource("comm2Level.bpmn20.xml")
            .addClasspathResource("my-process.bpmn20.xml");
    //Create deployment object
    Deployment deploy2 = deployment2.deploy();
    logger.info("deploy ={}", deploy2);

    //Create a deployment query object
    DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
    //Because it is a deployment, you can use singleResult
    //Deployment deployment1 = deploymentQuery.deploymentId(deploy.getId()).singleResult();
    //Since it has been deployed twice, you need to make corresponding changes in the code and return to list
    List<Deployment> deployments = deploymentQuery.orderByDeploymenTime().asc().listPage(0, 100);

    //Traverse deployments -- in iter mode
    for (Deployment deployment1 : deployments) {
        logger.info("deployment = {}", deployment1);
    }

    logger.info("deploymentlist size = {}", deployments.size());


    //Query process definition
    List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery()
            //.deploymentId(deploy.getId())
            .orderByProcessDefinitionKey().asc()
            .listPage(0, 100);

    for (ProcessDefinition processDefinition : processDefinitions) {
        logger.info("processDefinition = {}, version = {}, key={}, id={}", processDefinition,
                processDefinition.getVersion(),
                processDefinition.getKey(),
                processDefinition.getId());
    }


}

 

The execution log is as follows:

 

1.6 specify users and user groups for process execution

The storage service provides the interface between the execution user and the user group of the specified process

Here are the related test functions in the test code

/ / measurement

@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testCandiateStarter() {
    RepositoryService repositoryService = activitiRule.getRepositoryService();
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
    logger.info("processDefinition.id={}", processDefinition.getId());

    //Create process and user, user group relationship
    repositoryService.addCandidateStarterUser(processDefinition.getId(), "user");
    repositoryService.addCandidateStarterGroup(processDefinition.getId(), "groupadmin");

    //Get user group relationship information
    List<IdentityLink> identityLinksForProcessDefinition
            = repositoryService.getIdentityLinksForProcessDefinition(processDefinition.getId());

    for (IdentityLink identityLink : identityLinksForProcessDefinition) {
        logger.info("identityLink = {}", identityLink);

    }

    //Delete process and user group relationships
    repositoryService.deleteCandidateStarterGroup(processDefinition.getId(), "groupadmin");
    repositoryService.deleteCandidateStarterUser(processDefinition.getId(), "user");


}

Try specifying user groups to start

The implementation is as follows

117 original articles published, 82 praised, 440000 visitors+
His message board follow

Tags: xml Junit Java

Posted on Wed, 05 Feb 2020 03:24:25 -0800 by nocniagenti