This example demonstrates how to use multi-tenancy for a shared process engine. You learn
- How to deploy a process definition with a tenant-id,
- How to start a process instance from a process definition with a tenant-id,
- How to implement a service task which uses the tenant-id from the process instance,
- How to use multi-tenancy with Camunda Web Applications
The example process for the tenants looks like:
Please refer to the User Guide for details about multi-tenancy.
Create a processes.xml
deployment descriptor which includes one process-archive per tenant.
<process-application
xmlns="http://www.camunda.org/schema/1.0/ProcessApplication"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<process-archive name="tenant1-archive" tenantId="tenant1">
<process-engine>default</process-engine>
<properties>
<property name="resourceRootPath">classpath:processes/tenant1/</property>
<property name="isDeleteUponUndeploy">false</property>
<property name="isScanForProcessDefinitions">true</property>
</properties>
</process-archive>
<process-archive name="tenant2-archive" tenantId="tenant2">
<process-engine>default</process-engine>
<properties>
<property name="resourceRootPath">classpath:processes/tenant2/</property>
<property name="isDeleteUponUndeploy">false</property>
<property name="isScanForProcessDefinitions">true</property>
</properties>
</process-archive>
</process-application>
Implement a ServletProcessApplication
to start process instances when the application starts.
@ProcessApplication(name="Multi-Tenancy App")
public class MultiTenancyProcessApplication extends ServletProcessApplication {
@PostDeploy
public void startProcessInstances(ProcessEngine processEngine) {
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
// start a process instance for 'tenant1'
runtimeService
.createProcessInstanceByKey("example-process")
.processDefinitionTenantId("tenant1")
.execute();
// next, start a process instance for 'tenant2'
runtimeService
.createProcessInstanceByKey("example-process")
.processDefinitionTenantId("tenant2")
.execute();
}
}
Implement a service task as JavaDelegate
that can be used for multiple tenants. While execution, it retrieves the tenant-id from the execution (i.e. the process instance) and do some tenant specific logic.
public class TenantAwareServiceTask implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
String tenantId = execution.getTenantId();
// do some logic based on the tenant-id (e.g. invoke a tenant-aware service)
}
}
- Checkout the project with Git
- Import the project into your IDE
- Build it with Maven
- Deploy it to a camunda-bpm-platform distro at your own choice
- Check the console or the log file if you can find:
TenantAwareServiceTask.execute invoked for tenant with id: tenant1
andTenantAwareServiceTask.execute invoked for tenant with id: tenant2
- Open your browser and go to
http://localhost:8080/camunda/app/cockpit
- Log in with
demo
/demo
as member of groupcamunda-admin
- Check that you see two deployed process definitions with key
example-process
- one for each tenant - Switch to Admin and create a new user
- Go to tenant section and create a new tenant with id
tenant1
- Select the user and add him to the tenant
tenant1
- Make sure that the user has the permissions to read process definitions and access Cockpit Web Application
- Switch to Cockpit and log in with the new user
- Check that the user see only one deployed process definition with key
example-process