Table of Contents
Due to the actual microservices server paradigm & server costs corresponding to resource’s consumption, Quarkus is a framework that caught my attention some years ago, as it solves JVM language problems on these concerns. In this article you will find Quarkus GraalVM examples and explanation. We are going to create a basic Quarkus REST-API application & create a native image with GraalVM. Also we’ll mention some existing alternatives to Quarkus.
The generated Quarkus project for this article can be found in our GitHub repository.
This framework is designed to:
- minimize the size of the generated jar files.
- lower memory usage.
- fastest application startup.
Some metrics from the official site:
All these improvements can be accomplished thanks to the following:
- Use of GraalVM: native image build
- Build Time Metadata Processing: removes unused classes
- Reduction in reflection usage
- Native Image Pre Boot: preboot image as much as possible at build time serializing the state in the executable.
Note that for java projects jdk8 is supported but jdk11 is the default from version 1.13. Also Quarkus embraces the https://12factor.net/ witch is basic in modern software development.
LICENCE: under the Apache Software License 2.0 or compatible license.
Documentation & guides
The documentation in the official Quarkus page is complete enough and with lots of guides for specific implementations & integrations. Also checkout the official Quarkus github repository and the wiki.
Bootstrapping a project
We have several options to start a new project:
- Using https://code.quarkus.io/ (similar to Spring initializer) to bootstrap your project dependencies & archetype. Some extensions are in preview or experimental state, so be careful.
- With official quarkus maven archetypes in github.
- Via command line with the maven (or gradle) plugin.
- With IDE Quarkus plugins (f.ex. for Intellij is already bundled).
We are going to start with a simple example on how to create, build & run a project with the starter web-site. First we select some packages and download the project as a zip:
Quarkus starter web-site helps you with some info on how to proceed:
Now we are going to move it to a proper folder:
mkdir ~/projects/articles mv ~/Downloads/quarkus-basic ~/projects/articles/quarkus-basic cd ~/projects/articles/quarkus-basic
You can now import the project into your IDE. Checkout the README.md & some code examples generated by the initializer page.
Build & run the project
Be sure you use jdk11:
# With sdk-man (use or install): sdk use java 11.0.10.hs-adpt # Build & run with maven ./mvnw compile quarkus:dev
This should show you some logging in the terminal:
Build native image with GraalVM docker image
We can use maven-quarkus-plugin to use a dockerized GraalVM to create a native image of our application.
NOTE: On MacOS11.3 with docker version (20.10.5) it didn’t work, I was getting an out of memory error when packaging to native. I tried to set quarkus.native.native-image-xmx=6g in the application.properties to set some extra memory for the Java heap (see docs), but it didn’t work either. It works out-of-the-box in an Ubuntu 20.04.2 LTS.
And now execute next in terminal to generate the native image:
./mvnw package -Pnative -Dquarkus.native.container-build=true
It will take about 3 minutes to the quarkus-maven-plugin to package our small project:
And the memory (& cpu) usage is quite big:
As we can see, the generated runner’s at the target folder is quite small compared to Spring generated jars:
You can execute the native application from your terminal:
As you can see in the image below it starts really quickly (less than 1 second the first time & 0.030s second time):
Quarkus + GraalVM is great for small microservices projects for all performance improvements it brings into the scene, but these improvements decrease as much as project size increases.
Also to have in mind that the performance on Quarkus applications could be affected by the use of some Quarkus libraries in preview/experimental status, that need some performance & resource improvements.
Current alternatives are:
- Quarkus project github.
- Quarkus application initializer web-site.
- Check out the Quarkus guides!!
- Quarkus app seeds maven artifacts repository.
- InfoQ: Starting Fast: Investigating Java’s Static Compilation Landscape.
- DZone: AOT compilation.
- DZone: Quarkus vs Spring Boot.
- BetterProgramming: Java Microservice Frameworks.
- Medium: SpringBoot vs Quarkus: a Real-Life Experiment.
- Medium: Faster, Lower, Better with Quarkus in k8s
If you need any help with this kind of projects, let us know! We are experts in software development!