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