Activiti6.0流程引擎学习——(19)activiti的流程存储服务(RepositoryService)

这篇博客的测试文件结构是基于 https://blog.csdn.net/JJBOOM425/article/details/85069863 中创建的脚手架项目中进行测试的,首先要按照我们定义的脚手架创建maven工程,这里就不列举了,然后我们在test中创建 RepositoryServiceTest  测试类:

Activiti6.0流程引擎学习——(19)activiti的流程存储服务(RepositoryService)_第1张图片

测试类内容如下:

package com.jjf.activiti;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * test
 */
public class RepositoryServiceTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryServiceTest.class);

    @Rule
    public ActivitiRule activitiRule = new ActivitiRule();   //默认流程配置文件

    @Test
    public void testRepository(){
        RepositoryService repositoryService = activitiRule.getRepositoryService();  //获取RepositoryService对象

        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();   //流程部署对象
        deploymentBuilder.name("测试部署资源")
                .addClasspathResource("my-process.bpmn20.xml")
                .addClasspathResource("LeaveProcess.bpmn20.xml");    //部署两个流程定义配置文件

        Deployment deploy = deploymentBuilder.deploy();   //将部署对象与两个定义文件部署到数据库中
        LOGGER.info("deploy = [{}]",deploy);     //输出这个部署对象

        DeploymentBuilder deploymentBuilder2 = repositoryService.createDeployment();   //流程部署对象
        deploymentBuilder2.name("测试部署资源2")
                .addClasspathResource("my-process.bpmn20.xml")
                .addClasspathResource("LeaveProcess.bpmn20.xml");    //部署两个流程定义配置文件
        deploymentBuilder2.deploy();    //第二次部署

        DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();   //通过这个类查询
        Deployment deployment = deploymentQuery.deploymentId(deploy.getId()).singleResult();//根据deploy的ID查询出一个独立的对象
        LOGGER.info("deployment = [{}]",deployment);     //上下 deploy与deployment 输出的部署对象相同

        List definitions = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).listPage(0, 100);//使用流程定义的查询对象,使用listPage因为有两个资源文件

        for (ProcessDefinition processDefinition : definitions){
            LOGGER.info("processDefinition = [{}] , version = [{}] , key = [{}] , id = [{}]",
                    processDefinition,
                    processDefinition.getVersion(),
                    processDefinition.getKey(),
                    processDefinition.getId());
        }
    }
}

这里我们部署了两个流程定义配置文件( LeaveProcess.bpmn20.xml my-process.bpmn20.xml):

Activiti6.0流程引擎学习——(19)activiti的流程存储服务(RepositoryService)_第2张图片

这里的 LeaveProcess.bpmn20.xml  是我们之前创建的请假流程流程图,my-process.bpmn20.xml 是自带的流程图。

然后我们直接测试执行:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
deploy = [DeploymentEntity[id=1, name=测试部署资源]]
deployment = [DeploymentEntity[id=1, name=测试部署资源]]
processDefinition = [ProcessDefinitionEntity[LeaveProcess:1:6]] , version = [1] , key = [LeaveProcess] , id = [LeaveProcess:1:6]
processDefinition = [ProcessDefinitionEntity[my-process:1:5]] , version = [1] , key = [my-process] , id = [my-process:1:5]

Process finished with exit code 0

可以得到部署的两个流程定义文件。

接下去我修改测试类:

package com.jjf.activiti;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * test
 */
public class RepositoryServiceTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryServiceTest.class);

    @Rule
    public ActivitiRule activitiRule = new ActivitiRule();   //默认流程配置文件

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

        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();   //流程部署对象
        deploymentBuilder.name("测试部署资源1")
                .addClasspathResource("my-process.bpmn20.xml")
                .addClasspathResource("LeaveProcess.bpmn20.xml");    //部署两个流程定义配置文件

        Deployment deploy = deploymentBuilder.deploy();   //将部署对象与两个定义文件部署到数据库中
        LOGGER.info("deploy = [{}]",deploy);     //输出这个部署对象

        DeploymentBuilder deploymentBuilder2 = repositoryService.createDeployment();   //流程部署对象
        deploymentBuilder2.name("测试部署资源2")
                .addClasspathResource("my-process.bpmn20.xml")
                .addClasspathResource("LeaveProcess.bpmn20.xml");    //部署两个流程定义配置文件
        deploymentBuilder2.deploy();    //第二次部署

        DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();   //通过这个类查询
        List deploymentlist = deploymentQuery
                .orderByDeploymenTime().asc().listPage(0,100);    //根据deploy的部署时间进行排序查出所有的部署对象
        LOGGER.info("deployment = [{}]",deploymentlist);     //当通过ID查找时, deploy与deployment 输出的部署对象相同
        for (Deployment deployment:deploymentlist){
            LOGGER.info("deployment = [{}]",deployment);
        }
        LOGGER.info("deploymentlist.size = [{}]",deploymentlist.size());

        List definitions = repositoryService.createProcessDefinitionQuery()
                 .orderByProcessDefinitionKey().asc().listPage(0, 100);
        for (ProcessDefinition processDefinition : definitions){
            LOGGER.info("processDefinition = [{}] , version = [{}] , key = [{}] , id = [{}]",
                    processDefinition,
                    processDefinition.getVersion(),
                    processDefinition.getKey(),
                    processDefinition.getId());
        }
    }
}

