Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
829 changes: 804 additions & 25 deletions code/chapter11/README.adoc

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions code/chapter11/catalog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<properties>

<!-- Setting the source and target of the Java Compiler -->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand All @@ -30,7 +30,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<version>1.18.36</version>
<scope>provided</scope>
</dependency>

Expand All @@ -46,7 +46,7 @@
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>6.1</version>
<version>7.1</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
Expand All @@ -55,6 +55,16 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Maven Compiler Plugin for Java 21 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>21</release>
</configuration>
</plugin>

<!-- Enable liberty-maven plugin -->
<plugin>
<groupId>io.openliberty.tools</groupId>
Expand Down
23 changes: 0 additions & 23 deletions code/chapter11/catalog/src/main/liberty/config/server.xml

This file was deleted.

2 changes: 0 additions & 2 deletions code/chapter11/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
user-service:
build: ./user
Expand Down
8 changes: 4 additions & 4 deletions code/chapter11/inventory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.release>21</maven.compiler.release>
<jakarta.jakartaee-api.version>10.0.0</jakarta.jakartaee-api.version>
<microprofile.version>6.1</microprofile.version>
<liberty.version>23.0.0.3</liberty.version>
<lombok.version>1.18.24</lombok.version>
<microprofile.version>7.1</microprofile.version>
<liberty.version>25.0.0.1</liberty.version>
<lombok.version>1.18.36</lombok.version>

