Reactive Messaging with Quarkus on OpenShift
  • Introduction
  • Setup the IBM Cloud Environment
    • Introduction
    • Access the Cluster
    • Access IBM Cloud Shell
  • Setup the sample application
    • Exercise 1 - Setup via Script
    • YouTube - How to setup the sample application (optional)
  • Build new version of the Microservice
    • Exercise 2 - Reactive Messaging with MicroProfile
    • Exercise 3 - Server Sent Events
    • Exercise 4 - Vert.x Event Bus
    • Exercise 5 (optional) - Use distributed Logging
  • Resources
    • How to setup the reactive sample application on OpenShift
    • Blog posts related to reactive
    • Workshop: Reactive Endpoint with Quarkus on OpenShift
    • Cloud-Native-Starter project
    • Cloud-Native-Starter project reactive
Powered by GitBook
On this page
  • Step 1: Modify Subscriber, Publisher and Processor Class
  • Step 2: Configure Kafka
  • Step 3: Deploy new Version
  • Step 4: Verify the new Version

Was this helpful?

  1. Build new version of the Microservice

Exercise 2 - Reactive Messaging with MicroProfile

PreviousExercise 1 - Setup via ScriptNextExercise 3 - Server Sent Events

Last updated 4 years ago

Was this helpful?

In this lab you'll learn how to use reactive messaging with . With simple Java annotations messages can be sent and received in memory as well as via .

MicroProfile Messaging implements the standard which defines how to do asynchronous stream processing for different programming languages independently from specific libraries.

The interfaces of the main Reactive Streams components have been available since JDK 9. The implementation of these interfaces is provided by MicroProfile.

The 'Articles' service writes messages to Kafka after new articles have been created. In this lab we'll take a look how these messages can be read from the 'Web-API' service.

Step 1: Modify Subscriber, Publisher and Processor Class

Let's take a look at a Java class which receives incomping messages from Kafka and sends outgoing in-memory messages.

Invoke the following command in the Cloud Shell.

cd ~/cloud-native-starter/reactive/web-api-reactive/src/main/java/com/ibm/webapi/apis
nano NewArticleListener.java

The @Incoming annotation indicates that the method consumes the items from the topic 'new-article-created'. The @Outgoing annotation indicates that the objects returned by the method are sent to the stream 'stream-new-article'. @Broadcast indicates that the item are dispatched to all subscribers.

The snippet is a Subscriber as well as a Publisher which means that it is automatically also a Processor which can, for example, convert incoming messages and forward them.

Let's make a simple change and redeploy the microservice by adding this line.

System.out.println("Here you can add process functionality");

Exit the Editor via 'Ctrl-X', 'y' and 'Enter'.

Confirm that the changes have been saved.

cd ~/cloud-native-starter/reactive/web-api-reactive/src/main/java/com/ibm/webapi/apis
cat NewArticleListener.java

Step 2: Configure Kafka

The incoming messages in the snippet above are received from Kafka. The 'Articles' service writes these messages to Kafka after new articles have been created.

In order to subscribe to these Kafka messages in Quarkus, the topic needs to be configured in application.properties.

cd ~/cloud-native-starter/reactive/web-api-reactive/src/main/resources
cat application.properties
cd ~/cloud-native-starter/reactive/web-api-reactive/src/main/java/com/ibm/webapi/apis
cat NewArticleListener.java

Step 3: Deploy new Version

cd ~/cloud-native-starter/reactive/web-api-reactive
oc start-build web-api-reactive --from-dir=.

On the 'Builds' page wait until the new build has been completed.

Once completed, delete the 'Web-API' pod which causes a new pod with the latest image to be started.

Step 4: Verify the new Version

Create a new article by invoking a curl post command. You can get the URL from the script show-urls.

~/cloud-native-starter/reactive/os4-scripts/show-urls.sh

In order to see the logs, you can do two things:

  1. Use the following instructions which leverage a terminal

In the terminal get the pod name:

oc get pods

After this invoke this command to display the logs of the pod.

oc logs web-api-reactive-xxxxxxxxxxx-xxxxx

Your added line shows up in the logs now.

Use distributed logging as documented in

Lab 5
MicroProfile Messaging
Apache Kafka
Reactive Streams
Subscriber, Publisher and Processor