Outlined in this chapter

Process engines and services

The process engine stores services

RepositoryService

  • Manage process definition file XML and static services
  • Pausing and activating a particular process
  • Process defines startup permission management
  • Deploy file builder DeploymentBuilder
  • Deploy file query DeploymentQuery
  • The process definition file query object ProcessDefinitionQuery
  • The process Deployment file object Deployment
  • The process definition file object ProcessDefinittion
  • Java format for process definition BpmnModel(BpmnModel is a pure Java data structure List or Map)

Process RepositoryService -RepositoryService

  • RepostoryServiceTest

     private static final Logger LOGGER = LoggerFactory.getLogger(RepostoryServiceTest.class);
    
        @Rule
        public ActivitiRule activitiRule = new ActivitiRule();
    
        @Test
        public void testRepostory(a) {
            RepositoryService repositoryService = activitiRule.getRepositoryService();
            // First deployment
            DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
            deploymentBuilder.name("Test deployment resources")
                    .addClasspathResource("my-process.bpmn20.xml")
                    .addClasspathResource("second_approve.bpmn20.xml");
            // Second deployment
            DeploymentBuilder deployment1 = repositoryService.createDeployment();
            deployment1.name("Test deployment resources")
                    .addClasspathResource("my-process.bpmn20.xml")
                    .addClasspathResource("second_approve.bpmn20.xml");
            deployment1.deploy();
    
            Deployment deploy = deploymentBuilder.deploy();
            LOGGER.info(" deploy ={}", deploy);
            DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
            List<Deployment> deployments = deploymentQuery
    // .deploymentId(deploy.getId())
                    .orderByDeploymenTime().asc()
                    .listPage(0.100);
            for (Deployment deployment : deployments) {
                LOGGER.info("deployment = {}", deployment);
            }
            LOGGER.info("deployments.size() = {}", deployments.size());
            List<ProcessDefinition> definitionList = repositoryService.createProcessDefinitionQuery()
    // .deploymentId(deployment.getId())
                    .orderByProcessDefinitionKey().asc()
                    .listPage(0.100);
            for (ProcessDefinition processDefinition : definitionList) {
                LOGGER.info("processDefinition= {},version ={}, key = {},id ={}", processDefinition, processDefinition.getVersion() , processDefinition.getKey() , proce ssDefinition.getId()); }}Copy the code
  • The test results

  • The following figure shows the six generated files

  • testSuspend
 @Test
    @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
    public void testSuspend(a) {
        RepositoryService repositoryService = activitiRule.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .singleResult();
        LOGGER.info("processDefinition.id = {}", processDefinition.getId());
        repositoryService.suspendProcessDefinitionById(processDefinition.getId());
        try {
            LOGGER.info("Start up");
            activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
            LOGGER.info("Startup successful");

        } catch (Exception e) {
            LOGGER.info("Startup failed");
            LOGGER.info(e.getMessage(), e);
        }
        repositoryService.activateProcessDefinitionById(processDefinition.getId());
        LOGGER.info("Start up");
        activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
        LOGGER.info("Startup successful");
    }

Copy the code

    @Test
    @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
    public void testCandidateStarter(a) {
        RepositoryService repositoryService = activitiRule.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .singleResult();
        LOGGER.info("processDefinition.id = {}", processDefinition.getId());
        repositoryService.addCandidateStarterUser(processDefinition.getId(), "user");
        repositoryService.addCandidateStarterGroup(processDefinition.getId(), "groupM");
        List<IdentityLink> identityLinks = repositoryService.getIdentityLinksForProcessDefinition(processDefinition.getId());
        for (IdentityLink identityLink : identityLinks) {
            LOGGER.info("identityLink", identityLink);
        }
        repositoryService.deleteCandidateStarterGroup(processDefinition.getId(), "groupM");
        repositoryService.deleteCandidateStarterUser(processDefinition.getId(), "user");
    }
Copy the code

Process run control service

RuntimeService

  • Start process and control process data
  • ProcessInstance and Execution flow query
  • Triggers a process operation to receive messages and information

