Maven Coordinates

To enable Long Running Actions add the following dependency to your project’s pom.xml (see Managing Dependencies).

<dependency>
  <groupId>io.helidon.microprofile.lra</groupId>
  <artifactId>helidon-microprofile-lra</artifactId>
</dependency>
<!-- Support for Narayana coordinator -->
<dependency>
  <groupId>io.helidon.lra</groupId>
  <artifactId>helidon-lra-coordinator-narayana-client</artifactId>
</dependency>
Copied

Long Running Actions (LRA)

Distributed transactions for microservices are known as SAGA design patterns and are defined by the Micro Profile Long Running Actions specification. Unlike well known XA protocol, LRA is asynchronous and therefore much more scalable. Every LRA JAX-RS resource (participant) defines endpoints to be invoked when transaction needs to be completed or compensated.

LRA transactions need to be coordinated over REST API by the LRA coordinator. Coordinator keeps track of all transactions and calls the @Compensate or @Complete endpoints for all participants involved in the particular transaction. LRA transaction is first started, then joined by participant. Participant reports the successful finish of transaction by calling complete. Coordinator then calls the JAX-RS complete endpoint that was registered during the join of each participant. As the completed or compensated participants don’t have to be on same instance, the whole architecture is highly scalable.

Complete

In case of error during the LRA transaction, participant reports cancel of LRA to coordinator. Coordinator calls compensate on all the joined participants.

Cancel

When participant joins the LRA with timeout defined @LRA(value = LRA.Type.REQUIRES_NEW, timeLimit = 5, timeUnit = ChronoUnit.MINUTES), coordinator compensate if timeout occurs before close is reported by participants.

Timeout

Example

The following example shows how a simple LRA participant starts and joins a transaction after calling the '/start-example' resource. When startExample method finishes successfully, close is reported to coordinator and /complete-example endpoint is called by coordinator to confirm successful closure of the LRA.

If an exception occurs during startExample method execution, coordinator receives cancel call and /compensate-example is called by coordinator to compensate for cancelled LRA transaction.

Example of simple LRA participant
@PUT
@LRA(LRA.Type.REQUIRES_NEW) 
@Path("start-example")
public Response startExample(@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI lraId, 
                             String data) {
    if (data.contains("BOOM")) {
        throw new RuntimeException("BOOM 💥"); 
    }

    LOGGER.info("Data " + data + " processed 🏭");
    return Response.ok().build(); 
}

@PUT
@Complete 
@Path("complete-example")
public Response completeExample(@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI lraId) {
    LOGGER.log(Level.INFO, "LRA id: {0} completed 🎉", lraId);
    return LRAResponse.completed();
}

@PUT
@Compensate 
@Path("compensate-example")
public Response compensateExample(@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI lraId) {
    LOGGER.log(Level.SEVERE, "LRA id: {0} compensated 🦺", lraId);
    return LRAResponse.compensated();
}
Copied
  • This JAX-RS PUT method will start new LRA transactions and join it before method body gets executed
  • LRA id assigned by coordinator to this LRA transaction
  • When method execution finishes exceptionally, cancel signal for this particular LRA is sent to coordinator
  • When method execution finishes successfully, complete signal for this particular LRA is sent to coordinator
  • Method which will be called by coordinator when LRA is completed
  • Method which will be called by coordinator when LRA is canceled

Configuration

Example of lra configuration
mp.lra:
  coordinator.url: http://localhost:8070/lra-coordinator 
  propagation.active: true 
  participant.url: http://coordinator.visibe.host:80/awsomeapp 
Copied
  • Url of coordinator
  • Propagate LRA headers LRA_HTTP_CONTEXT_HEADER and LRA_HTTP_PARENT_CONTEXT_HEADER through non-LRA endpoints
  • Url of the LRA enabled service overrides standard base uri, so coordinator can call load-balancer instead of the service

For more information continue to Micro Profile Long Running Actions specification.