8.5.2 Configure JAX-RS App as Transaction Participant

Before you begin, ensure that you have configured the property values for the MicroTx library.

  1. Include the MicroTx Java library file as a maven dependency in the application's pom.xml file. The following sample code is for the 24.2 release. Provide the correct version, based on the release version that you want to use.
    • In Jakarta EE8 environments, such as Helidon 2.x, use the TmmLib file.

      <dependency>
           <groupId>com.oracle.tmm.jta</groupId>
           <artifactId>TmmLib</artifactId>
           <version>24.2</version>
      </dependency>
    • In Jakarta EE9 environments, such as Helidon 3.x applications, use the TmmLib-jakarta file.

      <dependency>
           <groupId>com.oracle.tmm.jta</groupId>
           <artifactId>TmmLib-jakarta</artifactId>
           <version>24.2</version>
      </dependency>
  2. Import the following packages.
    import oracle.trm.tcc.annotation.TCC;
    import javax.ws.rs.core.Application;
  3. Inject TCC annotation in the transaction participant application code.
    To enable participant services join an existing TCC transaction, add @TCC annotation before the resource class of the transaction participant service.

    Insert the following code in the transaction participant code. In the following example, the myTransactionParticipantApp class contains code for the transaction participant service. Replace the name of the class based on your environment.

    @Path("/")
    @TCC(timeLimit = 120, timeUnit = ChronoUnit.SECONDS)
    //Add @TCC annotation so that the transaction participant service joins an existing TCC transaction
    //The transaction initiator service passes the TCC context in the request header.
    public class myTransactionParticipantApp extends Application {
        // Service code that is specific to the transaction participant service. 
    }
  4. In the transaction participant application code, inject a tccClientService object and then call the addTccParticipant(String uri) method on this object to register a participant service with the TCC transaction. The participant service exposes a URI which MicroTx uses to confirm or cancel the transaction. MicroTx calls the PUT method to confirm the transaction and the DELETE method to cancel the transaction and release the reserved resource. Ensure that these methods are present and the confirm and cancel logic is implemented. To confirm or cancel the transaction, MicroTx sends a call to the exposed URI of all the participant services.

    The following code example describes the changes that you need to make to the participant application code.

    
    public class myTransactionParticipantApp extends Application {
        // Service code that is specific to the transaction participant service.
    
        @Inject
        TccClientService tccClientService;
    
        @POST
        //The REST endpoint of the transaction participant service.
        @Path("bookings")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response create() throws TccUnknownTransactionException
        // Business logic to create a booking.
          String bookingUri;
          // Register participant service with the TCC transaction
          tccClientService.addTccParticipant(bookingUri.toString());
        }
    
        @PUT
        @Path("bookings/{bookingId}")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response confirm() throws TccUnknownTransactionException {
        //Application-specific code to confirm the booking.
        }
     
        @DELETE
        @Path("bookings/{bookingId}")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response cancel() throws TccUnknownTransactionException {
        //Application-specific code to cancel the booking.
        }          
    }

    Where,

    • myTransactionParticipantApp is a class that contains code for the transaction participant service. This class already contains user-defined methods that the participant service uses to confirm or cancel a transaction.
    • bookings is the REST endpoint of the transaction participant service. The transaction initiator service calls this endpoint to perform a task, such as creating a hotel booking.
    • bookingUri contains the resource URI that the participant service exposes and which MicroTx uses to confirm or cancel the transaction.
    • bookingId is the unique ID of the booking that you want to confirm or cancel.
  5. Save the changes.
Ensure that you make these changes in the code of all transaction participant services.