Use BPMN Model API for typical compensation use case


I want to define the “classical” compensation example using the Model API

Now I struggle how to define this in the Model Api, I would start like this - but how can I define the CancelCar activity which is not embedded in the normal flow?

  .addModelInstance("travel.bpmn", Bpmn.createProcess("travel").executable() //
    .serviceTask().name("Reserve car") //.camundaClass(ReserveCar.class)

I also thinking about creating an issue or PR to improve the API in a way to allow something like this - as I think this is far more intuitive for the use case at hand. Or what do you think?

  .addModelInstance("travel.bpmn", Bpmn.createProcess("travel").executable() //
    .serviceTask().name("Reserve car") //.camundaClass(ReserveCar.class)
      .boundaryEvent().compensateEventDefinition().serviceTask().name("Cancel car")



I think you can’t do that with the fluent builder at the moment. This is how we do it in the engine test suite:

public static void addUserTaskCompensationHandler(BpmnModelInstance modelInstance, String boundaryEventId, String compensationHandlerId) {

    BoundaryEvent boundaryEvent = modelInstance.getModelElementById(boundaryEventId);
    BaseElement scope = (BaseElement) boundaryEvent.getParentElement();

    UserTask compensationHandler = modelInstance.newInstance(UserTask.class);

    Association association = modelInstance.newInstance(Association.class);

I agree that having this as part of the fluent builder would be nice. Feel free to make a proposal and raise a pull request. I would probably go with something like:

Bpmn.createProcess("travel").executable() //
    .serviceTask().name("Reserve car") //.camundaClass(ReserveCar.class)
          .serviceTask().name("Cancel car")

edit: corrected the code snippet


Hi Thorben.
Thanks a lot!

What do you think about a solution using a static helper like mock or assertion frameworks are typically do it?

Bpmn.createProcess("travel").executable() //
    .serviceTask().name("Reserve car").camundaClass(ReserveCar.class)
               Bpmn.serviceTask().name("Cancel car"))

But it might be a new concept not used anywhere else, but I think it is more intuitive than compensationHandlerDone (I think also at other places, but thats too late anyway ;-)).


Hm, good question, not sure what the best way is. I personally lean towards consistency in such questions, but that’s debatable. If we want to prevent users from creating invalid models (e.g. compensation handlers with outgoing sequence flows), the compensationHandler() approach might be favourable, as it could return a builder that is limited to creating a single task.

On a side note, I found the idea proposed by @David_Karr quite interesting:

Java 8 is out of the picture at the moment, though.


Note that it’s possible to build APIs that can be enhanced with lambdas without requiring Java 8. Parameters that represent instances of SAM classes can use anonymous inner classes in Java 7, but true lambdas in Java 8.


Sorry, what’s a SAM class? :slight_smile:


I agree on the argument that expressing a hierarchical structure in a linear way is kind of hard to understand. But I definitely doubt that anonymous inner classes are more readable. Where is the problem with using static helper methods where the result can be passed to other methods?

The compensationHandler( someCompensationTask ) approach indeed makes it impossible to attach more than one activity as compensation action - so you cannot use that in a wrong way. It is also very readable. You could also do sub processes in a ciomparable way:

Bpmn.createProcess("travel").executable() //

I personally like that much more, as it is very intuitive - and with static imports it gets also very readable. If we decide that this direction make sense, we could introduce this step by step for different use cases, very much as we did for fluent service API’s.




I quickly tried to squeeze an quick win into the current code base but surrendered. I think we should even consider adding a new fluent builder focusing on the real-life use cases making them as easy as possible for NON CAMUNDA or BPMN experts. I added it to the roadmap discussions.

For the time being I added an own SagaBuilder now hiding the glitches of the Model API:

Now at least my visible flow definition looks like I want it to be:

SagaBuilder saga = SagaBuilder.newSaga("trip") //
        .activity("Reserve car", ReserveCarAdapter.class) //
        .compensationActivity("Cancel car", CancelCarAdapter.class) //
        .activity("Book hotel", BookHotelAdapter.class) //
        .compensationActivity("Cancel hotel", CancelHotelAdapter.class) //
        .activity("Book flight", BookFlightAdapter.class) //
        .compensationActivity("Cancel flight", CancelFlightAdapter.class) //
        .triggerCompensationOnAnyError() //

  camunda.getRepositoryService().createDeployment() //
        .addModelInstance("trip.bpmn", saga.getModel()) //


Thanks for sharing. What about a Java-8-based community extension for a “proper” fluent API?


Having 7.8.0-alpha1 ( the situation got great by the way :slight_smile:
I kept my small DSL:
But the implemenation is just translating that into model API - very lean:

Love it…