The complete phase

In the complete phase, the LRA coordinator invokes the method annotated with @Complete and performs the business operations needed to make the business operation consistent, in terms of data status.

In our scenario, the method will set the status of the offer to "ACCEPTED" and the status of the football player to "Purchased".

The following code is required to do so:

/**
* LRA complete method: it sets the final status of the football player offer
* and football player based on a successful response of the send offer
* method.
*
* @param lraId The Long Running Action identifier needed to retrieve
* the record on which perform the operation.
*/
@PUT
@Path("/complete")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Complete
public void confirmOffer(@HeaderParam(LRAClient.LRA_HTTP_HEADER) String lraId) {
LOG.log(Level.INFO, "Start method confirmOffer: I'm in LRA complete phase");
LOG.log(Level.FINE, "Value of header lraId {0}", lraId);

String lraIdParameter = lraId.substring(lraId.lastIndexOf('/') + 1);

LOG.log(Level.FINE, "Value of lraIdParameter {0}", lraIdParameter);

// Set the offer to accepted
LOG.log(Level.FINE, "Setting the offer as ACCEPTED ...");
FootballPlayerOffer fpo = footballPlayerOfferTarget.path("lraId/" +
lraIdParameter).request().get(new GenericTypeFootballPlayerOfferImpl());

fpo.setStatus("ACCEPTED");

footballPlayerOfferTarget.path(fpo.getId().toString()).request().put(
Entity.entity(fpo, MediaType.APPLICATION_JSON_TYPE));

LOG.log(Level.FINE, "Set the offer as ACCEPTED ...");

// Set the football player status to purchased
FootballPlayer player = footballPlayerTarget.path("lraId/" + lraIdParameter).request().get(new GenericTypeFootballPlayerImpl());

LOG.log(Level.FINE, "Got football player {0}", player);

player.setStatus("Purchased");
player.setLraId(null);

LOG.log(Level.FINE, "Changing football player status ...");

footballPlayerTarget.path(player.getId().toString()).request().put(
Entity.entity(player, MediaType.APPLICATION_JSON_TYPE));

LOG.log(Level.INFO, "End method confirmOffer: LRA complete phase terminated"); }

The main element of the method is the @Complete annotation; in this way, the method is considered a participant of the LRA, and the LRA coordinator will automatically invoke it in the case of the success of the operation that starts the LRA context.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset