How to add cross-cutting concerns?

We have some issues adding cross-cutting concerns to our process applications, for example collecting metrics before and after each Service Task or adding some logging identifiers to the MDC context of the executing thread.

We tried to write a ProcessEnginePlugin which adds a CustomBPMNParseListener that adds ExecutionListeners. This works for simple cases an execution order is:

startMetricsListener
delegate code
endMetricsListener

Unfortunately if there are ExecutionListeners modeled in the BPMN diagram, the execution order is not guaranteed, so we end up with:

customListener and startMetricsListener in any order
delegate code
endMetricsListener

Next try was to use the Spring Event Bridge of Camunda, which basically has the same issues.

Any ideas how to guarantee something like this?

cross-cutting-listeners
user-listerners
delegate code
user-listerners
cross-cutting-listeners

Have you looked at overriding the execute() method in your ProcessApplication? That can be used to intercept any asynchronous actions from Camunda. We use it to add information to the MDC context as well.

Currently we use the Spring Boot Auto Deployment for our Processes, so we do not have any ProcessApplication where we could override execute(). Seems like no ProcessApplication is created at all?

I’m not intimately familiar with the exact options for Spring Boot, but I assume the engine is creating a ProcessApplication under the hood. But you can probably easily define one anyway by just specifying a bean with @EnableProcessApplication, as described here: https://docs.camunda.org/manual/7.11/user-guide/spring-boot-integration/process-applications/

Yes, adding @EnableProcessApplication works, I’m able to intercept all asynchronous invocations now.

Another option which might be worth looking at is adding a custom (pre/post) command interceptor in the process engine configuration.

Hi @criew, Am trying to implement the same functionality in my application which is also a spring boot project. But for some reason no process tasks would intercept when I publish some sample data. Can you post your code snippet, how you made it work?

Hi @jgigliotti Justin, Would you have any insight on this?

@abhilashk6303 I’d suggest starting a new thread with an example of what you’re trying to do and what’s not working. It’s really challenging to provide assistance without data specific to your issue.