# Exercise 4 - Vert.x Event Bus

The [Vert.x Event Bus](https://vertx.io/docs/vertx-core/java/#event_bus) allows different parts of your application to communicate with each other. Check out the [guide](https://quarkus.io/guides/reactive-messaging) to find out more details.

In this lab you'll learn how to use the bus to communicate in-memory between different layers of the 'Articles' service.

![](/files/-M8o81HIO2DMbhNT4A2C)

The 'Articles' service uses a clean architecture approach. There are three different layers:

* API
* Business
* Data

The API layer contains the implementation of the REST APIs and the external messaging interfaces. The data layer contains the implementation of the persistence and could include calls to other external services. The API layer and the data layer can be easily replaced without changing the business logic.

![](/files/-M8o81HJ10flg5FnhLQ4)

In this lab you'll use the event bus to communicate between the business and the API layers.

## Step 1: Understand the Publisher

Let's take a look at the implementation of [ArticleService](https://github.com/IBM/cloud-native-starter/blob/master/reactive/articles-reactive/src/main/java/com/ibm/articles/business/ArticleService.java) in the business layer.

```
cd ~/cloud-native-starter/reactive/articles-reactive/src/main/java/com/ibm/articles/
cat business/ArticleService.java
```

![](/files/-M8o81HKvTqkmwHqiSW0)

An instance of the bus can be injected via @Inject.

Next let's modify the method 'sendMessageToKafka' slightly by adding a System.out.println.

```
System.out.println("Sending message via Vert.x Event Bus");
```

```
cd ~/cloud-native-starter/reactive/articles-reactive/src/main/java/com/ibm/articles/
nano business/ArticleService.java
```

![](/files/-M8o81HLJTvHdH8Nit-0)

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

## Step 2: Understand the Subscriber

The method 'sendMessageToKafka' in the class [NewArticleCreatedListener.java](https://github.com/IBM/cloud-native-starter/blob/master/reactive/articles-reactive/src/main/java/com/ibm/articles/apis/NewArticleCreatedListener.java) in the API layer is invoked when the messages should be sent to Kafka. This is defined via the annotation @ConsumeEvent.

Let's modify this method slightly as well.

```java
System.out.println("Receiving message via Vert.x Event Bus");
```

```bash
cd ~/cloud-native-starter/reactive/articles-reactive/src/main/java/com/ibm/articles/
nano apis/NewArticleCreatedListener.java
```

![](/files/-M8o81HMfdxLt2dZsUiy)

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

## Step 3: Deploy new Version

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

![](/files/-M8o81HND-4MTQQYQmIp)

Wait until the build has been completed.

![](/files/-M8o81HOXMDGHn9o2xf9)

Delete the articles pod. This will trigger Kubernetes to start a new pod with the latest version of the image.

![](/files/-M8o81HPDK6pD2utqbVu)

## Step 4: Deploy new Version

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

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

![](/files/-M8o81HQg84iSl7Vmh0Z)

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](/reactive-messaging-with-quarkus-on-openshift/build-new-version-of-the-microservice/exercise-05.md)

In the terminal get the pod name:

```bash
oc get pods
```

![](/files/-M8o81HRE5p6mxNCK-hi)

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

```bash
oc logs articles-reactive-xxxxxxxxxxx-xxxxx
```

![](/files/-M8o81HSyEvio2cGwhiI)

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