diff --git a/quarkus-modules/pom.xml b/quarkus-modules/pom.xml index d78698437001..992bd8d21c69 100644 --- a/quarkus-modules/pom.xml +++ b/quarkus-modules/pom.xml @@ -31,6 +31,7 @@ quarkus-management-interface quarkus-mcp-langchain quarkus-panache + quarkus-resources quarkus-testcontainers quarkus-virtual-threads diff --git a/quarkus-modules/quarkus-resources/pom.xml b/quarkus-modules/quarkus-resources/pom.xml new file mode 100644 index 000000000000..dcbbe1202fae --- /dev/null +++ b/quarkus-modules/quarkus-resources/pom.xml @@ -0,0 +1,125 @@ + + + 4.0.0 + com.baeldung.quarkus + quarkus-resources + 1.0.0-SNAPSHOT + + com.baeldung + quarkus-modules + 1.0.0-SNAPSHOT + + + + + io.quarkus.platform + quarkus-bom + ${quarkus.platform.version} + pom + import + + + + + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-rest + + + io.quarkus + quarkus-rest-jackson + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + + + + + io.quarkus.platform + quarkus-maven-plugin + ${quarkus.platform.version} + true + + + + build + generate-code + generate-code-tests + native-image-agent + + + + + + maven-compiler-plugin + + true + + + + maven-surefire-plugin + + --add-opens java.base/java.lang=ALL-UNNAMED + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + --add-opens java.base/java.lang=ALL-UNNAMED + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + + + native + + + native + + + + false + false + true + + + + + 3.15.6 + true + 5.10.2 + + diff --git a/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.jvm b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.jvm new file mode 100644 index 000000000000..53f8aaa14e45 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.jvm @@ -0,0 +1,98 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# ./mvnw package +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/quarkus-resources-jvm . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/quarkus-resources-jvm +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005 being the default) like this : EXPOSE 8080 5005. +# Additionally you will have to set -e JAVA_DEBUG=true and -e JAVA_DEBUG_PORT=*:5005 +# when running the container +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 quarkus/quarkus-resources-jvm +# +# This image uses the `run-java.sh` script to run the application. +# This scripts computes the command line to execute your Java application, and +# includes memory/GC tuning. +# You can configure the behavior using the following environment properties: +# - JAVA_OPTS: JVM options passed to the `java` command (example: "-verbose:class") - Be aware that this will override +# the default JVM options, use `JAVA_OPTS_APPEND` to append options +# - JAVA_OPTS_APPEND: User specified Java options to be appended to generated options +# in JAVA_OPTS (example: "-Dsome.property=foo") +# - JAVA_MAX_MEM_RATIO: Is used when no `-Xmx` option is given in JAVA_OPTS. This is +# used to calculate a default maximal heap memory based on a containers restriction. +# If used in a container without any memory constraints for the container then this +# option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio +# of the container available memory as set here. The default is `50` which means 50% +# of the available memory is used as an upper boundary. You can skip this mechanism by +# setting this value to `0` in which case no `-Xmx` option is added. +# - JAVA_INITIAL_MEM_RATIO: Is used when no `-Xms` option is given in JAVA_OPTS. This +# is used to calculate a default initial heap memory based on the maximum heap memory. +# If used in a container without any memory constraints for the container then this +# option has no effect. If there is a memory constraint then `-Xms` is set to a ratio +# of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx` +# is used as the initial heap size. You can skip this mechanism by setting this value +# to `0` in which case no `-Xms` option is added (example: "25") +# - JAVA_MAX_INITIAL_MEM: Is used when no `-Xms` option is given in JAVA_OPTS. +# This is used to calculate the maximum value of the initial heap memory. If used in +# a container without any memory constraints for the container then this option has +# no effect. If there is a memory constraint then `-Xms` is limited to the value set +# here. The default is 4096MB which means the calculated value of `-Xms` never will +# be greater than 4096MB. The value of this variable is expressed in MB (example: "4096") +# - JAVA_DIAGNOSTICS: Set this to get some diagnostics information to standard output +# when things are happening. This option, if set to true, will set +# `-XX:+UnlockDiagnosticVMOptions`. Disabled by default (example: "true"). +# - JAVA_DEBUG: If set remote debugging will be switched on. Disabled by default (example: +# true"). +# - JAVA_DEBUG_PORT: Port used for remote debugging. Defaults to 5005 (example: "8787"). +# - CONTAINER_CORE_LIMIT: A calculated core limit as described in +# https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt. (example: "2") +# - CONTAINER_MAX_MEMORY: Memory limit given to the container (example: "1024"). +# - GC_MIN_HEAP_FREE_RATIO: Minimum percentage of heap free after GC to avoid expansion. +# (example: "20") +# - GC_MAX_HEAP_FREE_RATIO: Maximum percentage of heap free after GC to avoid shrinking. +# (example: "40") +# - GC_TIME_RATIO: Specifies the ratio of the time spent outside the garbage collection. +# (example: "4") +# - GC_ADAPTIVE_SIZE_POLICY_WEIGHT: The weighting given to the current GC time versus +# previous GC times. (example: "90") +# - GC_METASPACE_SIZE: The initial metaspace size. (example: "20") +# - GC_MAX_METASPACE_SIZE: The maximum metaspace size. (example: "100") +# - GC_CONTAINER_OPTIONS: Specify Java GC to use. The value of this variable should +# contain the necessary JRE command-line options to specify the required GC, which +# will override the default of `-XX:+UseParallelGC` (example: -XX:+UseG1GC). +# - HTTPS_PROXY: The location of the https proxy. (example: "myuser@127.0.0.1:8080") +# - HTTP_PROXY: The location of the http proxy. (example: "myuser@127.0.0.1:8080") +# - NO_PROXY: A comma separated lists of hosts, IP addresses or domains that can be +# accessed directly. (example: "foo.example.com,bar.example.com") +# +### +FROM registry.access.redhat.com/ubi9/openjdk-21:1.23 + +ENV LANGUAGE='en_US:en' + + +# We make four distinct layers so if there are application changes the library layers can be re-used +COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/ +COPY --chown=185 target/quarkus-app/*.jar /deployments/ +COPY --chown=185 target/quarkus-app/app/ /deployments/app/ +COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/ + +EXPOSE 8080 +USER 185 +ENV JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +ENV JAVA_APP_JAR="/deployments/quarkus-run.jar" + +ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ] + diff --git a/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.legacy-jar b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.legacy-jar new file mode 100644 index 000000000000..0aa01b327127 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.legacy-jar @@ -0,0 +1,94 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# ./mvnw package -Dquarkus.package.jar.type=legacy-jar +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.legacy-jar -t quarkus/quarkus-resources-legacy-jar . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/quarkus-resources-legacy-jar +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005 being the default) like this : EXPOSE 8080 5005. +# Additionally you will have to set -e JAVA_DEBUG=true and -e JAVA_DEBUG_PORT=*:5005 +# when running the container +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 quarkus/quarkus-resources-legacy-jar +# +# This image uses the `run-java.sh` script to run the application. +# This scripts computes the command line to execute your Java application, and +# includes memory/GC tuning. +# You can configure the behavior using the following environment properties: +# - JAVA_OPTS: JVM options passed to the `java` command (example: "-verbose:class") - Be aware that this will override +# the default JVM options, use `JAVA_OPTS_APPEND` to append options +# - JAVA_OPTS_APPEND: User specified Java options to be appended to generated options +# in JAVA_OPTS (example: "-Dsome.property=foo") +# - JAVA_MAX_MEM_RATIO: Is used when no `-Xmx` option is given in JAVA_OPTS. This is +# used to calculate a default maximal heap memory based on a containers restriction. +# If used in a container without any memory constraints for the container then this +# option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio +# of the container available memory as set here. The default is `50` which means 50% +# of the available memory is used as an upper boundary. You can skip this mechanism by +# setting this value to `0` in which case no `-Xmx` option is added. +# - JAVA_INITIAL_MEM_RATIO: Is used when no `-Xms` option is given in JAVA_OPTS. This +# is used to calculate a default initial heap memory based on the maximum heap memory. +# If used in a container without any memory constraints for the container then this +# option has no effect. If there is a memory constraint then `-Xms` is set to a ratio +# of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx` +# is used as the initial heap size. You can skip this mechanism by setting this value +# to `0` in which case no `-Xms` option is added (example: "25") +# - JAVA_MAX_INITIAL_MEM: Is used when no `-Xms` option is given in JAVA_OPTS. +# This is used to calculate the maximum value of the initial heap memory. If used in +# a container without any memory constraints for the container then this option has +# no effect. If there is a memory constraint then `-Xms` is limited to the value set +# here. The default is 4096MB which means the calculated value of `-Xms` never will +# be greater than 4096MB. The value of this variable is expressed in MB (example: "4096") +# - JAVA_DIAGNOSTICS: Set this to get some diagnostics information to standard output +# when things are happening. This option, if set to true, will set +# `-XX:+UnlockDiagnosticVMOptions`. Disabled by default (example: "true"). +# - JAVA_DEBUG: If set remote debugging will be switched on. Disabled by default (example: +# true"). +# - JAVA_DEBUG_PORT: Port used for remote debugging. Defaults to 5005 (example: "8787"). +# - CONTAINER_CORE_LIMIT: A calculated core limit as described in +# https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt. (example: "2") +# - CONTAINER_MAX_MEMORY: Memory limit given to the container (example: "1024"). +# - GC_MIN_HEAP_FREE_RATIO: Minimum percentage of heap free after GC to avoid expansion. +# (example: "20") +# - GC_MAX_HEAP_FREE_RATIO: Maximum percentage of heap free after GC to avoid shrinking. +# (example: "40") +# - GC_TIME_RATIO: Specifies the ratio of the time spent outside the garbage collection. +# (example: "4") +# - GC_ADAPTIVE_SIZE_POLICY_WEIGHT: The weighting given to the current GC time versus +# previous GC times. (example: "90") +# - GC_METASPACE_SIZE: The initial metaspace size. (example: "20") +# - GC_MAX_METASPACE_SIZE: The maximum metaspace size. (example: "100") +# - GC_CONTAINER_OPTIONS: Specify Java GC to use. The value of this variable should +# contain the necessary JRE command-line options to specify the required GC, which +# will override the default of `-XX:+UseParallelGC` (example: -XX:+UseG1GC). +# - HTTPS_PROXY: The location of the https proxy. (example: "myuser@127.0.0.1:8080") +# - HTTP_PROXY: The location of the http proxy. (example: "myuser@127.0.0.1:8080") +# - NO_PROXY: A comma separated lists of hosts, IP addresses or domains that can be +# accessed directly. (example: "foo.example.com,bar.example.com") +# +### +FROM registry.access.redhat.com/ubi9/openjdk-21:1.23 + +ENV LANGUAGE='en_US:en' + + +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/quarkus-run.jar + +EXPOSE 8080 +USER 185 +ENV JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +ENV JAVA_APP_JAR="/deployments/quarkus-run.jar" + +ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ] diff --git a/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.native b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.native new file mode 100644 index 000000000000..45eadcba0a75 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.native @@ -0,0 +1,29 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode. +# +# Before building the container image run: +# +# ./mvnw package -Dnative +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native -t quarkus/quarkus-resources . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/quarkus-resources +# +# The ` registry.access.redhat.com/ubi9/ubi-minimal:9.6` base image is based on UBI 9. +# To use UBI 8, switch to `quay.io/ubi8/ubi-minimal:8.10`. +### +FROM registry.access.redhat.com/ubi9/ubi-minimal:9.6 +WORKDIR /work/ +RUN chown 1001 /work \ + && chmod "g+rwX" /work \ + && chown 1001:root /work +COPY --chown=1001:root --chmod=0755 target/*-runner /work/application + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.native-micro b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.native-micro new file mode 100644 index 000000000000..bd39fb095e41 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/docker/Dockerfile.native-micro @@ -0,0 +1,32 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode. +# It uses a micro base image, tuned for Quarkus native executables. +# It reduces the size of the resulting container image. +# Check https://quarkus.io/guides/quarkus-runtime-base-image for further information about this image. +# +# Before building the container image run: +# +# ./mvnw package -Dnative +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native-micro -t quarkus/quarkus-resources . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/quarkus-resources +# +# The `quay.io/quarkus/ubi9-quarkus-micro-image:2.0` base image is based on UBI 9. +# To use UBI 8, switch to `quay.io/quarkus/quarkus-micro-image:2.0`. +### +FROM quay.io/quarkus/ubi9-quarkus-micro-image:2.0 +WORKDIR /work/ +RUN chown 1001 /work \ + && chmod "g+rwX" /work \ + && chown 1001:root /work +COPY --chown=1001:root --chmod=0755 target/*-runner /work/application + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/quarkus-modules/quarkus-resources/src/main/java/com/baeldung/quarkus/resources/ResourceAccessAPI.java b/quarkus-modules/quarkus-resources/src/main/java/com/baeldung/quarkus/resources/ResourceAccessAPI.java new file mode 100644 index 000000000000..0b6b764356f6 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/java/com/baeldung/quarkus/resources/ResourceAccessAPI.java @@ -0,0 +1,57 @@ +package com.baeldung.quarkus.resources; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; + +@Path("/resources") +public class ResourceAccessAPI { + + private static final Logger LOGGER = LoggerFactory.getLogger(ResourceAccessAPI.class); + + @GET + @Path("/default") + @Produces(MediaType.TEXT_PLAIN) + public Response getDefaultResource() throws IOException { + return Response.ok(readResource("default-resource.txt")).build(); + } + + @GET + @Path("/default-nested") + @Produces(MediaType.TEXT_PLAIN) + public Response getDefaultNestedResource() throws IOException { + return Response.ok(readResource("text/another-resource.txt")).build(); + } + + @GET + @Path("/json") + @Produces(MediaType.APPLICATION_JSON) + public Response getJsonResource() throws IOException { + return Response.ok(readResource("resources.json")).build(); + } + + + private String readResource(String resourcePath) throws IOException { + LOGGER.info("Reading resource from path: {}", resourcePath); + try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath)) { + if (in == null) { + LOGGER.error("Resource not found at path: {}", resourcePath); + throw new IOException("Resource not found: " + resourcePath); + } + LOGGER.info("Successfully read resource: {}", resourcePath); + return new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines() + .collect(Collectors.joining("\n")); + } + } +} diff --git a/quarkus-modules/quarkus-resources/src/main/resources/META-INF/native-image/com.baeldung.quarkus/quarkus-resources/resource-config.json b/quarkus-modules/quarkus-resources/src/main/resources/META-INF/native-image/com.baeldung.quarkus/quarkus-resources/resource-config.json new file mode 100644 index 000000000000..0731ea609972 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/resources/META-INF/native-image/com.baeldung.quarkus/quarkus-resources/resource-config.json @@ -0,0 +1,7 @@ +{ + "resources": [ + { + "pattern": ".*\\.json$" + } + ] +} \ No newline at end of file diff --git a/quarkus-modules/quarkus-resources/src/main/resources/META-INF/resources/index.html b/quarkus-modules/quarkus-resources/src/main/resources/META-INF/resources/index.html new file mode 100644 index 000000000000..9880aa29e63c --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/resources/META-INF/resources/index.html @@ -0,0 +1,9 @@ + + + + Quarkus Resources article from Baeldung + + +

