Skip to content

Commit

Permalink
Pass workflow-module-id to adapters & improve tenant assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanpelikan committed Dec 21, 2023
1 parent e19321d commit 5fa574c
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<dependency>
<groupId>io.vanillabp</groupId>
<artifactId>spring-boot-support</artifactId>
<version>1.0.6</version>
<version>1.0.7-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.vanillabp.springboot.adapter.AdapterConfigurationBase;
import io.vanillabp.springboot.adapter.SpringDataUtil;
import io.vanillabp.springboot.adapter.VanillaBpProperties;
import jakarta.annotation.PostConstruct;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.spring.application.SpringProcessApplication;
import org.camunda.bpm.spring.boot.starter.CamundaBpmAutoConfiguration;
Expand All @@ -29,7 +30,6 @@
import org.springframework.context.annotation.Lazy;
import org.springframework.data.repository.CrudRepository;

import jakarta.annotation.PostConstruct;
import java.math.BigInteger;
import java.util.function.Function;

Expand All @@ -44,7 +44,10 @@ public class Camunda7AdapterConfiguration extends AdapterConfigurationBase<Camun

@Value("${workerId}")
private String workerId;


@Value("${spring.application.name:@null}")
private String applicationName;

@Autowired
private SpringDataUtil springDataUtil; // ensure persistence is up and running

