Football-player-offer-microservice-lra

This microservice implements the business domain of the offer to buy football players.

The Maven pom.xml file is similar to the one that was described previously.

We will use MySQL, not PostgreSQL to enforce the concept of a dedicated database schema for each microservice.

The following is the code for the attributes of the entity FootballPlayerOffer.java, which is used to display the information, managed by this microservice:

   @Entity
@Table(name = "FOOTBALL_PLAYER_OFFER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "FootballPlayerOffer.findAll", query
= "SELECT f FROM FootballPlayerOffer f")
})
public class FootballPlayerOffer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Long id;

@Basic(optional = false)
@NotNull
@Column(name = "ID_FOOTBALL_PLAYER")
private Long idFootballPlayer;

@Basic(optional = false)
@NotNull
@Column(name = "ID_FOOTBALL_MANAGER")
private Long idFootballManager;

@Column(name = "price")
private BigInteger price;

@Basic(optional = true)
@Size(min = 1, max = 50)
@Column(name = "status")
private String status;

@Basic(optional = true)
@Size(min = 1, max = 100)
@Column(name = "lraId")
private String lraId;

public FootballPlayerOffer() {
}

public FootballPlayerOffer(Long id, Long idFootballPlayer,
Long idFootballManager, BigInteger price, String status, String lraId) {
this.id = id;
this.idFootballPlayer = idFootballPlayer;
this.idFootballManager = idFootballManager;
this.price = price;
this.status = status;
this.lraId = lraId;
}

...
}

Like we did for the football-player-microservice-lra microservice, we will only associate the @LRA annotation on the edit/PUT method to handle the saga implementation with the LRA model. The following code is required:

@ApplicationScoped
@Path("/footballplayeroffer")
public class FootballPlayerOfferFacadeREST extends AbstractFacade<FootballPlayerOffer> {
...

@PUT
@Path("{id}")
@Consumes({MediaType.APPLICATION_JSON})
@Transactional(Transactional.TxType.REQUIRES_NEW)
@LRA(value = LRA.Type.SUPPORTS)
public void edit(@PathParam("id") Long id, FootballPlayerOffer entity) {
super.edit(entity);
}

...

@GET
@Path("lraId/{id}")
@Produces({MediaType.APPLICATION_JSON})
public FootballPlayerOffer findByLraId(@PathParam("id") String lraId) {
TypedQuery<FootballPlayerOffer> query = getEntityManager().createQuery(
"SELECT f FROM FootballPlayerOffer f WHERE f.lraId = :lraId",
FootballPlayerOffer.class);
return query.setParameter("lraId", lraId).getSingleResult();
}

...
}

Now, you are ready to run the microservice with the following command; the reasons behind the use of the SUPPORTS value in the @LRA annotation and the introduction of the findByLraId method are the same as those seen when using football-player-microservice-lra:

$ java -jar   target/football-player-offer-microservice-lra-thorntail.jar -Dlra.http.host=localhost   -Dlra.http.port=8580 

Also, in this case, you must remember to set the lra.http.port and lra.http.host parameters so that you can connect your service with the LRA coordinator.

You can find the full code implementation in the GitHub repository, at https://github.com/Hands-on-MSA-JakartaEE/ch5.git.

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

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