Getting started with Quarkus |Quarkus Tutorial

(*Note - This article has been updated and tested on the latest version of Quarkus 1.13.6)

If you are new to Qurakus then this article will help you in Getting Started with Quarkus. In this article of Getting started with Quarkus |Quarkus Tutorial, we will be developing a Hello World RestEnd Point application as well as testing the Hot Reload feature provided by the Quarkus.



What is Quarkus?

Supersonice Subatomic Java and its a Java stack from Kubernetes(k8s) native. Quarkus is being developed for OpenJDK, HotSpot and GraalVM.

Quarkus has been developed on the principle of Container First, which eventually results in very low memory usage and extremely fast startup time.

What Quarkus offers?

Quarkus Faster Boot Time

Fast boot time (Native Image Pre Boot) - What Quarkus does is it pre-boots as much of the framework as possible while building the native image . This ultimately results a native image which has already run most of the startup code. Here are stats of quarkus boot time + response time
Image Source - https://quarkus.io/

Quarkus Support for Graal/SubstrateVM - In Quarkus the application is compiled to its native image which significantly starts up much faster and takes very less heap as compared to standard JVM.

Build Time Metadata Processing - Quarkus application contains the classes which are only needed at the runtime while in the traditional world(Spring boot) all classes are required at the deployment which hangs around for the whole life cycle of the application.

So if you are using the Quarkus then non-necessary classes are not loaded in the JVM and it ultimately provides very efficient memory usage and extremely fast startup time.

No Reflection API in Quarkus - Quarkus does not use reflection API reducing startup time and memory usage.

Quarkus memory usage



quarkus:dev - Debugging Mode/ Live reload/ Hot deploy

One of the feature which I liked the most about the quarkus is "quarkus:dev". So what does it provides

  1. It allows the developer to run/deploy code without restart
  2. You can simply make a change in the Java code and you need to refresh the browser. It will update the code on the fly
  3. Live reload/Hot deploy will let you increase your productivity many folds by avoiding those unnecessary vicious cycle of build and deploy

Quarkus Setup requirements

  1. IDE - IntelliJ IDEA, Eclipse, VSCode, Vim or Emacs
  2. JDK - JDK 8 or 11+ (any distribution) or GraalVM 19.2.1 or 19.3.1 (Optional)
  3. Build Tool - Apache Maven 3.5.3+ or Gradle
  4. Quarkus - Quarkus 1.13.6.Final


Bootstrapping with Quarkus

The sample application which we going to build with Quarkus is RESTful web application with one restend point. For the RESTful application, JAX-RS extension is included by default with Quarkus.
In this tutorial, we are going to create simple rest endpoint "/hello" which will return string.

There are two ways you can bootstrap your Quarkus application

  1. Maven to generate Quarkus application
  2. UI based application generator from - https://code.quarkus.io/

Maven to generate Quarkus application

Use the following maven command to generate the Quarkus application

1mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
2    -DprojectGroupId=com.jhooq \
3    -DprojectArtifactId=hello-world \
4    -DclassName="com.jhooq.JhooqHelloWorld"


UI based application generator for Quarkus

Head over to Quarkus then you need to fill in the following information

  1. Group - com.jhooq
  2. Artifact - hello-world
  3. Build Tool - Gradle
  4. Version - 1.13.6
  5. Package Name - com.jhooq
  6. Quarkus Version - 1.13.6 (This is the latest version Quarkus had at the time writing this article)

After filling the above details, please click on "Generate your application" then you can download the zip file. Later which can be imported to IDE (Intellij, Eclipse…)

Please refer to the following image for UI based application generator

Generate Quarkus application from https://code.quarkus.io/

Implement REST API with Quarkus

Now let's do some implementation work. Here we are going to create a Java class JhooqHelloWorld.java with one REST endpoint (/hello) which will return "Hello World ! Welcome to Jhooq"

 1import javax.ws.rs.GET;
 2import javax.ws.rs.Path;
 3import javax.ws.rs.Produces;
 4import javax.ws.rs.core.MediaType;
 5
 6@Path("/hello")
 7public class ExampleResource {
 8
 9    @GET
10    @Produces(MediaType.TEXT_PLAIN)
11    public String customerOrder() {
12        return "hello world ! From Jhooq";
13    }
14
15}

As you can see in the above class we do not need to define main() method like we used to define in the SpringBoot implementation.

Quarkus comes with very simplistic approach and removed all boiler plat code which is required for implementing your business need

In the above REST implementation, we are using JAX-RS



build.gradle

This is how the build.gradle looks like

 1plugins {
 2    id 'java'
 3    id 'io.quarkus'
 4}
 5
 6repositories {
 7     mavenLocal()
 8     mavenCentral()
 9}
10
11dependencies {
12    implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
13    implementation 'io.quarkus:quarkus-resteasy'
14
15    testImplementation 'io.quarkus:quarkus-junit5'
16    testImplementation 'io.rest-assured:rest-assured'
17}
18
19group 'com.jhooq'
20version '1.0.0-SNAPSHOT'
21
22compileJava {
23    options.compilerArgs << '-parameters'
24}
25
26java {
27    sourceCompatibility = JavaVersion.VERSION_1_8
28    targetCompatibility = JavaVersion.VERSION_1_8
29}

gradlew assemble quarkusDev - Running Quarkus Application

To run the Quarkus application you need to execute the following command into the terminal

1./gradlew assemble quarkusDev

After executing the above gradlew command you could see the following logs in your console

 1gradlew assemble quarkusDev
 2Starting a Gradle Daemon, 2 incompatible and 1 stopped Daemons could not be reused, use --status for details
 3
 4> Task :quarkusBuild
 5building quarkus runner
 6Listening for transport dt_socket at address: 5005
 72020-03-12 19:57:28,776 INFO  io.quarkus] (main) Quarkus 1.2.1.Final started in 2.079s. Listening on: http://0.0.0.0:8080
 82020-03-12 19:57:28,807 INFO  io.quarkus] (main) Profile dev activated. Live Coding activated.
 92020-03-12 19:57:28,807 INFO  io.quarkus] (main) Installed features: cdi, resteasy]
10<===========--> 85% EXECUTING 8m 13s]
11> :quarkusDev

For maven you can use the following command

1./mvnw compile quarkus:dev

Test Quarkus Application

Let's test our changes by accessing the rest endpoint which we have implemented in the JhooqHelloWorld.java

You can use follow curl command

1curl http://localhost:8080/hello
2Hello World ! Welcome to Jhooq

Or you can access the same url via browser also

Running Quarkus Application

Conclusion

  1. In this article of "Getting started with Quarkus |Quarkus Tutorial", you have looked at our first hello world example with Quarkus
  2. We got to know different ways of generating the Quarkus project either from https://code.quarkus.io/ or using Maven or Gradle
  3. We developed our first REST application endpoint using JAX-RS
  4. After this session, I could certainly say that Quarkus has definitely taken an upper edge when it comes to developing and deploying Cloud Native application. Because of its super quick startup time and less memory footprint.
  5. Let me know what you think about using Quarkus in your production environment.

Next Step - Run Quarkus inside docker | Dockerizing a Quarkus Application