Today, developing and deploying applications to the Cloud is no longer a strange thing. We don’t need to worry much about the infrastructure to deploy the application, everything Cloud Providers has already installed, we just focus on developing the application.
The trend of deploying applications on the Cloud is how to be scalable, in short, we can deploy an application with a load balancer, which can handle a large number of requests from users at the same time. To do this, our deployment strategy must accommodate that the number of instances of the application can increase or decrease based on the traffic to the application and also, when starting a new instance to handle user requests, the fastest start time is possible.
For scripting languages like JavaScript, PHP, or Python, when starting a new instance of the application, they can almost immediately handle requests from users. But with Java, applications need a start time to handle requests.
Understanding those limitations, there are many new Java frameworks and libraries introduced to solve this start-time problem, aiming to help us develop Cloud Native applications with Java. One of them is Quarkus, a Supersonic Subatomic Java, optimized to run Java applications using containers in the Cloud environment.
In this tutorial, I will give you an overview of Quarkus and how to up and run a Java application using Quarkus, to see, how long it takes to run an application with Quarkus!
First, let’s talk a little about the term Supersonic Subatomic Java of Quarkus.
Supersonic in a nutshell, as I said above, the time for a Quarkus application to start and be ready to handle requests is significantly reduced compared to other Java frameworks. Normally, for a RESTful API application without a connection to the database, implemented using Java frameworks other than Quarkus, the average time to start the application can take at least 4s, but for Quarkus using HotSpot JVM, this time is only about 0.7s on average. If Quarkus runs with GraalVM, this time is reduced even more, only about 0.01s. Thanks to that, Quarkus applications can almost immediately start and handle user requests.
Subatomic is related to the size of the Quarkus application. When we build Quarkus applications, the Quarkus executable file will be smaller than when we build applications using other Java frameworks. Another thing is that Quarkus applications will consume less memory!
Now, we will create a new Quarkus project as an example!
Create new Quarkus project
Similar to the Spring framework with Spring Initializr https://start.spring.io/, Quarkus also has a tool to help us quickly create a new Quarkus project at https://code.quarkus.io/.
You can quickly create a new RESTful API application using the Quarkus RESTEasy extension or using the Quarkus REST extension in a reactive manner. In simple terms, extensions are like dependencies!
For example, I create a new RESTful API application by declaring the use of the Quarkus RESTEasy extension, an implementation of Jakarta EE Java API for RESTful Web Services, as follows:
You can edit project information if you want!
Press Generate your application:
and download the project to your computer.
Results of opening the project in IntelliJ:
By default, as you can see, Quarkus Code will generate a GreetingResource class that defines the endpoint “/hello” using JAX-RS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava.quarkus; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; @Path("/hello") public class GreetingResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello RESTEasy"; } } |
Run application
You can run this application to use in development mode using the quarkus-maven-plugin that is declared with the generated project, by running the Maven command as follows:
1 |
compile quarkus:dev |
Open the browser and go to the address http://localhost:8080/, you will see the following results:
Open IntellJ’s Console, you will see the following log lines:
1 2 3 4 5 6 7 8 9 10 11 12 |
Listening for transport dt_socket at address: 5005 Press [e] to edit command line args (currently ''), [h] for more options> Tests paused Press [e] to edit command line args (currently ''), [r] to resume testing, [h] for more options> Press [e] to edit command line args (currently ''), [r] to resume testing, [o] Toggle test output, [h] for more options> __ ____ __ _____ ___ __ ____ ______ --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \ --\___\_\____/_/ |_/_/|_/_/|_|\____/___/ 2024-08-07 06:41:41,424 INFO [io.quarkus] (Quarkus Main Thread) quarkus-example 1.0.0-SNAPSHOT on JVM (powered by Quarkus 3.13.0) started in 0.855s. Listening on: http://localhost:8080 2024-08-07 06:41:41,426 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated. 2024-08-07 06:41:41,426 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-context-propagation, vertx] |
In line 10 of the above log, if you notice, the start time of this example application is 0.855s. Too fast, right?
But if you now run this application by command line, with JAR file, by opening Terminal on macOS or Linux or Console on Windows, then go to the project folder and run the command:
1 |
mvn clean package |
then go to the tartget/quarkus-app directory, run the command:
1 |
java -jar quarkus-run.jar |
You will see the following result:
As you can see, the application start time is now less than half when we run in development mode in IntelliJ, only 0.355s.
If you build this application with the native profile using GraalVM with the native-image component, with the command:
1 |
mvn package -Dnative |
then you can run the application directly using the file quarkus-example-1.0.0-SNAPSHOT-runner in the target directory of the project.
The application start time will surprise you as follows:
Very fast, right? Just press the execute command and the application starts immediately. As you can see, it only takes 0.014s for our example application to be up and running. This is also the time that the application can be up and running on a Cloud environment.
John Clingan
Nice introduction! Maybe a followup article talking about Quarkus developer productivity like dev services, dev ui, continuous testing?
BTW, Quarkus does not support Jakarta EE, although it does support many Jakarta specifications. Jakarta EE is a specification that specifies a collection of ~30+ Jakarta specifications and how they work together. Quarkus does fully support MicroProfile, however.
Khanh Nguyen
Thank @John,
Appreciate your comments. I will have some other tutorials about Quarkus in the upcoming time.