Last active
March 23, 2024 22:46
-
-
Save paulparkinson/34b430e9f8085fad2445fd16258de6f8 to your computer and use it in GitHub Desktop.
TravelAgencyController implementation of Oracle Database SagaInitiator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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