Skip to content

Commit 16d961f

Browse files
committed
Minor updates to the quickstart guide
1 parent e590c43 commit 16d961f

File tree

2 files changed

+43
-83
lines changed

2 files changed

+43
-83
lines changed

quickstart/README.adoc

Lines changed: 42 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ We recommend that you follow the instructions in the next sections and create th
5858
by step. However, you can go right to the completed example.
5959

6060
The solution is located in the
61-
link:https://github.com/datastax/cassandra-quarkus/tree/master/quickstart[quickstart directory] of
61+
link:https://github.com/datastax/cassandra-quarkus/tree/main/quickstart[quickstart directory] of
6262
the Cassandra Quarkus extension GitHub repository.
6363

6464
== Creating a Blank Maven Project
@@ -95,7 +95,7 @@ public class Fruit {
9595

9696
As stated above, we are using the DataStax Object Mapper. In other words, we are not going to write
9797
our CQL queries manually; instead, we will annotate our data model with a few annotations, and the
98-
mapper will generate proper CQL queries for us underneath.
98+
mapper will generate proper CQL queries underneath.
9999

100100
This is why the `Fruit` class is annotated with `@Entity`: this annotation marks it as an _entity
101101
class_ that is mapped to a Cassandra table. Its instances are meant to be automatically persisted
@@ -143,11 +143,11 @@ public interface FruitMapper {
143143
----
144144

145145
The `@Mapper` annotation is yet another annotation recognized by the DataStax Object Mapper. A
146-
mapper is responsible for constructing instances of DAOs – in this case, out mapper is constructing
146+
mapper is responsible for constructing DAO instances – in this case, out mapper is constructing
147147
an instance of our only DAO, `FruitDao`.
148148

149-
Think of the mapper interface as a factory for DAO beans: if you intend to construct and inject a
150-
specific DAO bean in your own code, then you first need to add a `@DaoFactory` method for it in a
149+
Think of the mapper interface as a factory for DAO beans. If you intend to construct and inject a
150+
specific DAO bean in your own code, then you first must add a `@DaoFactory` method for it in a
151151
`@Mapper` interface.
152152

153153
TIP: `@DaoFactory` method names are irrelevant.
@@ -161,15 +161,15 @@ TIP: `@DaoFactory` method names are irrelevant.
161161
TIP: `Uni` is a type from the Mutiny library, which is the reactive programming library used by
162162
Quarkus. This will be explained in more detail in the "Reactive Programming" section below.
163163

164-
== Generating the DAO and Mapper Implementations
164+
== Generating the DAO and mapper implementations
165165

166166
As you probably guessed already, we are not going to implement the interfaces above. Instead, the
167167
Object Mapper will generate such implementations for us.
168168

169169
The Object Mapper is composed of 2 pieces:
170170

171171
1. A (compile-time) annotation processor that scans the classpath for classes annotated with
172-
`@Entity`, and generates code for them; and
172+
`@Mapper`, `@Dao` or `@Entity`, and generates code and CQL queries for them; and
173173
2. A runtime module that contains the logic to execute the generated queries.
174174

175175
Therefore, enabling the Object Mapper requires two steps:
@@ -203,40 +203,33 @@ With Gradle, this is done by adding the following line to the `build.gradle` fil
203203
annotationProcessor "com.datastax.oss.quarkus:cassandra-quarkus-mapper-processor:${cassandra-quarkus.version}"
204204
----
205205

206-
IMPORTANT: make sure you are enabling the right annotation processor! The Cassandra Quarkus
207-
extension requires the `cassandra-quarkus-mapper-processor` annotation processor, and not the Java
208-
driver's `java-driver-mapper-processor`. The former has more capabilities than the latter, and is
209-
the only one suitable for use in a Quarkus application.
206+
IMPORTANT: Verify that you are enabling the right annotation processor! The Cassandra driver ships
207+
with its Object Mapper annotation processor, called `java-driver-mapper-processor`. But the
208+
Cassandra Quarkus extension also ships with its own annotation processor:
209+
`cassandra-quarkus-mapper-processor`, which has more capabilities than the driver's. This annotation
210+
processor is the only one suitable for use in a Quarkus application, so check that this is the one
211+
in use. Also, never use both annotation processors together.
210212

211213
[start=2]
212-
1. Declare the `cassandra-quarkus-mapper-runtime` dependency in compile scope in the project's
213-
`pom.xml` file as follows:
214+
1. Declare the `java-driver-mapper-runtime` dependency in compile scope in the project's `pom.xml`
215+
file as follows:
214216

215217
[source,xml]
216218
----
217219
<dependency>
218-
<groupId>com.datastax.oss.quarkus</groupId>
219-
<artifactId>cassandra-quarkus-mapper-runtime</artifactId>
220-
<version>${project.version}</version>
221-
<scope>compile</scope>
220+
<groupId>com.datastax.oss</groupId>
221+
<artifactId>java-driver-mapper-runtime</artifactId>
222222
</dependency>
223223
----
224224

225-
With Gradle, this is done by adding the following line to the `build.gradle` file:
226-
227-
[source,groovy]
228-
----
229-
compile "com.datastax.oss.quarkus:cassandra-quarkus-mapper-runtime:${cassandra-quarkus.version}"
230-
----
231-
232225
IMPORTANT: Although this module is called "runtime", it must be declared in compile scope.
233226

234227
If your project is correctly set up, you should now be able to compile it without errors, and you
235228
should see the generated code in the `target/generated-sources/annotations` directory (if you are
236-
using Maven). You don't need to get familiar with the generated code though, as it is mostly
229+
using Maven). It's not required to get familiar with the generated code though, as it is mostly
237230
internal machinery to interact with the database.
238231

239-
== Creating a Service & JSON REST Endpoint
232+
== Creating a service & JSON REST endpoint
240233

241234
Now let's create a `FruitService` that will be the business layer of our application and store/load
242235
the fruits from the Cassandra database.
@@ -333,19 +326,19 @@ public class FruitDto {
333326
}
334327
----
335328

336-
The translation to and from JSON is done automatically by the Quarkus RestEasy extension, which is
337-
included in this guide's pom.xml file. If you want to add it manually to your application, add the
338-
below snippet to your application's ppm.xml file:
329+
The translation to and from JSON is done automatically by the Quarkus RESTEasy Reactive extension,
330+
which is included in this guide's pom.xml file. If you want to add it manually to your application,
331+
add the below snippet to your application's ppm.xml file:
339332

340333
[source,xml]
341334
----
342335
<dependency>
343336
<groupId>io.quarkus</groupId>
344-
<artifactId>quarkus-resteasy</artifactId>
337+
<artifactId>quarkus-resteasy-reactive</artifactId>
345338
</dependency>
346339
<dependency>
347340
<groupId>io.quarkus</groupId>
348-
<artifactId>quarkus-resteasy-jsonb</artifactId>
341+
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
349342
</dependency>
350343
----
351344

@@ -620,20 +613,7 @@ NOTE: The `getAll()` method above returns `Multi`, and the `add()` method return
620613
are the same Mutiny types that we met before; they are automatically recognized by the Quarkus
621614
reactive REST API, so we don't need to convert them into JSON ourselves.
622615

623-
To effectively integrate the reactive logic with the REST API, your application needs to declare a
624-
dependency to the Quarkus RestEasy Reactive extension:
625-
626-
[source,xml]
627-
----
628-
<dependency>
629-
<groupId>io.quarkus</groupId>
630-
<artifactId>quarkus-resteasy-reactive</artifactId>
631-
</dependency>
632-
<dependency>
633-
<groupId>io.quarkus</groupId>
634-
<artifactId>quarkus-resteasy-reactive-jsonb</artifactId>
635-
</dependency>
636-
----
616+
RESTEasy Reactive natively supports the Mutiny reactive types e.g. `Uni` and `Multi`.
637617

638618
This dependency is already included in this guide's pom.xml, but if you are starting a new project
639619
from scratch, make sure to include it.
@@ -727,12 +707,10 @@ framework you plan to use.
727707

728708
=== Enabling Metrics with Micrometer
729709

730-
Micrometer is the recommended metrics framework in Quarkus applications since Quarkus 1.9.
710+
Micrometer is the recommended metrics framework in Quarkus applications.
731711

732712
To enable Micrometer metrics in your application, you need to add the following to your pom.xml.
733713

734-
For Quarkus 1.11+:
735-
736714
[source,xml]
737715
----
738716
<dependency>
@@ -745,24 +723,6 @@ For Quarkus 1.11+:
745723
</dependency>
746724
----
747725

748-
For Quarkus < 1.11:
749-
750-
[source,xml]
751-
----
752-
<dependency>
753-
<groupId>com.datastax.oss</groupId>
754-
<artifactId>java-driver-metrics-micrometer</artifactId>
755-
</dependency>
756-
<dependency>
757-
<groupId>io.quarkus</groupId>
758-
<artifactId>quarkus-micrometer</artifactId>
759-
</dependency>
760-
<dependency>
761-
<groupId>io.micrometer</groupId>
762-
<artifactId>micrometer-registry-prometheus</artifactId>
763-
</dependency>
764-
----
765-
766726
This guide uses Micrometer, so the above dependencies are already included in this guide's pom.xml.
767727

768728
=== Enabling Metrics with MicroProfile Metrics
@@ -784,7 +744,7 @@ Remove any dependency to Micrometer from your pom.xml, then add the following on
784744
=== Enabling Cassandra Metrics
785745

786746
Even when metrics are enabled in your application, the Cassandra client will not report any metrics,
787-
unless you opt-in for this feature. So your next step is to enable Cassandra metrics in your
747+
unless you opt in for this feature. So your next step is to enable Cassandra metrics in your
788748
`application.properties` file.
789749

790750
[source,properties]
@@ -861,17 +821,17 @@ you can run the native executable as follows:
861821

862822
You can then point your browser to `http://localhost:8080/fruits.html` and use your application.
863823

864-
== Eager vs Lazy Initialization
824+
== Choosing between eager and lazy initialization
865825

866826
As explained above, this extension allows you to inject many types of beans:
867827

868828
- A simple bean like `QuarkusCqlSession` or `FruitDao`;
869-
- The asynchronous version of that bean, e.g. `CompletionStage<QuarkusCqlSession>` or
870-
`CompletionStage<FruitDao>;
871-
- The reactive version of that bean, e.g., `Uni<QuarkusCqlSession>` or `Uni<FruitDao>`.
829+
- The asynchronous version of that bean, for example `CompletionStage<QuarkusCqlSession>` or
830+
`CompletionStage<FruitDao>;
831+
- The reactive version of that bean, for example `Uni<QuarkusCqlSession>` or `Uni<FruitDao>`.
872832

873833
The most straightforward approach is obviously to inject the bean directly. This should work just
874-
fine for most applications; however, the `QuarkusCqlSession` bean, and all DAO beans that depend on
834+
fine for most applications. However, the `QuarkusCqlSession` bean, and all DAO beans that depend on
875835
it, might take some time to initialize before they can be used for the first time, and this process
876836
is blocking.
877837

@@ -884,21 +844,21 @@ that needs to interact with the Cassandra database.
884844

885845
Using lazy initialization speeds up your application startup time, and avoids startup failures if
886846
the Cassandra database is not available. However, it could also prove dangerous if your code is
887-
fully asynchronous, e.g. if you are using https://quarkus.io/guides/reactive-routes[reactive
888-
routes]: indeed, the lazy initialization could accidentally happen on a thread that is not allowed
847+
fully non-blocking, for example if it uses https://quarkus.io/guides/reactive-routes[reactive
848+
routes]. Indeed, the lazy initialization could accidentally happen on a thread that is not allowed
889849
to block, such as a Vert.x event loop thread. Therefore, setting `quarkus.cassandra.init.eager-init`
890850
to `false` and injecting `QuarkusCqlSession` should be avoided in these contexts.
891851

