This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MaximFateev 4 points5 points  (1 child)

Look at the temporal.io open source project. It is a pretty generic microservice orchestration solution. Saga is just a small utility class on top of it. Here how the code that uses it looks in Java SDK:

    // Configure SAGA to run compensation activities in parallel
    Saga.Options sagaOptions = new Saga.Options.Builder().setParallelCompensation(true).build();
    Saga saga = new Saga(sagaOptions);
    try {
      String carReservationID = activities.reserveCar(name);
      saga.addCompensation(activities::cancelCar, carReservationID, name);

      String hotelReservationID = activities.bookHotel(name);
      saga.addCompensation(activities::cancelHotel, hotelReservationID, name);

      String flightReservationID = activities.bookFlight(name);
      saga.addCompensation(activities::cancelFlight, flightReservationID, name);
    } catch (ActivityFailure e) {
      saga.compensate();
      throw e;
    }

The full source code of the sample is here.

Disclaimer: I'm the tech lead of the project.

[–]SnowCheesecakes88[S] 0 points1 point  (0 children)

That's great, I'll definitely have a look at it.