Compensation is triggered twice if an activity is executed twice

In the model, I have Send Task followed by Receive Task used for communicating with another microservice. Based on the output from the Receive Task, either re-trigger the Send Task once again or proceed further with the flow.
I have also defined a compensation for the Send Task. If the Send Task is executed twice, then on triggering the compensation flow, the compensation for the Send Task also gets triggered twice.
However I want the compensation for the Send Task to be executed only once irrespective of the number of times the Send Task gets executed, Could you please advise on how this can be achieved.


Hi @Sneha_Patel
Welcome to the forum

Can you upload the model you’re describing, its easier to understand with a visual aid.

Hi @Niall

Please find the attached models. I could not upload the actual models due to security reasons. So I have created sample model to explain the scenario. Here, consider the scenario wherein the Send Task is executed twice. On triggering the Cancel message, the compensation is triggered and the compensation sub process is executed twice. Hope this explains the problem.[compensation-test.bpmn (12.0 KB)

You could set a variable to a special value when the compensation is executed and check its value before performing the compensating logic. If the variable is already set, the compensation becomes a no-op. So, techically, the compensating activity would be executed twice, but effectively it would only be executed once.

Thanks @fml2 . The solution you mentioned would work but we wanted to avoid this extra logic. Is there any other way of achieving this. We do not want to put any such logic that is visible in the bpmn model. If I have understood correctly, we would need to put an exclusive gateway to check the special variable value. And based on the special variable value, we would have to either execute it or skip it.

You don’t need a gateway, you can do the check in the Java code. Hence the process model will be simple.

Hi @fml2,
compensation-test.bpmn (12.0 KB)
Thanks for the response.I am attaching the model here. In the compensation subprocess, there is a send task and a receive task. I can skip the send task through java code. But how can we skip the receive task. Kindly suggest.

You could throw a BpmnError in the send task if the process should be a no-op, and model a boundary error event (and do nothing in that case). This would keep the model more compact. I’m not sure whether the it will be more comprehensible then.

Thanks @fml2. I was thinking if it would be a good idea to call process modification API within Send Task for no-op case. Do you see any issues if we implement this way - here there will be no change to the model.


This does not sound like a good idea to me because it will not be comprehensible. Why don’t you want to have a gateway? It’s one step more but it would make the model clear and concise.

Thanks @fml2 for your views on this.