通过前面的学习,我们了解到activiti是通过 activiti.cfg.xml获取到 ProcessEnigneConfiguration对象,这个对象负责管理流程引擎的配置相关信息,通过这个配置管理对象,我们可以创建出ProcessEnigne流程引擎对象。通过流程引擎对象我们可以得到各种服务对象,如:
流程存储服务RepositoryService主要的功能如下:
在idea中通过前面的acitivi脚手架创一个新的module
创建完成后如下图所示:
接着我们在test目录下创建一个测试类
先建立一下这个类的雏形
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();
}
}
我们可以通过ctrl+repositoryService来查看一下这个核心api提供的接口,如下图,这些接口就是提供了前面说的主要功能。
下面对于一次部署多个流程定义文件进行测试代码的补充
@Test
public void test(){
RepositoryService repositoryService = activitiRule.getRepositoryService();
//创建一个流程部署对象,并部署两个流程定义文件
DeploymentBuilder deployment = repositoryService.createDeployment();
deployment.name("测试部署资源")
.addClasspathResource("comm2Level.bpmn20.xml")
.addClasspathResource("my-process.bpmn20.xml");
//创建部署对象
Deployment deploy = deployment.deploy();
logger.info("deploy ={}", deploy);
//创建一个部署查询对象
DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
//因为是一次部署,所以可以用 singleResult
Deployment deployment1 = deploymentQuery.deploymentId(deploy.getId()).singleResult();
logger.info("deploy ={}", deployment1);
//查询流程定义
List 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());
}
}
在上述代码中,通过一次部署两个流程定义文件,然后将流程定义内容打印出来,执行情况如下:
存储服务提供了流程的挂起和重新启动的接口代码如下:
@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());
//将流程定义挂起,这个时候如果去启动会报告异常
repositoryService.suspendProcessDefinitionById(processDefinition.getId());
try {
logger.info("开始启动");
activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
logger.info("启动成功");
} catch (Exception e) {
logger.info("启动失败");
logger.info(e.getMessage(),e);
}
//重新激活,并启动,这个时候可以启动成功
repositoryService.activateProcessDefinitionById(processDefinition.getId());
try {
logger.info("重新激活,并开始启动");
activitiRule.getRuntimeService().startProcessInstanceById(processDefinition.getId());
logger.info("启动成功");
} catch (Exception e) {
logger.info("启动失败");
logger.info(e.getMessage(),e);
}
}
执行日志如下:
接着我们可以对两个流程定义文件,进行多次部署,代码如下:
@Test
public void test() {
RepositoryService repositoryService = activitiRule.getRepositoryService();
//创建一个流程部署对象,并部署两个流程定义文件
DeploymentBuilder deployment = repositoryService.createDeployment();
deployment.name("测试部署资源")
.addClasspathResource("comm2Level.bpmn20.xml")
.addClasspathResource("my-process.bpmn20.xml");
//创建部署对象
Deployment deploy = deployment.deploy();
logger.info("deploy ={}", deploy);
//创建第二个流程部署对象,并部署两个流程定义文件
DeploymentBuilder deployment2 = repositoryService.createDeployment();
deployment2.name("测试部署资源2")
.addClasspathResource("comm2Level.bpmn20.xml")
.addClasspathResource("my-process.bpmn20.xml");
//创建部署对象
Deployment deploy2 = deployment2.deploy();
logger.info("deploy ={}", deploy2);
//创建一个部署查询对象
DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
//因为是一次部署,所以可以用 singleResult
//Deployment deployment1 = deploymentQuery.deploymentId(deploy.getId()).singleResult();
//因为部署了两次,所以代码上要做相应变化,返回list
List deployments = deploymentQuery.orderByDeploymenTime().asc().listPage(0, 100);
//遍历deployments-- 用iter方式
for (Deployment deployment1 : deployments) {
logger.info("deployment = {}", deployment1);
}
logger.info("deploymentlist size = {}", deployments.size());
//查询流程定义
List 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());
}
}
执行日志如下:
存储服务提供了指定流程的执行用户和用户组的接口
下面是测试代码中的相关测试函数
//测
@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());
//创建流程和用户,用户组关系
repositoryService.addCandidateStarterUser(processDefinition.getId(), "user");
repositoryService.addCandidateStarterGroup(processDefinition.getId(), "groupadmin");
//获取用户组关系信息
List identityLinksForProcessDefinition
= repositoryService.getIdentityLinksForProcessDefinition(processDefinition.getId());
for (IdentityLink identityLink : identityLinksForProcessDefinition) {
logger.info("identityLink = {}", identityLink);
}
//删除流程和用户组关系
repositoryService.deleteCandidateStarterGroup(processDefinition.getId(), "groupadmin");
repositoryService.deleteCandidateStarterUser(processDefinition.getId(), "user");
}
试指定用户组来启动
执行的情况如下