In this workshop we will show how to effectively leverage the supersonic and subatomic framework Quarkus to make fast and reliable java applications.
See the diagram below for the architecture of the app we will be building.
- Docker -> https://www.docker.com/
- GraalVM -> https://www.graalvm.org/downloads/
- Java: -> https://www.oracle.com/java/technologies/downloads/
- Favourite IDE
We have multiple ways of bootstrapping our application, using Intellij, Maven/Gradle or using quarkus initializer
- Navigate to https://code.quarkus.io/.
- Choose Group name and artifact name.
- Choose Build Tool (in this project Maven will be used).
- Choose Java version (in this project Java 17 will be used).
- Select the following dependencies.
- RESTEasy Classic Jackson
- Agroal - Database connection pool
- JDBC Driver - PostgreSQL
- RESTEasy Classic
- Hibernate ORM with Panache
- Under Generate your application - choose download as a zip.
- Unzip the app and import it in the IDE.
- We will be using postgres as part of docker - create a docker file for postgres container(refer to docker-postgres.yml file)
Starting the container is done with the following command
docker-compose -f "docker-postgres.yml" up -d
- Write a seed script to seed some data when the app is started (refer to seed.sql file)
- Configure your app to connect to the database (refer to application.properties file which is located under resources package)
- Create our Movie entity with all the information we need (refer to Movie.java file which is located under entity package)
- Create the Movie repository (refer to MovieRepository.java file which is located under repository package)
- Create the Movie resource (refer to MovieResource.java file which is located under web package)
- For tests refer to GreetingResourceTest.java file and MovieResourceTest.java files located under test package
Adding Docker Container extension for building linux executables
./mvnw quarkus:add-extension -Dextensions="container-image-docker"
You can run your application in dev mode that enables live coding using: ./mvnw compile quarkus:dev
Or you can run it in your IDE
The application can be packaged using:
./mvnw package
It produces the quarkus-run.jar
file in the target/quarkus-app/
directory.
Be aware that it’s not an über-jar as the dependencies are copied into the target/quarkus-app/lib/
directory.
The application is now runnable using java -jar target/quarkus-app/quarkus-run.jar
.
If you want to build an über-jar, execute the following command:
./mvnw package -Dquarkus.package.type=uber-jar
The application, packaged as an über-jar, is now runnable using java -jar target/*-runner.jar
.
You can create a native executable using:
./mvnw package -Dquarkus-profile=stage -Pnative
This will create native executable with profile stage (depending on your operating system) and this executable will be located under target as target/*-runner
You don't need anything besides your operating system to run it (for windows this is an exe file)
Build it as jvm image
./mvnw package "-Dquarkus.container-image.build=true" "-Dquarkus.container-image.tag=jvm-panache-hibernate"
Starting it (change the m.t with your docker repository)
docker run -i --rm -p 8080:8080 --network=quarkus-network m.t/quarkus-app:jvm-panache-hibernate
Build it as native image
./mvnw package "-Dquarkus.container-image.build=true" "-Dquarkus.package.type=native" "-Dquarkus.native.container-build=true" "-Dquarkus.container-image.tag=native-with-panache"
Starting it (change the m.t with your docker repository)
docker run -i --rm -p 8080:8080 --network=quarkus-network m.t/quarkus-app:native-panache-hibernate