|
| 1 | +# Quarkus demo: Hibernate Data Repositories (Jakarta Data) and RESTEasy |
| 2 | + |
| 3 | +This is a minimal CRUD service exposing a couple of endpoints over REST, |
| 4 | +with a front-end based on Angular so you can play with it from your browser. |
| 5 | + |
| 6 | +While the code is surprisingly simple, under the hood this is using: |
| 7 | + - [Hibernate Data Repositories (Jakarta Data)](https://hibernate.org/repositories/) to perform the CRUD operations on the database |
| 8 | + - RESTEasy to expose the REST endpoints |
| 9 | + - A PostgreSQL database; see below to run one via Docker |
| 10 | + - ArC, the CDI inspired dependency injection tool with zero overhead |
| 11 | + - The high performance Agroal connection pool |
| 12 | + - Infinispan based caching |
| 13 | + - All safely coordinated by the Narayana Transaction Manager |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +To compile and run this demo you will need: |
| 18 | + |
| 19 | +- JDK 17+ |
| 20 | +- GraalVM |
| 21 | + |
| 22 | +In addition, you will need either a PostgreSQL database, or Docker to run one. |
| 23 | + |
| 24 | +### Configuring GraalVM and JDK 17+ |
| 25 | + |
| 26 | +Make sure that both the `GRAALVM_HOME` and `JAVA_HOME` environment variables have |
| 27 | +been set, and that a JDK 17+ `java` command is on the path. |
| 28 | + |
| 29 | +See the [Building a Native Executable guide](https://quarkus.io/guides/building-native-image) |
| 30 | +for help setting up your environment. |
| 31 | + |
| 32 | +## Building the demo |
| 33 | + |
| 34 | +Launch the Maven build on the checked out sources of this demo: |
| 35 | + |
| 36 | +> ./mvnw package |
| 37 | +
|
| 38 | +## Running the demo |
| 39 | + |
| 40 | +### Live coding with Quarkus |
| 41 | + |
| 42 | +The Maven Quarkus plugin provides a development mode that supports |
| 43 | +live coding. To try this out: |
| 44 | + |
| 45 | +> ./mvnw quarkus:dev |
| 46 | +
|
| 47 | +In this mode you can make changes to the code and have the changes immediately applied, by just refreshing your browser. |
| 48 | + |
| 49 | +Hot reload works even when modifying your JPA entities or Jakarta Data repositories. |
| 50 | +Try it! Even the database schema will be updated on the fly. |
| 51 | + |
| 52 | +### Run Quarkus in JVM mode |
| 53 | + |
| 54 | +When you're done iterating in developer mode, you can run the application as a |
| 55 | +conventional jar file. |
| 56 | + |
| 57 | +First compile it: |
| 58 | + |
| 59 | +> ./mvnw package |
| 60 | +
|
| 61 | +Next we need to make sure you have a PostgreSQL instance running (Quarkus automatically starts one for dev and test mode). To set up a PostgreSQL database with Docker: |
| 62 | + |
| 63 | +> docker run --rm=true --name quarkus_test -e POSTGRES_USER=quarkus_test -e POSTGRES_PASSWORD=quarkus_test -e POSTGRES_DB=quarkus_test -p 5432:5432 postgres:17 |
| 64 | +
|
| 65 | +Connection properties for the Agroal datasource are defined in the standard Quarkus configuration file, |
| 66 | +`src/main/resources/application.properties`. |
| 67 | + |
| 68 | +Then run it: |
| 69 | + |
| 70 | +> java -jar ./target/quarkus-app/quarkus-run.jar |
| 71 | +
|
| 72 | +Have a look at how fast it boots. |
| 73 | +Or measure total native memory consumption... |
| 74 | + |
| 75 | +### Run Quarkus as a native application |
| 76 | + |
| 77 | +You can also create a native executable from this application without making any |
| 78 | +source code changes. A native executable removes the dependency on the JVM: |
| 79 | +everything needed to run the application on the target platform is included in |
| 80 | +the executable, allowing the application to run with minimal resource overhead. |
| 81 | + |
| 82 | +Compiling a native executable takes a bit longer, as GraalVM performs additional |
| 83 | +steps to remove unnecessary codepaths. Use the `native` profile to compile a |
| 84 | +native executable: |
| 85 | + |
| 86 | +> ./mvnw package -Dnative |
| 87 | +
|
| 88 | +After getting a cup of coffee, you'll be able to run this binary directly: |
| 89 | + |
| 90 | +> ./target/hibernate-orm-jakarta-data-quickstart-1.0.0-SNAPSHOT-runner |
| 91 | +
|
| 92 | +Please brace yourself: don't choke on that fresh cup of coffee you just got. |
| 93 | + |
| 94 | +Now observe the time it took to boot, and remember: that time was mostly spent to generate the tables in your database and import the initial data. |
| 95 | + |
| 96 | +Next, maybe you're ready to measure how much memory this service is consuming. |
| 97 | + |
| 98 | +N.B. This implies all dependencies have been compiled to native; |
| 99 | +that's a whole lot of stuff: from the bytecode enhancements that Hibernate ORM |
| 100 | +applies to your entities, to the lower level essential components such as the PostgreSQL JDBC driver, the Undertow webserver. |
| 101 | + |
| 102 | +## See the demo in your browser |
| 103 | + |
| 104 | +Navigate to: |
| 105 | + |
| 106 | +<http://localhost:8080/index.html> |
| 107 | + |
| 108 | +Have fun, and join the team of contributors! |
| 109 | + |
| 110 | +## Running the demo in Kubernetes |
| 111 | + |
| 112 | +This section provides extra information for running both the database and the demo on Kubernetes. |
| 113 | +As well as running the DB on Kubernetes, a service needs to be exposed for the demo to connect to the DB. |
| 114 | + |
| 115 | +Then, rebuild demo docker image with a system property that points to the DB. |
| 116 | + |
| 117 | +```bash |
| 118 | +-Dquarkus.datasource.jdbc.url=jdbc:postgresql://<DB_SERVICE_NAME>/quarkus_test |
| 119 | +``` |
0 commit comments