Skip to content

Commit c231bd9

Browse files
authored
Merge pull request #163 from redhat-developer-demos/reactive
Reactive
2 parents b3d5cce + b658160 commit c231bd9

3 files changed

Lines changed: 47 additions & 22 deletions

File tree

documentation/modules/ROOT/pages/14_reactive.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ cd {project-name}
3636
--
3737
====
3838

39+
== Start the app in Dev Mode:
40+
41+
[tabs%sync]
42+
====
43+
Maven::
44+
+
45+
--
46+
[.console-input]
47+
[source,bash,subs="+macros,+attributes"]
48+
----
49+
./mvnw quarkus:dev
50+
----
51+
52+
--
53+
Quarkus CLI::
54+
+
55+
--
56+
[.console-input]
57+
[source,bash,subs="+macros,+attributes"]
58+
----
59+
quarkus dev
60+
----
61+
--
62+
====
63+
64+
3965
== Create Beer POJO
4066

4167
Create a new `Beer` Java class in `src/main/java` in the `com.redhat.developers` package with the following contents:
@@ -117,6 +143,8 @@ public interface BeerService {
117143

118144
== Configure REST Client properties
119145

146+
NOTE: The original punkapi.com service is not operational anymore. To reproduce the application, you can deploy the API on an Openshift Sandbox (developers.redhat.com/sandbox). Once you have logged in to the Sandbox, go to the "Developers" perspective in the top left corner, click on +Add in the left menu, and then in the "Git Repository" section click on the "Import from Git". Paste in the Git Repo URL field: "https://github.com/kdubois/punkapi-server.git", scroll down and set "Target port" to `3333`. Wait for the build to complete (this may take a few minutes) and the app to be deployed. Now click on the deployed app circle in the Topology view and copy the URL from the "Routes" section which should be visible on the right side of your screen. Then paste this URL in the application.properties below:
147+
120148
Add the following properties to your `application.properties` in `src/main/resources`:
121149

122150
[.console-input]

documentation/modules/ROOT/pages/15_reactive-messaging.adoc

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
= Reactive Messaging
22

3-
As a Java developer you are aware that the JMS is the standard when comes to working with messages.
4-
JMS is a blocking API which blocks us from implementing the reactive principles.
3+
As a Java developer you're likely familiar with JMS, which is considered to be the standard when it comes to working with messages.
4+
JMS however is a blocking API, which prevents us from implementing the reactive principles.
55

6-
Quarkus has SmallRye Reactive Messaging which is an implementation of the Eclipse MicroProfile Reactive Messaging specification.
7-
Quarkus implements version 2.x of this specification but also provides many other extensions.
6+
Quarkus has a "SmallRye Reactive Messaging" extension which is an implementation of the Eclipse MicroProfile Reactive Messaging specification. Reactive Messaging allows us to implement messaging in a non-blocking, reactive way.
87

9-
In this chapter we're going to use SmallRye Reactive Messaging to generate beers having a price but using messages instead of synchronous calls.
8+
In this chapter we're going to use SmallRye Reactive Messaging to generate beers once again. This time we're going to add a (random) price to the beers. We're going to be using messages instead of synchronous calls to do this.
109

11-
At this point, we're going to use an inmemory channel which means that messages are sent through the same application using the memory as transport channel of the messages.
10+
To do so, we're going to use an in-memory channel. This means that messages are sent through the application using memory as transport channel of the messages.
1211

1312
In the following section, we'll see what we need to change to start using an external broker for sending messages.
1413

1514
== Add the Reactive Messaging extension
1615

17-
Just open a new terminal window, and make sure you’re at the root of your `{project-name}` project, then run:
16+
Open a new terminal window, and make sure you’re at the root of your `{project-name}` project, then run:
1817

1918
[tabs]
2019
====
@@ -41,9 +40,9 @@ quarkus extension add messaging
4140

4241
== Modify BeerResource
4342

44-
Let's create a new endpoint that finds a beer and sends/emits a message to `beers` channel with the beer.
43+
Let's create a new endpoint that finds a beer and sends/emits a message to a `beers` channel.
4544

46-
Open `BeerResource` class and add the following code.
45+
Open the `BeerResource` class and add the following code.
4746

4847
In the imports section:
4948

@@ -79,9 +78,9 @@ public Response emitBeer(@PathParam("beer") int beerId) {
7978
<4> Sends an ack to caller
8079

8180
The previous code sends the beer as a `JsonObject` to `beers` channel.
82-
Since we are using a memory channel, let's create a new Java class in the same project capturing the messages sent to the channel.
81+
Since we are using an in-memory channel, let's create a new Java class in the same project capturing the messages sent to the channel.
8382

84-
Moreover this new class, will send another event to a different channel which will be captured by another method.
83+
This new class will send another event to a different channel which will be captured by yet another method.
8584

8685
== Create BeerProcessor
8786

@@ -90,7 +89,7 @@ Create a new `BeerProcessor` Java class in `src/main/java` in the `org.acme` pac
9089
[.console-input]
9190
[source,java]
9291
----
93-
package org.acme;
92+
package com.redhat.developers;
9493
9594
import java.util.concurrent.ThreadLocalRandom;
9695
@@ -109,7 +108,7 @@ public class BeerProcessor {
109108
110109
@Incoming("beers") // <1>
111110
@Outgoing("messages") // <2>
112-
public JsonObject processPrize(JsonObject beer) { // <3>
111+
public JsonObject processPrice(JsonObject beer) { // <3>
113112
JsonObjectBuilder beerWithPrice = Json.createObjectBuilder(beer).add("price", getPrice());
114113
return beerWithPrice.build(); // <4>
115114
}
@@ -124,7 +123,7 @@ public class BeerProcessor {
124123
}
125124
}
126125
----
127-
<1> Listen events from `beers` channel
126+
<1> Listen to events from `beers` channel
128127
<2> Sends/Emits the result of the method call to the `messages` channel
129128
<3> Argument is the message of the `beers` channel
130129
<4> Return object is sent to the `messages` channel

documentation/modules/ROOT/pages/16_kafka-and-streams.adoc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
= Apache Kafka with Reactive Streams
22

3-
Mutiny is just part of the Reactive story. To complement it, we need Reactive Streams too. And an important service that can serve as the underlying implementation for our stream is http://kafka.apache.org[Apache Kafka,window=_blank].
3+
Mutiny is just one part of the Reactive story. To complement it, we could use Reactive Streams too. An important service that can serve as the underlying implementation for our stream is http://kafka.apache.org[Apache Kafka,window=_blank].
44

5-
In this chapter, we'll do a small change, we send beers with a price to a Kafka broker instead of using a memory channel.
5+
In this chapter, we'll make a small change: We will send beers with a price to a Kafka broker instead of using an in-memory channel.
66

77
== Add the Reactive Messaging Kafka extension
88

9-
Just open a new terminal window, and make sure you’re at the root of your `{project-name}` project, then run:
9+
Open a new terminal window, and make sure you’re at the root of your `{project-name}` project, then run:
1010

1111
[tabs]
1212
====
@@ -65,13 +65,13 @@ TIP: If the channel name is the same as the topic, it's not necessary to set the
6565

6666
Because starting a Kafka broker can be long and you need to develop fast in your local environment, Dev Services for Kafka is here to help you!
6767

68-
Since `quarkus-smallrye-reactive-messaging-kafka` extension is present, Dev Services for Kafka automatically starts a Kafka broker in dev mode and when running tests.
68+
Since we have added the `quarkus-messaging-kafka`, Quarkus Dev Services automatically starts a containerized Kafka broker in dev mode and when running tests.
6969

7070
TIP: You can disable Dev Services for Kafka by adding `quarkus.kafka.devservices.enabled=false` or configuring `kafka.bootstrap.servers` in `application.properties`.
7171

7272
== Invoke the endpoint
7373

74-
With all these changes done, having Docker/Podman running in your computer, and starting the service in dev mode, you can send the same request as in the previous chapter:
74+
There's not really any code to add at this point. Just by having Docker/Podman running on our computer, and starting the service in dev mode, we can now send the same request as in the previous chapter, but it will be sent to a Kafka topic instead of an in-memory channel. Let's try it:
7575

7676
[.console-input]
7777
[source,bash]
@@ -80,6 +80,4 @@ curl -w '\n' localhost:8080/beer/emit/1
8080
----
8181

8282

83-
Now, nothing is shown as return or in the Quarkus terminal, because the event is sent to a Kafka topic.
84-
85-
To check the content of the topic, we can use the Dev UI interfac by pointing your browser to http://localhost:8080/q/dev-ui/io.quarkus.quarkus-kafka-client/topics[window=_blank]
83+
As you can see, nothing is shown in the return message, nor is there anything in the Quarkus terminal, because the event is simply sent to a Kafka topic. We could create some additional code to retrieve the message from Kafka, but in this case we're going to use the Dev UI interface where we can actually find the contents of the Kafka topic in the Kafka by pointing our browser to http://localhost:8080/q/dev-ui/io.quarkus.quarkus-kafka-client/topics[window=_blank]

0 commit comments

Comments
 (0)