Hello from Baeldung!

+ + \ No newline at end of file diff --git a/quarkus-modules/quarkus-resources/src/main/resources/application.properties b/quarkus-modules/quarkus-resources/src/main/resources/application.properties new file mode 100644 index 000000000000..fda716c11332 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/resources/application.properties @@ -0,0 +1 @@ +quarkus.native.resources.includes=text/**,*.txt \ No newline at end of file diff --git a/quarkus-modules/quarkus-resources/src/main/resources/default-resource.txt b/quarkus-modules/quarkus-resources/src/main/resources/default-resource.txt new file mode 100644 index 000000000000..06f60447244d --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/resources/default-resource.txt @@ -0,0 +1 @@ +This is the default resource. diff --git a/quarkus-modules/quarkus-resources/src/main/resources/resources.json b/quarkus-modules/quarkus-resources/src/main/resources/resources.json new file mode 100644 index 000000000000..1587a669681c --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/resources/resources.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} diff --git a/quarkus-modules/quarkus-resources/src/main/resources/text/another-resource.txt b/quarkus-modules/quarkus-resources/src/main/resources/text/another-resource.txt new file mode 100644 index 000000000000..e975684a4fe6 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/main/resources/text/another-resource.txt @@ -0,0 +1 @@ +This is another resource from a sub-directory. diff --git a/quarkus-modules/quarkus-resources/src/test/java/com/baeldung/quarkus/resources/ResourceAccessAPIUnitTest.java b/quarkus-modules/quarkus-resources/src/test/java/com/baeldung/quarkus/resources/ResourceAccessAPIUnitTest.java new file mode 100644 index 000000000000..de1480e15dc3 --- /dev/null +++ b/quarkus-modules/quarkus-resources/src/test/java/com/baeldung/quarkus/resources/ResourceAccessAPIUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.quarkus.resources; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +@QuarkusTest +class ResourceAccessAPIUnitTest { + @Test + @DisplayName("should return content from default resource") + void givenAPI_whenGetDefaultResource_thenReturnsContent() { + given() + .when().get("/resources/default") + .then() + .statusCode(200) + .body(is("This is the default resource.")); + } + + @Test + @DisplayName("should return content from nested default resource") + void givenAPI_whenGetDefaultNestedResource_thenReturnsContent() { + given() + .when().get("/resources/default-nested") + .then() + .statusCode(200) + .body(is("This is another resource from a sub-directory.")); + } + + @Test + @DisplayName("should return content from included json resource") + void givenAPI_whenGetJsonResource_thenReturnsContent() { + given() + .when().get("/resources/json") + .then() + .statusCode(200) + .body("version", is("1.0.0")); + } + +} \ No newline at end of file