# Exercise 2 - Reactive Messaging with MicroProfile

In this lab you'll learn how to use reactive messaging with [MicroProfile Messaging](https://download.eclipse.org/microprofile/microprofile-reactive-messaging-1.0/microprofile-reactive-messaging-spec.html). With simple Java annotations messages can be sent and received in memory as well as via [Apache Kafka](https://kafka.apache.org/).

MicroProfile Messaging implements the [Reactive Streams](https://www.reactive-streams.org/) 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 [Subscriber, Publisher and Processor](https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html) 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.

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F35d7a2a6f193128da1947510e6d724bb9baf73f1.png?generation=1598945289351298\&alt=media)

## 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.

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

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F389a6f1498e97ab4613cd9efaf2028a644ba01dc.png?generation=1598945289240792\&alt=media)

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.

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

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F4cf3678cbb01eab6dadf8f2e711cf9621f3b5058.png?generation=1598945288773904\&alt=media)

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

Confirm that the changes have been saved.

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

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F354c15ab644ba4e840f0da333428582036878b7f.png?generation=1598945290004548\&alt=media)

## 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.

```bash
cd ~/cloud-native-starter/reactive/web-api-reactive/src/main/resources
cat application.properties
```

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F2332c672214e2ee856cff831fa28b44d3df91c88.png?generation=1598945289138979\&alt=media)

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

## Step 3: Deploy new Version

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

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2Fa64f288b61614eba20294aa8375fe8eeb2d8ad39.png?generation=1591086434486572\&alt=media)

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

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2Fb56c837a1bca2586850440346088662a9caa824f.png?generation=1591086430516116\&alt=media)

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

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2Fd65e23efcee4e0cda6b6450620c7181d0ea9e672.png?generation=1591086431462563\&alt=media)

## 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
```

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F2b018c8d3ba4db412470c1039b68d3159e4941d9.png?generation=1591086461533501\&alt=media)

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

1. Use the following instructions which leverage a terminal
2. Use distributed logging as documented in [Lab 5](https://ibm-developer.gitbook.io/reactive-messaging-with-quarkus-on-openshift/build-new-version-of-the-microservice/exercise-05)

In the terminal get the pod name:

```bash
oc get pods
```

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F46ecf2931b707af9238400401bcd27c6a915c9b3.png?generation=1598945289439444\&alt=media)

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

```bash
oc logs web-api-reactive-xxxxxxxxxxx-xxxxx
```

![](https://2094539152-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8o7TovHRYmD6AT4Wrf%2Fsync%2F2e96a1932770353af398dddcb7167fd32a94d853.png?generation=1598945289840485\&alt=media)

Your added line shows up in the logs now.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ibm-developer.gitbook.io/reactive-messaging-with-quarkus-on-openshift/build-new-version-of-the-microservice/exercise-02.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
