其他分享
首页 > 其他分享> > Activiti7进阶之流程实例

Activiti7进阶之流程实例

作者:互联网

1 什么是流程实例?

流程定义和流程实例的图解

2 启动流程实例

package com.sunxiaping;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-01 21:13
 */
public class ActivitiStartProcessInstanceTest {
    /**
     * act_hi_actinst 活动实例
     * act_hi_identitylink 参与者信息
     * act_hi_procinst 流程实例
     * act_hi_taskinst 任务实例
     * act_ru_execution 执行表
     * act_ru_identitylink 参与者信息
     * act_ru_task   任务表
     */
    @Test
    public void test() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //根据流程定义的key启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday");
        //获取流程实例的相关信息
        String processDefinitionId = processInstance.getProcessDefinitionId();
        System.out.println("流程定义的id = " + processDefinitionId);
        String deploymentId = processInstance.getDeploymentId();
        System.out.println("流程部署的id = " + deploymentId);
        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
        String activityId = processInstance.getActivityId();
        System.out.println("当前活动的id = " + activityId);

    }
}

3 BusinessKey(业务标识)

package com.sunxiaping;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-01 21:13
 */
public class ActivitiBusinessKeyTest {

    @Test
    public void test() {
        String processDefinitionKey = "holiday";
        String businessKey = "1";

        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();

        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
        
        String businessKeyDb = processInstance.getBusinessKey();
        System.out.println("businessKeyDb = " + businessKeyDb);

    }
}

4 查询流程实例(关联businessKey)

5 流程实例挂起和激活

5.1 概述

5.2全部流程实例挂起和激活

package com.sunxiaping;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-03 20:02
 */
public class ActivitiAllProcessSuspendActivateTest {
    @Test
    public void test() {
        //获取ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        //获取ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        //获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //获取指定key的流程定义
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().processDefinitionKey("holiday").list();
        for (ProcessDefinition processDefinition : processDefinitionList) {
            //如果该流程定义是暂停的
            boolean suspended = processDefinition.isSuspended();
            if (suspended) {
                //如果该流程定义是暂停的,则激活该流程定义下的所有流程实例
                //第一个参数,表示流程定义的id
                //第二个参数,表示是否级联激活该流程定义的流程实例
                //第三个参数,表示激活这个流程定义的时间,如果不填写,从此刻开始
                repositoryService.activateProcessDefinitionById(processDefinition.getId(), true, null);

                System.out.println("流程定义" + processDefinition.getId() + "激活");
            } else {
                //如果该流程定义是激活的,则挂起该流程定义
                repositoryService.suspendProcessDefinitionById(processDefinition.getId(), true, null);

                System.out.println("流程定义" + processDefinition.getId() + "挂起");
            }
        }

    }
}

单个流程实例挂起,流程实例不能执行异常

5.3 单个流程实例挂起和激活

package com.sunxiaping;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-03 20:02
 */
public class ActivitiSingleProcessSuspendActivateTest {
    @Test
    public void test() {
        String processInstanceId = "2501";

        //获取ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        //获取ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();

        RuntimeService runtimeService = processEngine.getRuntimeService();

        List<ProcessInstance> processInstanceList = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).list();
        for (ProcessInstance processInstance : processInstanceList) {
            boolean suspended = processInstance.isSuspended();
            if (suspended) {
                runtimeService.activateProcessInstanceById(processInstance.getProcessInstanceId());
                System.out.println("流程实例" + processInstance.getProcessDefinitionId() + "激活");
            } else {
                runtimeService.suspendProcessInstanceById(processInstance.getProcessInstanceId());
                System.out.println("流程实例" + processInstance.getProcessDefinitionId() + "挂起");
            }
        }

    }
}

单个流程实例挂起,流程实例不能执行异常

标签:engine,进阶,实例,activiti,流程,Activiti7,org,import
来源: https://www.cnblogs.com/xuweiweiwoaini/p/13660402.html