Understanding the power of Quarkus

Quarkus is a full-stack, light-weight, superfast java framework for the JVM and the native application.

Image for post
Image for post

But why so hype about Quarkus? Why should we learn Quarkus? when we already have Spring boot like framework. which is an open-source framework. It gives a simple way to build, configure, and build web applications.

So let’s start out talking with the comparison between Quarkus and Spring boot.

Quarkus vs Spring boot

1. Start-up time :

If we compare the startup time between the Quarkus native, Quarkus JIT, and traditional native application(Spring boot). We can clearly see the difference.

Image for post
Image for post

2. Memory footprint

Memory footprint basically means the amount of main memory that a program uses or references while running. Here also we can see the drastic difference between the Quarkus the Spring boot or any other traditional native stack.

Image for post
Image for post

But How Quarkus is so fast and light-weight?

So a java application running process can be divided into 2 parts

  1. Compiling
  2. Startup

The compiling process usually contains resolving all the dependencies, running test cases, generating reports if any then bundle up everything into a jar file or war file.

The startup process usually contains file scanning, annotation scanning metamodel creation, etc.

Both processes take there own time.

So the special thing that quarkus does is it moves all the startup process work into the compilation process. This process is known as AOT (Ahead of time compilation). As because of AOT, we don’t need to take lots of metadata related classes in the jar file which use to be there just for the startup process and after that, they sit idle. which helped to reduce the jar size and as all the work moves to the compilation phase, it increased the bootup time as well. Quarkus achieved hot deployment because of its quick startup time. We need not redeploy the Quarkus application after making any change. It will automatically detect the changes and redeploys.

How hot deployment works.

Whenever we make any change on the Quarkus JVM application. Quarkus gets shutdown, reads the changes, and restarts. There are no other metadata to deal with so this process happens in milliseconds.

Ways to create Quarkus application

Prerequisites

Basically we can create a Quarkus application using

https://code.quarkus.io/

We can go to this website and can create a quick quarkus project. Here we can select the extensions that we need. Extensions are nothing but dependencies.

The minimal command to create a quarkus project.

When we run this command on terminal. It will ask you to enter the groupId, artifact Id, default resource name, snapshot number.

Herewith this command, we can add other details like:

Here we are defining the group id, artifact id, first resource or controller and 1 sample rest API with endpoint ‘/hello’

No main class

Unlike spring boot in which we use to have a main class having the main method by default from where the execution starts. Hereby default, we don’t have any main class. Quarkus handles it in the background but we can create one if we need to add some login on the start of the application. We need to annotate the main class with @QuarkusMain

First Rest API

After creating Quarkus application from one of the ways defined above. we can run our application using the command :

Here dev represents the environment of the application. This command will run the application in a dev environment.

OR

we can create a jar file and can run it.

Quarkus jar name ends with runner.jar like sample-project.runner.jar

Once the application starts go to a browser and enter the URL:

http://localhost:8080/hello

Image for post
Image for post

yup, Simple as you like!

By default, quarkus runs on the port 8080 and we can always change that.

Quarkus supports both kinds of annotations for mapping

spring annotations like @RequestMapping,@RestController, etc

javac annotations like @GET, @PATH, @Produces, etc

Swagger

To access swagger and openAPI we need to add this dependency

We can access Swagger at :

Image for post
Image for post

and openAPI at

If we run this URL, it will download a file containing all the information of all the APIs.

Special 404 page

In case we hit any wrong URL. Quarkus helps us figure out the right URL by providing all the URL details on the 404 resource not found page. It also shows static pages we have in our application

Image for post
Image for post

Property file

The property file is quite similar to that of spring boot with the same name ‘application.properties’. The only and the big difference between Quarkus and spring-boot application property file is in spring-boot we usually define environment-specific property file but in Quarkus we can define properties of all the environment in 1 file.

Here we defined the default port number which will run for all the environments except for the production. To define any environment property we can define like

Database configuration

Hibernate details

Log level

Custom configurations

and many more…

We can access the properties using the @ConfigProperty annotation

we can also provide the default value in @CofigProperty

If we have multiple fields to set then this way to set values will not be efficient. In that scenario, we can use @ConfigProperties

In the next blog, we will look at how to connect different databases with quarkus and will create a CRUD application.

Written by

Full stack developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store