Expand Down Expand Up @@ -90,6 +93,7 @@ public Camunda7DeploymentAdapter camunda7DeploymentAdapter(
return new Camunda7DeploymentAdapter(
properties,
processApplication,
applicationName,
taskWiring,
processEngine);

Expand Down Expand Up @@ -193,6 +197,7 @@ public <DE> Camunda7ProcessService<?> newProcessServiceImplementation(

final var result = new Camunda7ProcessService<DE>(
applicationEventPublisher,
applicationName,
processEngine,
workflowAggregate ->
!springDataUtil.isPersistedEntity(workflowAggregateClass, workflowAggregate),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ public class Camunda7DeploymentAdapter extends ModuleAwareBpmnDeployment {

private final Camunda7TaskWiring taskWiring;

private final String applicationName;

public Camunda7DeploymentAdapter(
final VanillaBpProperties properties,
final SpringProcessApplication processApplication,
final String applicationName,
final Camunda7TaskWiring taskWiring,
final ProcessEngine processEngine) {

super(properties);
this.processEngine = processEngine;
this.processApplication = processApplication;
this.taskWiring = taskWiring;
this.applicationName = applicationName;

}

Expand Down Expand Up @@ -67,21 +71,22 @@ public void deployAllWorkflowModules() {
@Override
protected void doDeployment(
final String workflowModuleId,
final String workflowModuleName,
final Resource[] bpmns,
final Resource[] dmns,
final Resource[] cmms)
throws Exception {

final var tenantId = workflowModuleId == null ? applicationName : workflowModuleId;

final var deploymentBuilder = processEngine
.getRepositoryService()
.createDeployment(processApplication.getReference())
.resumePreviousVersions()
.resumePreviousVersionsBy(ResumePreviousBy.RESUME_BY_DEPLOYMENT_NAME)
.enableDuplicateFiltering(true)
.source(applicationName)
.tenantId(workflowModuleName)
.name(workflowModuleName);
.tenantId(tenantId)
.name(workflowModuleId == null ? applicationName : workflowModuleId);

boolean hasDeployables = false;

Expand Down Expand Up @@ -120,7 +125,7 @@ protected void doDeployment(
processEngine
.getRepositoryService()
.createProcessDefinitionQuery()
.tenantIdIn(workflowModuleName)
.tenantIdIn(tenantId)
.list()
.stream()
.forEach(definition -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ public class Camunda7ProcessService<DE>

private final Function<String, Object> parseWorkflowAggregateIdFromBusinessKey;

private final String applicationName;

private AdapterAwareProcessService<DE> parent;

public Camunda7ProcessService(
final ApplicationEventPublisher applicationEventPublisher,
final String applicationName,
final ProcessEngine processEngine,
final Function<DE, Boolean> isNewEntity,
final Function<DE, ?> getWorkflowAggregateId,
Expand All @@ -47,6 +50,7 @@ public Camunda7ProcessService(

super();
this.applicationEventPublisher = applicationEventPublisher;
this.applicationName = applicationName;
this.processEngine = processEngine;
this.workflowAggregateRepository = workflowAggregateRepository;
this.workflowAggregateClass = workflowAggregateClass;
Expand Down Expand Up @@ -146,11 +150,12 @@ public DE startWorkflow(
// Hint: this is not done by setting "async-before" on the start-event
// since we don't know which process is used as a call-activity which
// has to be started synchronously.
final var tenantId = parent.getWorkflowModuleId() == null ? applicationName : parent.getWorkflowModuleId();
((ProcessEngineConfigurationImpl) processEngine
.getProcessEngineConfiguration())
.getCommandExecutorTxRequired()
.execute(new StartProcessCommand(
parent.getWorkflowModuleId(),
tenantId,
parent.getPrimaryBpmnProcessId(),
id));

Expand Down Expand Up @@ -231,10 +236,12 @@ private DE correlateMessage(

final var id = getWorkflowAggregateId
.apply(attachedAggregate);


final var tenantId = parent.getWorkflowModuleId() == null ? applicationName : parent.getWorkflowModuleId();
final var correlation = processEngine
.getRuntimeService()
.createMessageCorrelation(messageName)
.tenantId(tenantId)
.processInstanceBusinessKey(id.toString());
if (correlationIdLocalVariableName != null) {
correlation.localVariableEquals(
Expand All @@ -260,6 +267,7 @@ private DE correlateMessage(
final var correlationExecutions = processEngine
.getRuntimeService()
.createExecutionQuery()
.tenantIdIn(tenantId)
.messageEventSubscriptionName(messageName)
.processInstanceBusinessKey(id.toString())
.active();
Expand Down Expand Up @@ -307,11 +315,13 @@ public DE completeUserTask(

final var attachedAggregate = workflowAggregateRepository
.save(workflowAggregate);


final var tenantId = parent.getWorkflowModuleId() == null ? applicationName : parent.getWorkflowModuleId();
final var id = getWorkflowAggregateId.apply(workflowAggregate);
final var task = processEngine
.getTaskService()
.createTaskQuery()
.tenantIdIn(tenantId)
.processInstanceBusinessKey(id.toString())
.taskId(taskId)
.singleResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class StartProcessCommand implements Command<String> {
private final StartProcessJobHandlerConfiguration configuration;

public StartProcessCommand(
final String workflowModuleId,
final String tenantId,
final String bpmnProcessId,
final String businessKey) {

this.configuration = new StartProcessJobHandlerConfiguration(
workflowModuleId,
tenantId,
bpmnProcessId,
businessKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void execute(
.getRuntimeService()
.createProcessInstanceByKey(configuration.getBpmnProcessId())
.businessKey(configuration.getBusinessKey())
.processDefinitionTenantId(configuration.getWorkflowModuleId())
.processDefinitionTenantId(configuration.getTenantId())
.execute();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ public class StartProcessJobHandlerConfiguration implements JobHandlerConfigurat

private final String businessKey;

private final String workflowModuleId;
private final String tenantId;

private final String bpmnProcessId;

public StartProcessJobHandlerConfiguration(
final String workflowModuleId,
final String tenantId,
final String bpmnProcessId,
final String businessKey) {

this.businessKey = businessKey;
this.workflowModuleId = workflowModuleId;
this.tenantId = tenantId;
this.bpmnProcessId = bpmnProcessId;

}

@Override
public String toCanonicalString() {

return workflowModuleId + "\n" + bpmnProcessId + "\n" + businessKey;
return tenantId + "\n" + bpmnProcessId + "\n" + businessKey;

}

public String getWorkflowModuleId() {
return workflowModuleId;
public String getTenantId() {
return tenantId;
}

public String getBpmnProcessId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package io.vanillabp.camunda7.wiring;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;

import io.vanillabp.camunda7.service.Camunda7ProcessService;
import io.vanillabp.spi.process.ProcessService;
import io.vanillabp.spi.service.WorkflowTask;
import io.vanillabp.springboot.adapter.TaskWiringBase;
import io.vanillabp.springboot.parameters.MethodParameter;
import io.vanillabp.springboot.parameters.MethodParameterFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;

@Component
public class Camunda7TaskWiring extends TaskWiringBase<Camunda7Connectable, Camunda7ProcessService<?>, MethodParameterFactory> {
Expand Down Expand Up @@ -47,6 +46,7 @@ protected Class<WorkflowTask> getAnnotationType() {
@Override
@SuppressWarnings("unchecked")
protected void connectToBpms(
final String workflowModuleId,
final Camunda7ProcessService<?> processService,
final Object bean,
final Camunda7Connectable connectable,
Expand Down Expand Up @@ -128,6 +128,7 @@ public void validateWiring() {
}

protected void wireTask(
final String workflowModuleId,
final Camunda7ProcessService<?> processService,
final Camunda7Connectable connectable) {

Expand All @@ -137,7 +138,7 @@ protected void wireTask(
(method, annotation) -> methodMatchesTaskDefinition(connectable, method, annotation),
(method, annotation) -> methodMatchesElementId(connectable, method, annotation),
(method, annotation) -> validateParameters(processService, method),
(bean, method, parameters) -> connectToBpms(processService, bean, connectable, method, parameters));
(bean, method, parameters) -> connectToBpms(workflowModuleId, processService, bean, connectable, method, parameters));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public void parseRootElement(
oldVersionBpmn.get().booleanValue() ? null : tbw.messageBasedStartEventsMessages,
oldVersionBpmn.get().booleanValue() ? null : tbw.signalBasedStartEventsSignals);
tbw.connectables
.forEach(connectable -> taskWiring.wireTask(processService, connectable));
.forEach(connectable -> taskWiring.wireTask(workflowModuleId.get(), processService, connectable));
});

}
Expand Down

0 comments on commit 5fa574c

Please sign in to comment.