Difficulty: beginner
Estimated Time: 10 minutes

In this scenario, you will get an introduction to Quarkus.

What is Quarkus?

Logo

Supersonic, Subatomic Java

For years, the client-server architecture has been the de-facto standard to build applications. But a major shift happened. The one model rules them all age is over. A new range of applications and architecture styles has emerged and impacts how code is written and how applications are deployed and executed. HTTP microservices, reactive applications, message-driven microservices and serverless are now central players in modern systems.

Quarkus has been designed with this new world in mind, and provides first-class support for these different paradigms. Quarkus development model morphs to adapt itself to the type of application you are developing.

Quarkus is a Kubernetes Native Java stack tailored for GraalVM & OpenJDK HotSpot, crafted from the best of breed Java libraries and standards. Amazingly fast boot time, incredibly low RSS memory (not just heap size!) offering near instant scale up and high density memory utilization in container orchestration platforms like Kubernetes. Quarkus uses a technique called compile time boot. Learn more.

Unifies Imperative and Reactive

Application requirements have changed drastically over the last few years. For any application to succeed in the era of cloud computing, big data or IoT, going reactive is increasingly becoming the architecture style to follow.

Quarkus combines both the familiar imperative code and the non-blocking reactive style when developing applications.

Functions as a Service and Serverless

Thanks to their stellar startup time and low memory usage, you can implement functions using Quarkus to be used in serverless environments like AWS Lambda. You can use any of the Quarkus features in your function and benefit from the fast startup and low memory utilization. With Quarkus, you can embrace this new world without having to change your programming language.

Developer Joy

Quarkus provides a cohesive platform for optimized developer joy:

  • Unified configuration
  • Zero config, live reload in the blink of an eye
  • Streamlined code for the 80% common usages, flexible for the 20%
  • No hassle native executable generation

Other possibilities

Learn more at quarkus.io, or just drive on and get hands-on!

In this scenario, you learned about Quarkus, and how it can be used to create supersonic, subatomic Java applications. Quarkus provides an effective solution for running Java in this new world of serverless, microservices, containers, Kubernetes, FaaS, and the cloud because it has been designed with these in mind.

Its container-first approach for cloud-native Java applications unifies imperative and reactive programming paradigms for microservices development and offers an extensible set of standards-based enterprise Java libraries and frameworks combined with extreme developer productivity that promises to revolutionize the way we develop in Java.

Additional Resources

If you’re interested in helping continue to improve Quarkus, developing third-party extensions, using Quarkus to develop applications, or if you’re just curious about it, please check out these links:

Introduction to Quarkus

Step 1 of 6

Step 1

In this step, you will create a straightforward application serving a hello endpoint. To demonstrate dependency injection this endpoint uses a greeting bean.

Architecture

Wait for prerequisite downloads

A suitable Java runtime is being installed and should take less than a minute. Once it's done, continue below!

Create basic project

The easiest way to create a new Quarkus project is to click to run the following command:

mvn io.quarkus:quarkus-maven-plugin:2.0.0.Final:create \ -DprojectGroupId=org.acme \ -DprojectArtifactId=getting-started \ -DclassName="org.acme.quickstart.GreetingResource" \ -Dpath="/hello"

This will use the Quarkus Maven Plugin and generate a basic Maven project for you in the getting-started subdirectory, generating:

  • The Maven structure
  • An org.acme.quickstart.GreetingResource resource exposed on /hello
  • An associated unit test
  • A landing page that is accessible on http://localhost:8080 after starting the application
  • Example Dockerfiles for a variety of build targets (native, jvm, etc)
  • The application configuration file

Once generated, look at the getting-started/pom.xml. You will find the import of the Quarkus BOM, allowing to omit the version on the different Quarkus dependencies. In addition, you can see the quarkus-maven-plugin responsible of the packaging of the application and also providing the development mode.

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>${quarkus.platform.group-id}</groupId>
        <artifactId>${quarkus.platform.artifact-id}</artifactId>
        <version>${quarkus.platform.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

If we focus on the dependencies section, you can see we are using Quarkus extensions allowing the development and testing of REST applications:

  <dependencies>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-arc</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

During the project creation, the getting-started/src/main/java/org/acme/quickstart/GreetingResource.java file has been created with the following endpoint:

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello RESTEasy";
    }
}