892-
If you want to use Vert.x (or any other reactive framework) and keep the lazy initialization
893-
behavior, you should instead inject only `CompletionStage<QuarkusCqlSession>` or
894-
`Uni<QuarkusCqlSession>`. When injecting these beans, the initialization process will be triggered
895-
lazily, but it will happen in the background, in a non-blocking way, leveraging the Vert.x event
896-
loop. This way you don't risk blocking the Vert.x thread.
852+
If you want to use Vert.x (or any other non-blocking framework) and keep the lazy initialization
853+
behavior, you should instead inject only a `CompletionStage` or a `Uni` of the desired bean. When
854+
injecting these beans, the initialization process will be triggered lazily, but it will happen in
855+
the background, in a non-blocking way, leveraging the Vert.x event loop. This way you don't risk
856+
blocking the Vert.x thread.
897857

898858
Alternatively, you can set `quarkus.cassandra.init.eager-init` to true: in this case the session
899-
bean will be initialized eagerly during application startup, on the Quarkus main thread. This would
900-
eliminate any risk of blocking a Vert.x thread, at the cost of making your startup time (much)
901-
longer.
859+
bean and all DAO beans will be initialized eagerly during application startup, on the Quarkus main
860+
thread. This would eliminate any risk of blocking a Vert.x thread, at the cost of making your
861+
startup time (much) longer.
902862

903863
== Conclusion
904864

quickstart/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
</dependency>
8181
<dependency>
8282
<groupId>io.quarkus</groupId>
83-
<artifactId>quarkus-resteasy-reactive-jsonb</artifactId>
83+
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
8484
</dependency>
8585
<dependency>
8686
<groupId>io.quarkus</groupId>

0 commit comments

Comments
 (0)