From 971c637f55df6b50b4abd78a828e69fd464442d3 Mon Sep 17 00:00:00 2001 From: Stephan Pelikan Date: Thu, 29 Aug 2024 08:48:46 +0200 Subject: [PATCH] Add logging MDC --- .../Camunda7AdapterConfiguration.java | 8 +- .../io/vanillabp/camunda7/LoggingContext.java | 65 +++ .../service/Camunda7ProcessService.java | 396 +++++++++++------- .../camunda7/wiring/Camunda7TaskHandler.java | 85 +++- .../camunda7/wiring/Camunda7TaskWiring.java | 24 +- .../wiring/Camunda7UserTaskHandler.java | 41 +- 6 files changed, 440 insertions(+), 179 deletions(-) create mode 100644 spring-boot/src/main/java/io/vanillabp/camunda7/LoggingContext.java diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java b/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java index afdd957..44309c9 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java @@ -13,6 +13,8 @@ import io.vanillabp.springboot.adapter.SpringDataUtil; import io.vanillabp.springboot.adapter.VanillaBpProperties; import jakarta.annotation.PostConstruct; +import java.math.BigInteger; +import java.util.function.Function; import org.camunda.bpm.engine.ProcessEngine; import org.camunda.bpm.engine.spring.application.SpringProcessApplication; import org.camunda.bpm.spring.boot.starter.CamundaBpmAutoConfiguration; @@ -32,9 +34,6 @@ import org.springframework.context.annotation.Lazy; import org.springframework.data.repository.CrudRepository; -import java.math.BigInteger; -import java.util.function.Function; - @AutoConfigurationPackage(basePackageClasses = Camunda7AdapterConfiguration.class) @AutoConfigureBefore(CamundaBpmAutoConfiguration.class) @EnableProcessApplication("org.camunda.bpm.spring.boot.starter.SpringBootProcessApplication") @@ -119,7 +118,8 @@ public Camunda7TaskWiring taskWiring( springBeanUtil, processEntityAwareExpressionManager, userTaskEventHandler, - getConnectableServices()); + getConnectableServices(), + camunda7Properties); } diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/LoggingContext.java b/spring-boot/src/main/java/io/vanillabp/camunda7/LoggingContext.java new file mode 100644 index 0000000..52cbfd8 --- /dev/null +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/LoggingContext.java @@ -0,0 +1,65 @@ +package io.vanillabp.camunda7; + +import org.slf4j.MDC; + +public class LoggingContext extends io.vanillabp.springboot.adapter.LoggingContext { + + /** + * The current workflow's tenant. Per default the same value as the workflow module's ID + * but may be overwritten by using Spring Boot properties. + * + * see Multi-tenancy + */ + public static final String WORKFLOW_TENANT_ID = "workflowTenantId"; + + public static void clearContext() { + + io.vanillabp.springboot.adapter.LoggingContext.clearContext(); + + MDC.remove(WORKFLOW_ADAPTER_ID); + MDC.remove(WORKFLOW_AGGREGATE_ID); + MDC.remove(WORKFLOW_BPM_ID); + MDC.remove(WORKFLOW_BPMN_ID); + MDC.remove(WORKFLOW_TASK_NODE); + MDC.remove(WORKFLOW_TASK_ID); + MDC.remove(WORKFLOW_TASK_NODE_ID); + MDC.remove(WORKFLOW_MODULE_ID); + MDC.remove(WORKFLOW_TENANT_ID); + + } + + public static void setLoggingContext( + final String adapterId, + final String tenantId, + final String workflowModuleId, + final String aggregateId, + final String bpmnId, + final String taskId, + final String bpmId, + final String taskNode, + final String taskNodeId) { + + MDC.put(WORKFLOW_ADAPTER_ID, adapterId); + MDC.put(WORKFLOW_AGGREGATE_ID, aggregateId); + MDC.put(WORKFLOW_BPM_ID, bpmId); + MDC.put(WORKFLOW_BPMN_ID, bpmnId); + MDC.put(WORKFLOW_TASK_NODE, taskNode); + MDC.put(WORKFLOW_TASK_ID, taskId); + MDC.put(WORKFLOW_TASK_NODE_ID, taskNodeId); + MDC.put(WORKFLOW_MODULE_ID, workflowModuleId); + MDC.put(WORKFLOW_TENANT_ID, tenantId); + + final var context = io.vanillabp.springboot.adapter.LoggingContext.getWriteableContext(); + context.put(WORKFLOW_TENANT_ID, tenantId); + context.put(WORKFLOW_ADAPTER_ID, adapterId); + context.put(WORKFLOW_AGGREGATE_ID, aggregateId); + context.put(WORKFLOW_BPM_ID, bpmId); + context.put(WORKFLOW_BPMN_ID, bpmnId); + context.put(WORKFLOW_TASK_NODE, taskNode); + context.put(WORKFLOW_TASK_ID, taskId); + context.put(WORKFLOW_TASK_NODE_ID, taskNodeId); + context.put(WORKFLOW_MODULE_ID, workflowModuleId); + + } + +} diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java b/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java index a6173b4..1346e90 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java @@ -2,9 +2,12 @@ import io.vanillabp.camunda7.Camunda7AdapterConfiguration; import io.vanillabp.camunda7.Camunda7VanillaBpProperties; +import io.vanillabp.camunda7.LoggingContext; import io.vanillabp.camunda7.service.jobs.startprocess.StartProcessCommand; import io.vanillabp.springboot.adapter.AdapterAwareProcessService; import io.vanillabp.springboot.adapter.ProcessServiceImplementation; +import java.util.Collection; +import java.util.function.Function; import org.camunda.bpm.engine.ProcessEngine; import org.camunda.bpm.engine.exception.NullValueException; import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl; @@ -13,9 +16,6 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.repository.CrudRepository; -import java.util.Collection; -import java.util.function.Function; - public class Camunda7ProcessService implements ProcessServiceImplementation { @@ -75,6 +75,13 @@ public Collection getBpmnProcessIds() { } + @Override + public String getPrimaryBpmnProcessId() { + + return parent.getPrimaryBpmnProcessId(); + + } + public void wire( final String workflowModuleId, final String bpmnProcessId, @@ -138,28 +145,47 @@ public Object getWorkflowAggregateIdFromBusinessKey( public DE startWorkflow( final DE workflowAggregate) throws Exception { - final var attachedAggregate = workflowAggregateRepository - .save(workflowAggregate); + try { + + final var attachedAggregate = workflowAggregateRepository + .save(workflowAggregate); + + final var aggregateId = getWorkflowAggregateId + .apply(attachedAggregate) + .toString(); + + final var tenantId = camunda7Properties.getTenantId(parent.getWorkflowModuleId()); + final var bpmnProcessId = parent.getPrimaryBpmnProcessId(); + LoggingContext.setLoggingContext( + Camunda7AdapterConfiguration.ADAPTER_ID, + camunda7Properties.getTenantId(parent.getWorkflowModuleId()), + parent.getWorkflowModuleId(), + aggregateId, + bpmnProcessId, + null, + null, + null, + null); + + wakeupJobExecutorOnActivity(); + + // Start workflow asynchronously by Camunda's job-executor + // 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. + ((ProcessEngineConfigurationImpl) processEngine + .getProcessEngineConfiguration()) + .getCommandExecutorTxRequired() + .execute(new StartProcessCommand( + tenantId, + bpmnProcessId, + aggregateId)); - final var id = getWorkflowAggregateId - .apply(attachedAggregate) - .toString(); - - wakeupJobExecutorOnActivity(); - - // Start workflow asynchronously by Camunda's job-executor - // 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. - ((ProcessEngineConfigurationImpl) processEngine - .getProcessEngineConfiguration()) - .getCommandExecutorTxRequired() - .execute(new StartProcessCommand( - camunda7Properties.getTenantId(parent.getWorkflowModuleId()), - parent.getPrimaryBpmnProcessId(), - id)); - - return attachedAggregate; + return attachedAggregate; + + } finally { + LoggingContext.clearContext(); + } } @@ -225,96 +251,115 @@ private DE correlateMessage( final String correlationIdLocalVariableName, final String correlationId) { - final var isNewEntity = this.isNewEntity - .apply(workflowAggregate); - - // persist to get ID in case of @Id @GeneratedValue - // and force optimistic locking exceptions before running - // the workflow if aggregate was already persisted before - final var attachedAggregate = workflowAggregateRepository - .save(workflowAggregate); - - final var id = getWorkflowAggregateId - .apply(attachedAggregate); - - final var tenantId = camunda7Properties.getTenantId(parent.getWorkflowModuleId()); - final var correlation = - (tenantId == null - ? processEngine - .getRuntimeService() - .createMessageCorrelation(messageName) - : processEngine - .getRuntimeService() - .createMessageCorrelation(messageName) - .tenantId(tenantId)) - .processInstanceBusinessKey(id.toString()); - if (correlationIdLocalVariableName != null) { - correlation.localVariableEquals( - correlationIdLocalVariableName, - correlationId); - } - - wakeupJobExecutorOnActivity(); - - if (isNewEntity) { - - final var result = correlation.correlateStartMessage(); - logger.trace("Started process '{}#{}' by message-correlation '{}' (tenant: {})", - parent.getPrimaryBpmnProcessId(), - result.getProcessInstanceId(), - messageName, - result.getTenantId()); - - return attachedAggregate; + try { + + final var isNewEntity = this.isNewEntity + .apply(workflowAggregate); + + // persist to get ID in case of @Id @GeneratedValue + // and force optimistic locking exceptions before running + // the workflow if aggregate was already persisted before + final var attachedAggregate = workflowAggregateRepository + .save(workflowAggregate); + + final var aggregateId = getWorkflowAggregateId + .apply(attachedAggregate) + .toString(); + + final var bpmnProcessId = parent.getPrimaryBpmnProcessId(); + final var tenantId = camunda7Properties.getTenantId(parent.getWorkflowModuleId()); + LoggingContext.setLoggingContext( + Camunda7AdapterConfiguration.ADAPTER_ID, + tenantId, + parent.getWorkflowModuleId(), + aggregateId, + bpmnProcessId, + null, + null, + null, + null); + + final var correlation = + (tenantId == null + ? processEngine + .getRuntimeService() + .createMessageCorrelation(messageName) + : processEngine + .getRuntimeService() + .createMessageCorrelation(messageName) + .tenantId(tenantId)) + .processInstanceBusinessKey(aggregateId); + if (correlationIdLocalVariableName != null) { + correlation.localVariableEquals( + correlationIdLocalVariableName, + correlationId); + } + + wakeupJobExecutorOnActivity(); + + if (isNewEntity) { + + final var result = correlation.correlateStartMessage(); + logger.trace("Started process '{}#{}' by message-correlation '{}' (tenant: {})", + bpmnProcessId, + result.getProcessInstanceId(), + messageName, + result.getTenantId()); + + return attachedAggregate; + + } - } - - final var correlationExecutions = - (tenantId == null - ? processEngine - .getRuntimeService() - .createExecutionQuery() - : processEngine - .getRuntimeService() - .createExecutionQuery() - .tenantIdIn(tenantId)) - .messageEventSubscriptionName(messageName) - .processInstanceBusinessKey(id.toString()) - .active(); - if (correlationIdLocalVariableName != null) { - correlationExecutions.variableValueEquals( - correlationIdLocalVariableName, - correlationId); - } - final var hasMessageCorrelation = correlationExecutions.count() == 1; - - if (!hasMessageCorrelation) { - - logger.trace("Message '{}' of process having bpmn-process-id '{}' could " - + "not be correlated using correlation-id '{}' for workflow aggregate '{}'!", + final var correlationExecutions = + (tenantId == null + ? processEngine + .getRuntimeService() + .createExecutionQuery() + : processEngine + .getRuntimeService() + .createExecutionQuery() + .tenantIdIn(tenantId)) + .messageEventSubscriptionName(messageName) + .processInstanceBusinessKey(aggregateId) + .active(); + if (correlationIdLocalVariableName != null) { + correlationExecutions.variableValueEquals( + correlationIdLocalVariableName, + correlationId); + } + final var hasMessageCorrelation = correlationExecutions.count() == 1; + + if (!hasMessageCorrelation) { + + logger.trace("Message '{}' of process having bpmn-process-id '{}' could " + + "not be correlated using correlation-id '{}' for workflow aggregate '{}'!", + messageName, + bpmnProcessId, + correlationId, + aggregateId); + + return attachedAggregate; + + } + + final var result = correlation + .correlateWithResult() + .getExecution(); + + logger.trace("Correlated message '{}' using correlation-id '{}' for process '{}#{}' " + + "and execution '{}' (tenant: {})", messageName, - parent.getPrimaryBpmnProcessId(), correlationId, - id); + bpmnProcessId, + result.getProcessInstanceId(), + result.getId(), + result.getTenantId()); return attachedAggregate; - - } - - final var result = correlation - .correlateWithResult() - .getExecution(); - - logger.trace("Correlated message '{}' using correlation-id '{}' for process '{}#{}' " - + "and execution '{}' (tenant: {})", - messageName, - correlationId, - parent.getPrimaryBpmnProcessId(), - result.getProcessInstanceId(), - result.getId(), - result.getTenantId()); - return attachedAggregate; + } finally { + LoggingContext.clearContext(); + } } @@ -323,37 +368,55 @@ public DE completeUserTask( final DE workflowAggregate, final String taskId) { - final var attachedAggregate = workflowAggregateRepository - .save(workflowAggregate); - - final var id = getWorkflowAggregateId.apply(workflowAggregate); - - final var tenantId = camunda7Properties.getTenantId(parent.getWorkflowModuleId()); - final var task = (tenantId == null - ? processEngine - .getTaskService() - .createTaskQuery() - : processEngine - .getTaskService() - .createTaskQuery() - .tenantIdIn(tenantId)) - .processInstanceBusinessKey(id.toString()) - .taskId(taskId) - .singleResult(); - - if (task == null) { - throw new NullValueException("Task '" - + taskId - + "' not found!"); - } - - wakeupJobExecutorOnActivity(); + try { + + final var attachedAggregate = workflowAggregateRepository + .save(workflowAggregate); + + final var aggregateId = getWorkflowAggregateId.apply(workflowAggregate).toString(); + + final var tenantId = camunda7Properties.getTenantId(parent.getWorkflowModuleId()); + final var task = (tenantId == null + ? processEngine + .getTaskService() + .createTaskQuery() + : processEngine + .getTaskService() + .createTaskQuery() + .tenantIdIn(tenantId)) + .processInstanceBusinessKey(aggregateId) + .taskId(taskId) + .singleResult(); + + if (task == null) { + throw new NullValueException("Task '" + + taskId + + "' not found!"); + } + + final var bpmnProcessId = parent.getPrimaryBpmnProcessId(); + LoggingContext.setLoggingContext( + Camunda7AdapterConfiguration.ADAPTER_ID, + tenantId, + parent.getWorkflowModuleId(), + aggregateId, + bpmnProcessId, + taskId, + task.getProcessInstanceId(), + task.getProcessDefinitionId() + "#" + task.getTaskDefinitionKey(), + task.getExecutionId()); + + wakeupJobExecutorOnActivity(); + + processEngine + .getTaskService() + .complete(taskId); - processEngine - .getTaskService() - .complete(taskId); - - return attachedAggregate; + return attachedAggregate; + + } finally { + LoggingContext.clearContext(); + } } @@ -381,17 +444,56 @@ public DE cancelUserTask( final DE workflowAggregate, final String taskId, final String errorCode) { - - final var attachedAggregate = workflowAggregateRepository - .save(workflowAggregate); - wakeupJobExecutorOnActivity(); - - processEngine - .getTaskService() - .handleBpmnError(taskId, errorCode); + try { + + final var attachedAggregate = workflowAggregateRepository + .save(workflowAggregate); + + final var aggregateId = getWorkflowAggregateId.apply(workflowAggregate).toString(); + + final var tenantId = camunda7Properties.getTenantId(parent.getWorkflowModuleId()); + final var task = (tenantId == null + ? processEngine + .getTaskService() + .createTaskQuery() + : processEngine + .getTaskService() + .createTaskQuery() + .tenantIdIn(tenantId)) + .processInstanceBusinessKey(aggregateId) + .taskId(taskId) + .singleResult(); + + if (task == null) { + throw new NullValueException("Task '" + + taskId + + "' not found!"); + } + + final var bpmnProcessId = parent.getPrimaryBpmnProcessId(); + LoggingContext.setLoggingContext( + Camunda7AdapterConfiguration.ADAPTER_ID, + tenantId, + parent.getWorkflowModuleId(), + aggregateId, + bpmnProcessId, + taskId, + task.getProcessInstanceId(), + task.getTaskDefinitionKey(), + task.getExecutionId()); + + wakeupJobExecutorOnActivity(); + + processEngine + .getTaskService() + .handleBpmnError(taskId, errorCode); - return attachedAggregate; + return attachedAggregate; + + } finally { + LoggingContext.clearContext(); + } } diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java index 40ebd26..f479098 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java @@ -1,5 +1,7 @@ package io.vanillabp.camunda7.wiring; +import io.vanillabp.camunda7.Camunda7AdapterConfiguration; +import io.vanillabp.camunda7.LoggingContext; import io.vanillabp.camunda7.service.Camunda7ProcessService; import io.vanillabp.spi.service.MultiInstanceElementResolver; import io.vanillabp.spi.service.TaskEvent.Event; @@ -8,6 +10,11 @@ import io.vanillabp.springboot.adapter.TaskHandlerBase; import io.vanillabp.springboot.adapter.wiring.WorkflowAggregateCache; import io.vanillabp.springboot.parameters.MethodParameter; +import java.lang.reflect.Method; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; import org.camunda.bpm.engine.delegate.BpmnError; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; @@ -15,6 +22,7 @@ import org.camunda.bpm.model.bpmn.instance.Activity; import org.camunda.bpm.model.bpmn.instance.BaseElement; import org.camunda.bpm.model.bpmn.instance.MultiInstanceLoopCharacteristics; +import org.camunda.bpm.model.bpmn.instance.Process; import org.camunda.bpm.model.xml.ModelInstance; import org.camunda.bpm.model.xml.instance.ModelElementInstance; import org.slf4j.Logger; @@ -22,18 +30,16 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; -import java.lang.reflect.Method; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; - public class Camunda7TaskHandler extends TaskHandlerBase implements JavaDelegate { private static final Logger logger = LoggerFactory.getLogger(Camunda7TaskHandler.class); private final String bpmnProcessId; + private final String tenantId; + + private final String workflowModuleId; + private Object result; private final Camunda7ProcessService processService; @@ -44,11 +50,15 @@ public Camunda7TaskHandler( final Object bean, final Method method, final List parameters, - final Camunda7ProcessService processService) { + final Camunda7ProcessService processService, + final String tenantId, + final String workflowModuleId) { super(workflowAggregateRepository, bean, method, parameters); this.bpmnProcessId = bpmnProcessId; this.processService = processService; + this.tenantId = tenantId; + this.workflowModuleId = workflowModuleId; } @@ -75,8 +85,22 @@ public void execute( try { + final var currentElement = (Activity) getCurrentElement(execution.getBpmnModelInstance(), execution); + LoggingContext.setLoggingContext( + Camunda7AdapterConfiguration.ADAPTER_ID, + tenantId, + workflowModuleId, + execution.getBusinessKey(), + bpmnProcessId, + execution.getId(), + getSuperProcessInstanceId(execution), + getBpmnProcessId(execution) + + "#" + + currentElement.getId(), + execution.getId()); + logger.trace("Will handle task '{}' of workflow '{}' ('{}') by execution '{}'", - execution.getBpmnModelElementInstance().getId(), + currentElement.getId(), execution.getProcessInstanceId(), bpmnProcessId, execution.getId()); @@ -100,7 +124,7 @@ public void execute( (args, param) -> processTaskParameter( args, param, - taskParameter -> execution.getVariableLocal(taskParameter)), + execution::getVariableLocal), (args, param) -> processTaskEventParameter( args, param, @@ -136,6 +160,8 @@ public void execute( } throw new BpmnError(e.getErrorCode(), e); + } finally { + LoggingContext.clearContext(); } } @@ -146,6 +172,45 @@ public Object getResult() { } + static String getSuperProcessInstanceId( + final DelegateExecution execution) { + + DelegateExecution cExecution = execution; + while (true) { + + final var result = cExecution.getProcessInstanceId(); + cExecution = cExecution.getParentId() != null + ? ((ExecutionEntity) cExecution).getParent() + : cExecution.getSuperExecution(); + if (cExecution == null) { + return result; + } + + } + + } + + static String getBpmnProcessId( + final DelegateExecution execution) { + + DelegateExecution cExecution = execution; + while (true) { + + final var flowElement = cExecution.getBpmnModelElementInstance(); + if (flowElement instanceof Process) { + return flowElement.getId(); + } + cExecution = cExecution.getParentId() != null + ? ((ExecutionEntity) cExecution).getParent() + : cExecution.getSuperExecution(); + if (cExecution == null) { + return null; + } + + } + + } + static Map> getMultiInstanceContext( final DelegateExecution execution) { @@ -199,7 +264,7 @@ static Map> getMultiI } - private static ModelElementInstance getCurrentElement(final ModelInstance model, DelegateExecution miExecution) { + static ModelElementInstance getCurrentElement(final ModelInstance model, DelegateExecution miExecution) { // if current element is known then simply use it if (miExecution.getBpmnModelElementInstance() != null) { diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java index bb45822..071605a 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java @@ -1,5 +1,6 @@ package io.vanillabp.camunda7.wiring; +import io.vanillabp.camunda7.Camunda7VanillaBpProperties; import io.vanillabp.camunda7.service.Camunda7ProcessService; import io.vanillabp.spi.process.ProcessService; import io.vanillabp.spi.service.WorkflowTask; @@ -7,13 +8,12 @@ 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; +import org.springframework.context.ApplicationContext; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Component; @Component public class Camunda7TaskWiring extends TaskWiringBase, MethodParameterFactory> { @@ -24,17 +24,21 @@ public class Camunda7TaskWiring extends TaskWiringBase> connectableServices) { + final Collection> connectableServices, + final Camunda7VanillaBpProperties camunda7Properties) { super(applicationContext, springBeanUtil); this.processEntityAwareExpressionManager = processEntityAwareExpressionManager; this.userTaskEventHandler = userTaskEventHandler; this.connectableServices = connectableServices; + this.camunda7Properties = camunda7Properties; } @@ -65,19 +69,23 @@ protected void connectToBpms( bean, method, parameters, - processService); + processService, + workflowModuleId); userTaskEventHandler.addTaskHandler(connectable, taskHandler); return; } - + + final var tenantId = camunda7Properties.getTenantId(workflowModuleId); final var taskHandler = new Camunda7TaskHandler( connectable.getBpmnProcessId(), (CrudRepository) repository, bean, method, parameters, - processService); + processService, + tenantId, + workflowModuleId); processEntityAwareExpressionManager.addTaskHandler(connectable, taskHandler); diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java index 3d103c5..3b113e1 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java @@ -1,5 +1,7 @@ package io.vanillabp.camunda7.wiring; +import io.vanillabp.camunda7.Camunda7AdapterConfiguration; +import io.vanillabp.camunda7.LoggingContext; import io.vanillabp.camunda7.service.Camunda7ProcessService; import io.vanillabp.spi.service.TaskEvent; import io.vanillabp.spi.service.TaskEvent.Event; @@ -8,23 +10,25 @@ import io.vanillabp.springboot.adapter.wiring.WorkflowAggregateCache; import io.vanillabp.springboot.parameters.MethodParameter; import io.vanillabp.springboot.parameters.TaskEventMethodParameter; -import org.camunda.bpm.engine.delegate.DelegateTask; -import org.camunda.bpm.engine.delegate.TaskListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.data.repository.CrudRepository; - import java.lang.reflect.Method; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; +import org.camunda.bpm.engine.delegate.DelegateTask; +import org.camunda.bpm.engine.delegate.TaskListener; +import org.camunda.bpm.model.bpmn.instance.Activity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.repository.CrudRepository; public class Camunda7UserTaskHandler extends TaskHandlerBase implements TaskListener { private static final Logger logger = LoggerFactory.getLogger(Camunda7UserTaskHandler.class); private final Camunda7ProcessService processService; + + private final String workflowModuleId; private final String bpmnProcessId; @@ -34,11 +38,13 @@ public Camunda7UserTaskHandler( final Object bean, final Method method, final List parameters, - final Camunda7ProcessService processService) { + final Camunda7ProcessService processService, + final String workflowModuleId) { super(workflowAggregateRepository, bean, method, parameters); this.bpmnProcessId = bpmnProcessId; this.processService = processService; + this.workflowModuleId = workflowModuleId; } @@ -57,14 +63,27 @@ public void notify(final DelegateTask delegateTask) { try { + final var execution = delegateTask.getExecution(); + final var currentElement = (Activity) Camunda7TaskHandler.getCurrentElement(execution.getBpmnModelInstance(), execution); + LoggingContext.setLoggingContext( + Camunda7AdapterConfiguration.ADAPTER_ID, + delegateTask.getTenantId(), + workflowModuleId, + execution.getBusinessKey(), + bpmnProcessId, + delegateTask.getId(), + Camunda7TaskHandler.getSuperProcessInstanceId(execution), + Camunda7TaskHandler.getBpmnProcessId(execution) + + "#" + + currentElement.getId(), + execution.getId()); + logger.trace("Will handle user-task '{}' of workflow '{}' ('{}') by execution '{}'", - delegateTask.getBpmnModelElementInstance().getId(), + currentElement.getId(), delegateTask.getProcessInstanceId(), bpmnProcessId, delegateTask.getExecutionId()); - final var execution = delegateTask.getExecution(); - final Function multiInstanceSupplier = multiInstanceActivity -> { if (multiInstanceCache[0] == null) { multiInstanceCache[0] = Camunda7TaskHandler.getMultiInstanceContext(execution); @@ -125,6 +144,8 @@ public void notify(final DelegateTask delegateTask) { throw new RuntimeException(e); + } finally { + LoggingContext.clearContext(); } }