租户

suaxi
2024-07-13 / 0 评论 / 221 阅读 / 正在检测是否收录...

多租户是指一个单一的Camunda应用需要为多个租户服务的情况,对于每个租户来说,应该有某些隔离的保证,例如:A租户的流程实例不应该干扰B租户的流程实例

多租户可以通过两种不同的方式实现:

  • 一个租户使用一个流程引擎
  • 多个租户使用同一个流程引擎(将数据与租户标识符相关联)

此处以第二种方式为例

1.介绍-多个租户使用同一个流程引擎.png

1. 租户管理

package com.sw.camundademo;

import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.identity.Tenant;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Author Suaxi
 * @Date 2024/7/11 22:09
 * @Description
 */
@SpringBootTest
public class FlowTenantTest {

    @Autowired
    private IdentityService identityService;

    /**
     * 创建租户
     */
    @Test
    public void createTenant() {
        Tenant tenant = identityService.newTenant("tenant3");
        tenant.setName("租户3");
        identityService.saveTenant(tenant);
    }

    /**
     * 更新租户
     */
    @Test
    public void updateTenant() {
        Tenant tenant = identityService.createTenantQuery().tenantId("tenant1").singleResult();
        tenant.setName("租户1-更新测试");
        identityService.saveTenant(tenant);
    }

    /**
     * 删除租户
     */
    @Test
    public void deleteTenant() {
        identityService.deleteTenant("tenant3");
    }

    /**
     * 绑定租户和组的关系
     */
    @Test
    public void tenantGroupShip() {
        identityService.createTenantGroupMembership("bj", "userGroup");
        identityService.createTenantGroupMembership("bj", "userGroup1");
        identityService.createTenantGroupMembership("sh", "userGroup1");
        identityService.createTenantGroupMembership("sh", "userGroup2");
        identityService.createTenantGroupMembership("sz", "userGroup");
        identityService.createTenantGroupMembership("sz", "userGroup2");
    }
}


2. 流程部署

(1)流程图

2.财务审批-租户-流程图.png

(2)部署流程及流程相关数据的查询

/**
 * 部署流程
 * 1.部署时关联租户 - 租户特有
 * 2.部署时不关联租户 - 所有用户都有
 */
@Test
public void deploy() {
    Deployment deploy = repositoryService.createDeployment()
        .name("财务审批-租户")
        .addClasspathResource("flow/09.财务审批-租户.bpmn")
        .tenantId("bj")
        .deploy();
    System.out.println(deploy.getId());
}

@Test
public void deployWithTenant() {
    Deployment deploy = repositoryService.createDeployment()
        .name("财务审批-租户(不关联租户)")
        .addClasspathResource("flow/09.财务审批-租户.bpmn")
        //.tenantId("bj")
        .deploy();
    System.out.println(deploy.getId());
}

/**
 * 根据租户ID查询流程部署
 */
@Test
public void queryDeploymentByTenantId() {
    List<Deployment> deploymentList = repositoryService.createDeploymentQuery()
        .tenantIdIn("bj")
        .list();
    if (deploymentList != null && deploymentList.size() > 0) {
        for (Deployment deployment : deploymentList) {
            System.out.println(deployment.getId() + " - " + deployment.getName());

        }
    }
}

/**
 * 根据租户ID查询流程定义
 */
@Test
public void queryProcessDefinitionByTenantId() {
    List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
        .tenantIdIn("bj")
        .list();
    if (processDefinitionList != null && processDefinitionList.size() > 0) {
        for (ProcessDefinition processDefinition : processDefinitionList) {
            System.out.println(processDefinition.getId() + " - " + processDefinition.getName());

        }
    }
}

/**
 * 查询非租户的流程定义
 */
@Test
public void queryProcessDefinitionWithoutTenantId() {
    List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
        .withoutTenantId()
        .list();
    if (processDefinitionList != null && processDefinitionList.size() > 0) {
        for (ProcessDefinition processDefinition : processDefinitionList) {
            System.out.println(processDefinition.getId() + " - " + processDefinition.getName());

        }
    }
}

/**
 * 查询租户和非租户的流程定义
 */
@Test
public void queryAllProcessDefinition() {
    List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
        .includeProcessDefinitionsWithoutTenantId()
        .tenantIdIn("bj")
        .list();
    if (processDefinitionList != null && processDefinitionList.size() > 0) {
        for (ProcessDefinition processDefinition : processDefinitionList) {
            System.out.println(processDefinition.getId() + " - " + processDefinition.getName());

        }
    }
}


(3)启动流程实例

@Test
public void startFlow() {
    //根据 processInstanceKey、tenantId 启动流程实例
    String processInstanceKey = "Process_1xvsrpm";
    ProcessInstance processInstance = runtimeService.createProcessInstanceByKey(processInstanceKey)
        .processDefinitionTenantId("bj")
        .setVariable("group1", "userGroup")
        .execute();
    System.out.println("processInstance.getId() = " + processInstance.getId());

    //根据 processInstanceKey 启动流程实例
    Map<String, Object> variables = new HashMap<>();
    variables.put("group1", "userGroup");
    runtimeService.startProcessInstanceByKey(processInstanceKey, variables);
}


(4)拾取任务

/**
 * 查询当前登录用户待审批的任务(租户)
 */
@Test
public void queryCurrentUserTask() {
    List<Group> groupList = identityService.createGroupQuery()
        .groupMember("admin")
        .list();

    if (groupList != null && groupList.size() > 0) {
        for (Group group : groupList) {
            List<Tenant> tenantList = identityService.createTenantQuery()
                .groupMember(group.getId()).list();
            if (tenantList != null && tenantList.size() > 0) {
                //实际使用时建议单独写sql处理此处的查询
                String[] tenantIdArr = new String[tenantList.size()];
                for (int i = 0; i < tenantList.size(); i++) {
                    tenantIdArr[i] = tenantList.get(i).getId();
                }
                List<Task> taskList = taskService.createTaskQuery()
                    .tenantIdIn(tenantIdArr)
                    .list();
                if (taskList != null && taskList.size() > 0) {
                    for (Task task : taskList) {
                        System.out.println(task.getId() + " - " + task.getName());
                    }
                }
            }
        }
    }
}

/**
 * 拾取任务
 */
@Test
public void claimTask() {
    List<Task> taskList = taskService.createTaskQuery()
        .tenantIdIn("bj")
        //.taskCandidateGroup()
        .list();
    if (taskList != null && taskList.size() > 0) {
        for (Task task : taskList) {
            taskService.claim(task.getId(), "admin");
        }
    }
}
0

评论 (0)

取消