Understanding the power of Quarkus
Quarkus is a full-stack, light-weight, superfast java framework for the JVM and the native application.
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.
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
- Compiling
- 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
An IDE
JDK 1.8+ installed with JAVA_HOME
Apache maven 3.6.2+
Basically we can create a Quarkus application using
Online
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.
Using maven command
The minimal command to create a quarkus project.
mvn io.quarkus:quarkus-maven-plugin:1.5.2.Final:create
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:
mvn io.quarkus:quarkus-maven-plugin:1.5.2.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=lifecycle-quickstart \
-DclassName="org.acme.lifecycle.GreetingResource" \
-Dpath="/hello"
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
@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
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.
mvn package
java -jar target/<jar-name>.jar
Quarkus jar name ends with runner.jar like sample-project.runner.jar
Once the application starts go to a browser and enter the URL:
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
<dependency>
<groupid>io.quarkus</groupid>
<artifactId>quarkus-smallrye-openapi</artifactid>
</dependency>
We can access Swagger at :
http://localhost:8080/swagger-ui/
and openAPI at
http://localhost:8080/openapi
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
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
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
%<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
Hibernate details
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.log.sql=true
Log level
quarkus.log.level=INFO
quarkus.log.category."org.hibernate".level=DEBUG# Other log levels FATAL, ERROR, DEBUG, INFO, WARN, TRACE
Custom configurations
student.firstname=foo
student.lastname=bar
student.age=26
and many more…
Accessing properties
We can access the properties using the @ConfigProperty annotation
@ConfigProperty(name = "student.firstname")
String name;
we can also provide the default value in @CofigProperty
@ConfigProperty(name = "student.firstname", defaultValue = "mat")
String name;
If we have multiple fields to set then this way to set values will not be efficient. In that scenario, we can use @ConfigProperties
import io.quarkus.arc.config.ConfigProperties;@ConfigProperties(prefix = "student")
public class Student {
private String firstname;
private String lastname;
private Integer age;
...
}
In the next blog, we will look at how to connect different databases with quarkus and will create a CRUD application.