Understanding the power of Quarkus

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

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.

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.

But How Quarkus is so fast and light-weight?

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

  1. Startup

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

An IDE
JDK 1.8+ installed with JAVA_HOME
Apache maven 3.6.2+

Online

https://code.quarkus.io/

Using maven command

The minimal command to create a quarkus project.

mvn io.quarkus:quarkus-maven-plugin:1.5.2.Final:create
mvn io.quarkus:quarkus-maven-plugin:1.5.2.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=lifecycle-quickstart \
-DclassName="org.acme.lifecycle.GreetingResource" \
-Dpath="/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

@QuarkusMain
public class Main {
public static void main(String... args) {
Quarkus.run(MyApp.class, args);
}
public static class MyApp implements QuarkusApplication {
@Override
public int run(String... args) throws Exception {
System.out.println("Do startup logic here");
Quarkus.waitForExit();
return 0;
}
}
}

First Rest API

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

mvn compile quarkus:dev
mvn package
java -jar target/<jar-name>.jar

Swagger

To access swagger and openAPI we need to add this dependency

<dependency>
<groupid>io.quarkus</groupid>
<artifactId>quarkus-smallrye-openapi</artifactid>
</dependency>
http://localhost:8080/swagger-ui/
http://localhost:8080/openapi

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

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.

Properties of multiple environments

quarkus.http.port=8080
%prod.quarkus.http.port=9090
%<environemnt-name>.<property-name>=<proeprty-value>

Other properties we can define in the application.properties file

Database configuration

quarkus.datasource.url=jdbc:postgresql:test_db
quarkus.datasource.driver=org.postgresql.Driver
quarkus.datasource.username=root
quarkus.datasource.password=root
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.log.sql=true
quarkus.log.level=INFO
quarkus.log.category."org.hibernate".level=DEBUG
# Other log levels FATAL, ERROR, DEBUG, INFO, WARN, TRACE
student.firstname=foo
student.lastname=bar
student.age=26

Accessing properties

We can access the properties using the @ConfigProperty annotation

@ConfigProperty(name = "student.firstname")
String name;
@ConfigProperty(name = "student.firstname", defaultValue = "mat")
String name;
import io.quarkus.arc.config.ConfigProperties;@ConfigProperties(prefix = "student")
public class Student {
private String firstname;
private String lastname;
private Integer age;

...
}

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