A Quarkus dojo to experiment MicroProfile reactive messaging with Kafka
First, start the Kafka broker using the provided docker compose:
docker-compose up
Then, you will develop an application with three components:
- order-api: A REST API to manage orders, each time an order is created it will send a Kafka message with the new order.
- order-enhancer: A messaging component that listens to order messages and enhance them with user information.
- search-api: A REST API to search for enhanced orders. It listens to enhanced order messages.
Before going to the various steps of the dojo, please take some time to read the following documentations:
You can refer to the Quarkus USING APACHE KAFKA WITH REACTIVE MESSAGING guide at any time.
For this step, you can refer to the Sending / Receiving messages from non-reactive code documentation.
- Go to the order-api and open the
fr.loicmathieu.dojo.quarkus.messaging.order.api.OrderResource
REST endpoint. - Define an
Emitter<Order>
configured with theorders
. This channel is already configured in theapplication.properties
. - Update the
create(Order order)
method to use this emitter to send the Order to the broker.
Start the application with mvn quarkus:dev
.
You can send orders to the endpoint with this curl:
curl -XPOST -d '{"nbItem":2, "totalPrice": 200, "userId": "user1", "shippingAddress": "1 rue du Paradis à Lille"}' \
-H "Content-Type: application/json" \
http://localhost:8080
NOTE: The user service of the order-enhancer
is able to handle the users with id "user1", "user2" and "user3".
- Go to the order-enhancer and open the
fr.loicmathieu.dojo.quarkus.messaging.order.enhancer.MessageManager
. - Create a method that takes an
Order
as parameter and return theEnhancedOrder
enhanced with the User information recovered from theUserService
. - Annotate this method so it listens to the
orders
channel and send the enhanced order to theenhanced-orders
channel. These channels are already configured in theapplication.properties
. You can read the section The price converter of the Quarkus Kafka guide for an exemple. - Add some logging to see if the message arrives.
Start the application with mvn quarkus:dev
and create orders on the order API.
It’s not rare to have to test your application but deploying the infrastructure can be cumbersome. While Docker or Test Containers have improved the testing experience, you may want to mock this infrastructure.
- Open the test class
fr.loicmathieu.dojo.quarkus.messaging.order.enhancer.MessageManagerTest
, run it, it should fail. - Open the class
fr.loicmathieu.dojo.quarkus.messaging.order.enhancer.InMemoryChannelTestResource
and mock the channels. Check the SmallRye Reactive Messaging Testing section for an example.
- Go to the search-api and open the
fr.loicmathieu.dojo.quarkus.messaging.search.MessageManager
. - Create a method that takes an
EnhancedOrder
as parameter and save it via theEnhancedOrderService.add()
method. - Annotate this method so it listens to the
enhanced-orders
channel. This channel is already configured in theapplication.properties
.
Start the application with ̀mvn quarkus:dev
.
After creating a new order via the order API, you should have this order enhanced, then available via the search API.
You can test the search endpoint via:
curl http://localhost:8081