测试执行:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
deploy = [DeploymentEntity[id=1, name=测试部署资源1]]
deployment = [[DeploymentEntity[id=1, name=测试部署资源1], DeploymentEntity[id=7, name=测试部署资源2]]]
deployment = [DeploymentEntity[id=1, name=测试部署资源1]]
deployment = [DeploymentEntity[id=7, name=测试部署资源2]]
deploymentlist.size = [2]
processDefinition = [ProcessDefinitionEntity[LeaveProcess:1:6]] , version = [1] , key = [LeaveProcess] , id = [LeaveProcess:1:6]
processDefinition = [ProcessDefinitionEntity[LeaveProcess:2:12]] , version = [2] , key = [LeaveProcess] , id = [LeaveProcess:2:12]
processDefinition = [ProcessDefinitionEntity[my-process:1:5]] , version = [1] , key = [my-process] , id = [my-process:1:5]
processDefinition = [ProcessDefinitionEntity[my-process:2:11]] , version = [2] , key = [my-process] , id = [my-process:2:11]

Process finished with exit code 0

可以看到两次deployment对象,分别对应 测试部署资源1 与测试部署资源2,id为1与7。在看下面部署了两次LeaveProcess与my-process,版本为1与2。我们看id,my-process是id = [my-process:1:5],LeaveProcess是id = [LeaveProcess:1:6]。可以知道这里是先部署了my-process再部署了请假流程文件,这个顺序跟addClasspathResource的顺序有关。

这里可能会有疑问为什么第二次部署id从7开始,而我们这里只有两个流程呀?

其实这里一次文件部署生成了6个文件:1个部署文件,2个流程定义文件,2个流程定义文件对应的数据流数据,1个请假流程定义文件中对应的图片。

可能又会问为什么my-process没有对应的图片文件呢?

因为my-process是基于xml构建简单的流程定义文件,并没有各种位点信息,没有生成它的图片。


接下来我们来实现定义一个流程定义文件,只能通过某个用户或用户组来执行

这里我们在测试类中添加 testCandidateStarter 函数来实现这一功能:

    /**
     * 测试用户与用户组与流程定义文件建立关系
     */
    @Test
    @org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
    public void testCandidateStarter(){
        RepositoryService repositoryService = activitiRule.getRepositoryService();

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

        LOGGER.info("processDefinition的ID: [{}]",processDefinition.getId());

        repositoryService.addCandidateStarterUser(processDefinition.getId(),"user");   //按照用户的ID(user)来添加用户
        repositoryService.addCandidateStarterGroup(processDefinition.getId(),"groupM");    //添加用户组

        List identityLinkList = repositoryService
                .getIdentityLinksForProcessDefinition(processDefinition.getId());    //通过流程定义获取我们设置的关系

        for (IdentityLink identityLink : identityLinkList){
            LOGGER.info("identityLink = [{}]",identityLink);    //日志输出设置的关系
        }

        repositoryService.deleteCandidateStarterGroup(processDefinition.getId(),"groupM");    //删除用户组
        repositoryService.deleteCandidateStarterUser(processDefinition.getId(),"user");    //删除user用户

    }

执行结果:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
processDefinition的ID: [my-process:1:3]
identityLink = [IdentityLinkEntity[id=4, type=candidate, userId=user, processDefId=my-process:1:3]]
identityLink = [IdentityLinkEntity[id=5, type=candidate, groupId=groupM, processDefId=my-process:1:3]]

Process finished with exit code 0

我们从日志输出中看到, 这里指定一个叫user的候选人与groupM的用户组。现在我们的这个my-process流程定义文件只能通过user与groupM进行访问。这个方法的最后我们使用了deleteCandidateStarterGroup 与 deleteCandidateStarterUser删除关系。

以上就是我们对RepositoryService存储服务的学习。

你可能感兴趣的:(activiti)