RuntimeService starts process and variable management

  • Common ways to start a process (ID, key, message)

  • Start a process optional parameters (businessKey varibles, teandId)

    • BusinessKey is unique
  • Set and get variables

  • The following code

    public class RuntimeServiceTest {
        private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeServiceTest.class);
    
        @Rule
        public ActivitiRule activitiRule = new ActivitiRule();
    
    
        @Test  // Start by key
        @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
        public void testStartProcess(a) {
            RuntimeService runtimeService = activitiRule.getRuntimeService();
            HashMap<String, Object> variables = Maps.newHashMap();
            variables.put("key1"."value1");
            ProcessInstance processInstance = runtimeService
                    .startProcessInstanceByKey("my-process", variables);
            LOGGER.info("processInstance = {}", processInstance);
        }
    
        @Test  // Start by id
        @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
        public void testStartProcessById(a) {
            RuntimeService runtimeService = activitiRule.getRuntimeService();
            ProcessDefinition processDefinition = activitiRule.getRepositoryService()
                    .createProcessDefinitionQuery().singleResult();
    
            HashMap<String, Object> variables = Maps.newHashMap();
            variables.put("key1"."value1");
            ProcessInstance processInstance = runtimeService
                    .startProcessInstanceById(processDefinition.getId(), variables);
            LOGGER.info("processInstance = {}", processInstance);
        }
    
        @Test  // Start according to the process instance
        @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
        public void testProcessInstanceBuilder(a) {
            RuntimeService runtimeService = activitiRule.getRuntimeService();
            HashMap<String, Object> variables = Maps.newHashMap();
            variables.put("key1"."value1");
            ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder();
            ProcessInstance processInstance = processInstanceBuilder.businessKey("businessKey001")
                    .processDefinitionKey("my-process")
                    .variables(variables)
                    .start();
            /*ProcessInstance processInstance = runtimeService .startProcessInstanceByKey("my-process", variables); * /
            LOGGER.info("processInstance = {}", processInstance);
        }
    
        @Test
        @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
        public void testVariables(a) {
            RuntimeService runtimeService = activitiRule.getRuntimeService();
            HashMap<String, Object> variables = Maps.newHashMap();
            variables.put("key1"."value1");
            variables.put("key2"."value2");
            ProcessInstance processInstance = runtimeService
                    .startProcessInstanceByKey("my-process", variables);
    
            runtimeService.setVariable(processInstance.getId(), "key3"."values3");
            runtimeService.setVariable(processInstance.getId(), "key2"."values2_1");
    
            Map<String, Object> variables1 = runtimeService.getVariables(processInstance.getId());
            LOGGER.info("variables1 = {}", variables1);
        }
    
        @Test
        @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
        public void testProcessInstanceQuery(a) {
            RuntimeService runtimeService = activitiRule.getRuntimeService();
            HashMap<String, Object> variables = Maps.newHashMap();
            variables.put("key1"."value1");
            variables.put("key2"."value2");
            ProcessInstance processInstance = runtimeService
                    .startProcessInstanceByKey("my-process", variables);
            LOGGER.info("processInstance = {}", processInstance);
            ProcessInstance processInstance1 = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(processInstance.getId()).singleResult();
            List<Execution> executions = runtimeService.createExecutionQuery()
                    .listPage(0.100);
            for (Execution execution : executions) {
                LOGGER.info("execution = {}", execution); }}}Copy the code

    Process instances and execution flows

  • A ProcessInstance represents a data entity for a workflow business

  • Execution flow represents a specific Execution path in a process instance

  • The process instance interface inherits execution flow

The process trigger

  • Use trigger to trigger the ReceiveTask node
  • The signal capture event signalEventReceived is triggered
  • The message capture event messageEventReceived is triggered

Process Trigger

  • Java code
@Test
    @org.activiti.engine.test.Deployment(resources = {"my-process-trigger.bpmn20.xml"})
    public void testTrigger(a) {
        RuntimeService runtimeService = activitiRule.getRuntimeService();
        // Start the process
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");

        Execution execution = runtimeService.createExecutionQuery().activityId("someTask").singleResult();

        LOGGER.info("execution = {}", execution);
        runtimeService.trigger(execution.getId());

        execution = runtimeService.createExecutionQuery().activityId("someTask").singleResult();
        LOGGER.info("execution = {}", execution);

    }
Copy the code
  • my-process-trigger.bpmn20.xml

     <process id="my-process">
    
            <startEvent id="start"/>
            <sequenceFlow id="flow1" sourceRef="start" targetRef="someTask"/>
    
            <! -- <userTask id="someTask" name="Activiti is awesome!" />-->
            <receiveTask id="someTask"/>
            <sequenceFlow id="flow2" sourceRef="someTask" targetRef="end"/>
    
            <endEvent id="end"/>
    
        </process>
    Copy the code

The process triggers signalEventRecvied

  • Java code

        @Test
        @org.activiti.engine.test.Deployment(resources = {"my-process-signal-received.bpmn20.xml"})
        public void testSignalEventReceived(a) {
            RuntimeService runtimeService = activitiRule.getRuntimeService();
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process");
            Execution execution = runtimeService.createExecutionQuery().signalEventSubscriptionName("my-signal").singleResult();
            LOGGER.info("execution = {}", execution);
            runtimeService.signalEventReceived("my-process");
            execution = runtimeService.createExecutionQuery().signalEventSubscriptionName("my-signal").singleResult();
            LOGGER.info("execution = {}", execution);
        }
    Copy the code

  • my-process-signal-received.bpmn20.xml

     <signal id="messageStart" name="my-message"></signal>
        <process id="my-process">
    
            <startEvent id="start"/>
            <sequenceFlow id="flow1" sourceRef="start" targetRef="message-received"/>
            <intermediateCatchEvent id="message-received">
                <signalEventDefinition signalRef="messageStart"/>
            </intermediateCatchEvent>
            <! -- <userTask id="someTask" name="Activiti is awesome!" />-->
            <sequenceFlow id="flow2" sourceRef="message-received" targetRef="end"/>
    
            <endEvent id="end"/>
    
        </process>
    Copy the code

The process triggers messageEventRecevied

  • Java code

        @Test
        @org.activiti.engine.test.Deployment(resources = {"my-process-message-received.bpmn20.xml"})
        public void testMessageEventReceived(a) {
            RuntimeService runtimeService = activitiRule.getRuntimeService();
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-message");
            Execution execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("my-message").singleResult();
            LOGGER.info("execution = {}", execution);
            runtimeService.signalEventReceived("my-process");
            execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("my-message").singleResult();
            LOGGER.info("execution = {}", execution);
        }
    Copy the code
  • my-process-message-received.bpmn20.xml

<signal id="messageStart" name="my-message"></signal>
    <process id="my-process">

        <startEvent id="start"/>
        <sequenceFlow id="flow1" sourceRef="start" targetRef="message-received"/>
        <intermediateCatchEvent id="message-received">
            <signalEventDefinition signalRef="messageStart"/>
        </intermediateCatchEvent>
        <! -- <userTask id="someTask" name="Activiti is awesome!" />-->
        <sequenceFlow id="flow2" sourceRef="message-received" targetRef="end"/>

        <endEvent id="end"/>

    </process>
Copy the code

Task Management Service

  • Manage user tasks and process control
  • Set permissions for UserTask (owner, candidate, handler)
  • Add task attachments, task comments, and event comments for user tasks

TaskService manages tasks and controls processes

  • Task object creation and deletion

  • Query tasks and drive the Task node to complete execution

  • Set Task parameters (variable)

  • Java code

     @Test
        @Deployment(resources = {"my-process-task.bpmn20.xml"})
        public void testTaskService(a) {
            HashMap<String, Object> variables = Maps.newHashMap();
            variables.put("message"."my test message!!!");
            ProcessInstance processInstance = activitiRule.getRuntimeService()
                    .startProcessInstanceByKey("my-process", variables);
    
            TaskService taskService = activitiRule.getTaskService();
            Task task = taskService.createTaskQuery().singleResult();
            LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));
            LOGGER.info("task.description = {}", task.getDescription());
    
        }
    Copy the code
  • my-process-task.bpmn20.xml

        <process id="my-process">
    
            <startEvent id="start"/>
            <sequenceFlow id="flow1" sourceRef="start" targetRef="someTask"/>
            <userTask id="someTask" name="Activiti is awesome!"
                      activiti:candidateUsers="jimmy,user1,user2">
                <documentation>some Task ${message}</documentation>
            </userTask>
            <sequenceFlow id="flow2" sourceRef="someTask" targetRef="end"/>
    
            <endEvent id="end"/>
    
        </process>
    Copy the code