It’s a very simple REST endpoint, returning "hello" to requests on /hello.

Compared to vanilla JAX-RS, with Quarkus there is no need to create an Application class. It’s supported but not required. In addition, only one instance of the resource is created and not one per request. You can configure this using the different *Scoped annotations (ApplicationScoped, RequestScoped, etc).

Running the Application

First, change to the directory in which the project was created:

cd /root/projects/quarkus/getting-started

Now we are ready to run our application. Click here to run:

mvn quarkus:dev -Dquarkus.http.host=0.0.0.0

You should see:

__  ____  __  _____   ___  __ ____  ______
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
INFO  [io.quarkus] (Quarkus Main Thread) getting-started 1.0.0-SNAPSHOT on JVM (powered by Quarkus x.x.x.Final) started in 1.194s. Listening on: http://0.0.0.0:8080
INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]
--
Tests paused, press [r] to resume, [h] for more options>

Note the amazingly fast startup time! Once started, you can request the provided endpoint in the browser using this link.

You should see:

Hello RESTEasy

It's working!

Now, let's exercise the live reload capabilities of Quarkus. Click here to open the endpoint: getting-started/src/main/java/org/acme/quickstart/GreetingResource.java. Change return "Hello RESTEasy"; to return "Hola RESTEasy"; on line 14 in the editor. Don't save. Don't recompile or restart anything. Just try to reload the brower (or click here again.)

You should see the updated hola message.

Wow, how cool is that? Supersonic Subatomic live reload! Go ahead and change it a few more times and access the endpoint again. And we're just getting started.

quarkus:dev runs Quarkus in development mode. This enables live reload with background compilation, which means that when you modify your Java files your resource files and refresh your browser these changes will automatically take effect. This will also listen for a debugger on port 5005. If your want to wait for the debugger to attach before running you can pass -Ddebug on the command line. If you don’t want the debugger at all you can use -Ddebug=false.

The Dev UI

When running in Developer mode, Quarkus apps expose a useful UI for inspecting and making on-the-fly changes to the app (much like live coding mode). It allows you to quickly visualize all the extensions currently loaded, see and edit their configuration values, see their status and go directly to their documentation.

To access the Dev UI for your running app, click this link which should open up the Dev UI in a new browser tab.

Dev UI

For example, click on the Config Editor link within the Configuration tile to see and make updates to configuration. This is super useful for developers to confirm code and configuration changes, or experiment with various settings.

NOTE The Dev UI is only enabled when in developer mode. It is not deployed when in production mode, as it's designed for developers to use during development. For more detail on what you can do, check out the Dev UI Guide.

Continuous Testing

When in developer mode (via mvn quarkus:dev), Quarkus can automatically and continuously run your unit tests. You may have noticed in the console Tests paused, press [r] to resume, [h] for more options>. This is an indication that you can enter continuous test mode. Type r to turn continuous testing mode on in the console.

Earlier, you changed Hello to Hola which broke the default unit test, and you can now see this in the console:

Response body doesn't match expectation.
Expected: is "Hello RESTEasy"
  Actual: Hola RESTEasy

Let's fix the test. Change Hola RESTEasy back to Hello RESTEasy in the editor. As soon as you fix it, Quarkus automatically re-runs the test and you should now have passing tests:

All 1 tests are passing (0 skipped), 1 tests were run in 389ms. Tests completed at 12:25:40 due to changes to GreetingResource.class.

Quarkus analyses your unit tests and only re-runs the tests that are affected by code changes. It's one of many developer productivity features of Quarkus, providing immediate feedback to developers as they type. We'll leave the tests running continously just to ensure we don't mess up later.

Congratulations!

You've seen how to build a basic app, package it as an executable JAR and start it up very quickly. You also saw how Quarkus can run tests continuously to turbocharge your development tasks and facilitate test-driven development. We'll leave the app running and rely on hot reload for the next steps.

In the next step we'll inject a custom bean to showcase Quarkus' CDI capabilities.