Skip to content

Instantly share code, notes, and snippets.

@paulparkinson
Last active March 23, 2024 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulparkinson/34b430e9f8085fad2445fd16258de6f8 to your computer and use it in GitHub Desktop.
Save paulparkinson/34b430e9f8085fad2445fd16258de6f8 to your computer and use it in GitHub Desktop.
TravelAgencyController implementation of Oracle Database SagaInitiator
@Participant(name = "TravelAgency")
/* @Participant declares the participant’s name to the saga framework */
public class TravelAgencyController extends SagaInitiator {
/* TravelAgencyController extends the SagaInitiator class */
@LRA(end = false)
/* @LRA annotates the method that begins a saga and invites participants */
@POST("booking")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public jakarta.ws.rs.core.Response booking(
@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI lraId,
String bookingPayload) {
Saga saga = this.getSaga(lraId.toString());
/* The application can access the sagaId via the HTTP header
and instantiate the Saga object using it */
try {
/* The TravelAgency sends a request to the Airline sending
a JSON payload using the Saga.sendRequest() method */
saga.sendRequest ("Airline", bookingPayload);
response = Response.status(Response.Status.ACCEPTED).build();
} catch (SagaException e) {
response=Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Response(sender = "Airline.*")
/* @Response annotates the method to receive responses from a specific
Saga participant */
public void responseFromAirline(SagaMessageContext info) {
if (info.getPayload().equals("success")) {
saga.commitSaga ();
/* The TravelAgency commits the saga if a successful response is received */
} else {
/* Otherwise, the TravelAgency performs a Saga rollback */
saga.rollbackSaga ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment