In this scenario, you will get an introduction to the reactive programming model of Quarkus
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.
Quarkus uses Vert.x and Netty at its core. And uses a bunch of reactive frameworks and extensions on top to enable the developers. Quarkus is not just for HTTP microservices, but also for Event-Driven Architecture. The secret behind this is to use a single reactive engine for both imperative and reactive code.
Quarkus does this quite brilliantly. Between imperative and reactive the obvious choice is to have a reactive core. What that helps with is a fast non-blocking core that handles almost everything going via the event-loop. So if you were creating a typical REST application or a client-side application, Quarkus also gives you the imperative programming model. For example, Quarkus HTTP support is based on a non-blocking and reactive engine (Eclipse Vert.x and Netty). All the HTTP requests your application receive are handled by event loops (IO Thread) and then are routed towards the code that manages the request. Depending on the destination, it can invoke the code managing the request on a worker thread (Servlet, Jax-RS) or use the IO Thread (reactive route)
In this scenario you will create a simple
Coffee Resource endpoint by using JAX-RS with Quarkus backed by the Reactive SQL drivers; In our example we will use PostgreSQL Reactive SQL Driver. We will add methods like list, add and remove items from our list of famous Coffee.
In every architecture component data is a key ingredient. With no surprises,relational databases store and crunch major amounts of data for applications to be used. Historically, traditional databases have not been reactive, that also trickles down to the JDBC API since using those databases have to be in conjuction with the underlying architecture, which ensures consistency, transactions, ACID etc. However in recent years, improvements have been made and JDBC access can now be done reactivels.
Some of the advantages with reactive SQL with Quarkus are:
- Simple API focusing on scalability and low overhead.
- Reactive and non blocking which able to handle many database connections with a single thread.
- Ranked #1 in the TechEmpower Benchmark Round 15 Single query benchmark
Learn more at quarkus.io, or just drive on and get hands-on!
In this scenario, you learned about Quarkus and the Reactive SQL, 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. Quarkus 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.
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:
- Four reasons to try Quarkus
- Reactive Routes
- Reactive SQL Clients
- Kafka Extension
- AMQP Extension
- Using Vertx API in Quarkus
Reactive programming with Quarkus Reactive SQL
Inspect Java runtime
An appropriate Java runtime has been installed for you. Ensure you can use it by running this command:
If the command fails, wait a few moments and try again (it is installed in a background process and make take a few moments depending on system load).
The command should report the version in use, for example (the versions and dates may be slightly different than the below example):
openjdk 11.0.10 2021-01-19 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)
Import the code
Let's refresh the code we'll be using. Run the following command to clone the sample project:
cd /root/projects && rm -rf rhoar-getting-started && git clone https://github.com/openshift-katacoda/rhoar-getting-started
You start with a basic Maven-based application with the usual
pom.xml entries for a Quarkus app.
We've also included a frontend HTML file at
src/main/resources/META-INF/resources/index.html that will show our list of Coffee.
The Application You Will Build
The application is a simple CRUD app with a Front end that lists Coffee and gives options to remove and add more Coffee.
We also use a CoffeeResource that helps us define those methods with JAX-RS.
Further more we use a PostgreSQL database, where we create the databses, read from and write to it.
Lets get started. We have already created a project for you, and lets continue adding functionality to this bare bones project.
In this guide, we will use the Mutiny API of the Reactive PostgreSQL Client. If you’re not familiar with Mutiny reactive types, read the Getting Started with Reactive guide if you want to learn more!
Like other exercises, we’ll need another extension to start using the PosgtreSQL. Lets install it by clicking on the following command:
cd /root/projects/rhoar-getting-started/quarkus/reactive-sql &&
mvn quarkus:add-extension -Dextensions="reactive-pg-client"
The first time you add the extension, new dependencies may be downloaded via maven. This should only happen once, after that things will go even faster.
This will add the necessary entries in your
pom.xml to bring in the Reactive PostgreSQL extension. You'll see:
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-reactive-pg-client</artifactId> </dependency>
There are a few other extensions we'll use that are already there, including
resteasy-jackson (for encoding Java objects as JSON objects).
With the app initialized, lets start coding!