<!-- Liberty configuration -->
<liberty.var.default.http.port>6050</liberty.var.default.http.port>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.microprofile.tutorial.store.inventory.dto.Product;
import io.microprofile.tutorial.store.inventory.dto.InventoryWithProductInfo;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -52,16 +51,15 @@ public boolean isProductAvailable(Long productId) {
LOGGER.fine("Checking product availability for ID: " + productId);

try {
// Demonstrate RestClientBuilder usage - build a REST client programmatically
URI catalogServiceUri = URI.create("http://localhost:5050/catalog/api");

// Demonstrate RestClientBuilder usage with MP Rest Client 4.0
// Using new baseUri(String) method - no need for URI.create()
ProductServiceClient dynamicClient = RestClientBuilder.newBuilder()
.baseUri(catalogServiceUri)
.baseUri("http://localhost:5050/catalog/api")
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build(ProductServiceClient.class);

LOGGER.fine("Built dynamic REST client for catalog service at: " + catalogServiceUri);
LOGGER.fine("Built dynamic REST client for catalog service using baseUri(String)");

Product product = dynamicClient.getProductById(productId);
boolean available = product != null;
Expand Down Expand Up @@ -460,6 +458,7 @@ public Inventory reserveInventory(Long productId, int quantityToReserve) {
/**
* Demonstrates advanced RestClientBuilder usage with custom configuration.
* This method builds a REST client with specific timeout and error handling settings.
* Uses MicroProfile Rest Client 4.0 baseUri(String) convenience method.
*
* @param productId The product ID to check
* @return Product details if found, null otherwise
Expand All @@ -468,11 +467,10 @@ public Product getProductWithCustomClient(Long productId) {
LOGGER.info("Getting product details using custom RestClientBuilder for ID: " + productId);

try {
// Build REST client with custom configuration
URI catalogServiceUri = URI.create("http://localhost:5050/catalog/api");

// Build REST client with custom configuration using MP Rest Client 4.0
// Using baseUri(String) - no URI.create() needed
ProductServiceClient customClient = RestClientBuilder.newBuilder()
.baseUri(catalogServiceUri)
.baseUri("http://localhost:5050/catalog/api")
.connectTimeout(3, TimeUnit.SECONDS) // Custom connect timeout
.readTimeout(8, TimeUnit.SECONDS) // Custom read timeout
.build(ProductServiceClient.class);
Expand Down
15 changes: 0 additions & 15 deletions code/chapter11/inventory/src/main/liberty/config/server.xml

This file was deleted.

8 changes: 4 additions & 4 deletions code/chapter11/order/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.release>21</maven.compiler.release>
<jakarta.jakartaee-api.version>10.0.0</jakarta.jakartaee-api.version>
<microprofile.version>6.1</microprofile.version>
<liberty.version>23.0.0.3</liberty.version>
<lombok.version>1.18.24</lombok.version>
<microprofile.version>7.1</microprofile.version>
<liberty.version>25.0.0.1</liberty.version>
<lombok.version>1.18.36</lombok.version>
</properties>

<dependencies>
Expand Down
Empty file modified code/chapter11/order/restart-server.sh
100755 → 100644
Empty file.
Empty file modified code/chapter11/order/run-docker.sh
100755 → 100644
Empty file.
Empty file modified code/chapter11/order/run.sh
100755 → 100644
Empty file.
16 changes: 0 additions & 16 deletions code/chapter11/order/src/main/liberty/config/server.xml

This file was deleted.

6 changes: 3 additions & 3 deletions code/chapter11/payment/README.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Payment Service

This microservice is part of the Jakarta EE 10 and MicroProfile 6.1-based e-commerce application. It handles payment processing and transaction management.
This microservice is part of the Jakarta EE 10 and MicroProfile 7.1-based e-commerce application. It handles payment processing and transaction management.

== Features

Expand Down Expand Up @@ -38,8 +38,8 @@ This microservice is part of the Jakarta EE 10 and MicroProfile 6.1-based e-comm

=== Prerequisites

* JDK 17 or higher
* Maven 3.6.0 or higher
* JDK 21 or higher
* Maven 3.13.0 or higher

=== Local Development

Expand Down
18 changes: 14 additions & 4 deletions code/chapter11/payment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand All @@ -33,7 +33,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<version>1.18.36</version>
<scope>provided</scope>
</dependency>

Expand All @@ -49,7 +49,7 @@
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>6.1</version>
<version>7.1</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
Expand All @@ -65,6 +65,16 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Maven Compiler Plugin for Java 21 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>21</release>
</configuration>
</plugin>

<!-- Enable liberty-maven plugin -->
<plugin>
<groupId>io.openliberty.tools</groupId>
Expand Down
Empty file modified code/chapter11/payment/run-docker.sh
100755 → 100644
Empty file.
Empty file modified code/chapter11/payment/run.sh
100755 → 100644
Empty file.
Original file line number Diff line number Diff line change
@@ -1,52 +1,67 @@
package io.microprofile.tutorial.store.payment.client;

import io.microprofile.tutorial.store.payment.dto.product.Product;
import jakarta.json.JsonArray;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

public class ProductClient {
public static Product[] getProductsWithJsonb(String targetUrl) {
// This method would typically make a REST call to fetch products.
// For now, we return an empty array as a placeholder.
Client client = ClientBuilder.newClient();
Response response = client.target(targetUrl)
.request(MediaType.APPLICATION_JSON)
.get();

Product[] products = response.readEntity(Product[].class);
response.close();
client.close();
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;

import io.microprofile.tutorial.store.payment.dto.product.Product;
import io.microprofile.tutorial.store.payment.exception.ProductNotFoundException;

import java.util.List;

return products;
}
/**
* MicroProfile Rest Client interface for the Catalog/Product Service.
*
* This interface demonstrates:
* - @RegisterRestClient annotation to register as a REST client
* - @RegisterProvider to register custom exception mapper
* - configKey for external configuration via MicroProfile Config
* - Type-safe method definitions with Jakarta REST annotations
* - Automatic implementation generation by MicroProfile runtime
* - Custom error handling via ResponseExceptionMapper
*
* Configuration properties (in microprofile-config.properties):
* - catalog-service/mp-rest/url=http://localhost:5050/catalog/api
* - catalog-service/mp-rest/scope=jakarta.enterprise.context.ApplicationScoped
*
* This interface extends AutoCloseable to support try-with-resources pattern
* when using RestClientBuilder for programmatic client creation.
*/
@RegisterRestClient(configKey = "catalog-service")
@RegisterProvider(ProductServiceResponseExceptionMapper.class)
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
public interface ProductClient extends AutoCloseable {

public static Product[] getProductsWithJsonp(String targetUrl) {
// Default URL for product service
String defaultUrl = "http://localhost:6050/products";
Client client = ClientBuilder.newClient();
Response response = client.target(targetUrl != null ? targetUrl : defaultUrl)
.request(MediaType.APPLICATION_JSON)
.get();
/**
* Retrieves all products from the catalog service.
*
* @return List of all products
* @throws RuntimeException if service returns 5xx error
*/
@GET
List<Product> getAllProducts();

JsonArray jsonArray = response.readEntity(JsonArray.class);
response.close();
client.close();

return collectProducts(jsonArray);
}

private static Product[] collectProducts(JsonArray jsonArray) {
Product[] products = new Product[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
Product product = new Product();
product.setId(jsonArray.getJsonObject(i).getJsonNumber("id").longValue());
product.setName(jsonArray.getJsonObject(i).getString("name"));
product.setPrice(jsonArray.getJsonObject(i).getJsonNumber("price").doubleValue());
products[i] = product;
}
return products;
}
}
/**
* Retrieves a specific product by its ID.
*
* Example usage: productClient.getProductById(1L)
* Resulting HTTP request: GET /products/1
*
* Demonstrates checked exception handling:
* - Throws ProductNotFoundException if product not found (404)
* - Method must declare this checked exception in throws clause
*
* @param id The product ID
* @return The product with the specified ID
* @throws ProductNotFoundException if product is not found (404)
*/
@GET
@Path("/{id}")
Product getProductById(@PathParam("id") Long id) throws ProductNotFoundException;
}
Loading
Loading