Acquire Outer Scope ActivityId at Runtime

I have a multi-instance subprocess with activity id “MeasurementIntervalSubprocess”. Running in the subprocess is a service task with a java delegate. I need to set a subprocess instance variable which I am able to do with:
execution.setVariable("requestId", id, "MeasurementIntervalSubprocess");
What I would like to do is determine the subprocess activity id at runtime, rather than hard-code it. Is there a way to do this from my java delegate?

Can anyone answer this?

Hi Victoria, My I ask as to why you need this id? Perhaps you need to correlate something and an alternate mechanism exists…

regards

Rob

We are converting an existing system from a Fuse/Switchyard BPMN solution over to Camunda. While I have found Camunda pretty straightforward and very well documented, I have had a lot of problems with variable scope. I understand it from both the documentation and my own experimentation, but it is an ongoing issue.

Our system follows a pretty consistent pattern. We have service tasks that asynchronously invoke long-running remote services. We call a remote service, get back a unique identifier (called requestId), and then wait for a completion message, correlating on that requestId. We use dynamically named message, e.g. MsgFilePrep-${requestId}. Our callback is implemented as a custom REST controller, which correlates the message with:

runtimeService.createMessageCorrelation(messageName + "-" + requestId).correlate();

We do it this way to avoid making any changes to our back-end system.

The problem is that we are executing in the context of many different scopes. We associate a Java delegate with each service task, which is where the requestId variable is set after receiving it from the remote service. It is imperative that this variable is set in the correct scope so that it’s not overwriting requestId in some other scope.

I tried using correlation variables, but ran into the issue of having to be global vs. local-aware. I tried always using setVariableLocal, but if I do that and the scope happens to be global I cannot retrieve the variable (is this a bug? Call setVariableLocal in global scope and then neither getVariable nor getVariableLocal work). Additionally, a service task’s local variable scope is gone before arriving at the message receive where I need its value, so I would have to add input/output variables to every service task and message receive event. This is cumbersome. I looked into adding that capability to a BPMN parse plugin, but it looks like there needs to be a level of scope-awareness there too.

Our system is hierarchical (a request can have a parent, and the parent can have a parent, etc.), which makes management of scope a necessity. What I would really like to do is simply obtain the outer scope at runtime, but I don’t see a way to do that without having specific knowledge as to the name of that scope. I would rather not hard-code this. If there is a way to do this with a plugin or execution listener that would be great. I am open to any suggestions.

Thank you.