Apache Kafka with Reactive Streams

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 Apache Kafka.

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.

Add the Reactive Messaging Kafka extension

Just open a new terminal window, and make sure you’re at the root of your tutorial-app project, then run:

  • Maven

  • Quarkus CLI

./mvnw quarkus:add-extension -Dextensions=quarkus-smallrye-reactive-messaging-kafka
quarkus extension add quarkus-smallrye-reactive-messaging-kafka

Modify BeerProcessor

In the BeerProcessor Java class in src/main/java in the org.acme package should have the print method commented as it’s not necessary anymore because the content of the messages channel will be send to a Kafka topic:

/**@Incoming("messages")
public void print(JsonObject beer) {
    System.out.println(JsonbBuilder.create().toJson(beer));
}**/

Add the Reactive Messaging Kafka properties

Add the following properties to your application.properties in src/main/resources to configure messages channel to be backed by a Kafka topic instead of a memory channel:

mp.messaging.outgoing.messages.connector=smallrye-kafka (1)
mp.messaging.outgoing.messages.topic=pricedbeers (2)
1 messages channel is backed to Kafka
2 messages channel sends events to pricedbeers topic
If all channels are backed to Kafka, it’s not necessary to set the connector property.
If the channel name is the same as the topic, it’s not necessary to set the topic property.

Dev Services for Kafka

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!

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.

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

Invoke the endpoint

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:

curl localhost:8080/beer/emit/1

Now, nothing is shown as return or in the Quarkus terminal, because the event is sent to a Kafka topic.

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