Reactive Messaging Examples for Quarkus
Quarkus provides different reactive messaging capabilities. Cloud-native-starter uses some of these capabilities in this sample application!
Join the DZone community and get the full member experience.Join For Free
quarkus provides several different reactive messaging capabilities. the open-source project cloud-native-starter uses some of these capabilities in a sample application which is described in this article.
there a several easy to follow quarkus guides about the topic 'reactive messaging'. tutorials are great, but the best way to learn new technologies is for me is to use them in simple applications. that's why i've come up with a simple scenario.
you may also like: how to write reactive applications with microprofile
the sample comes with a web application that displays links to articles with author information in a simple web application. the web application invokes the web-api service which implements a backend-for-frontend pattern and invokes the articles and authors microservices. the articles service store data in a postgres database . messages are sent between the microservices via kafka. this diagram describes the high-level architecture:
one benefit of reactive models is the ability to update web applications by sending messages, rather than pulling for updates. this is more efficient and improves the user experience.
the following video shows how articles can be created via the rest api. the web application receives notifications and adds new articles to the page.
the next diagram explains the flow which i'll go through in more detail in this article.
- the 'submission' api client invokes a rest endpoint of the 'articles' service to create a new article.
- after the article has been created, a message is sent to kafka.
- the 'web-api' service has subscribed to kafka messages so that a listener is invoked.
- when new articles are created, events are streamed to the web-app.
let's take a closer look at the used technologies.
sending in-memory messages via vert.x event bus
the 'articles' and the 'web-api' service have been implemented in java with quarkus. in both cases, i have used a clean architecture approach where the code of the microservice is organized into three packages. these packages are rather independent of each other and could be exchanged with other implementations.
- api : contains the rest endpoints and handles incoming and outgoing messages.
- business : contains the business logic of the microservice and business entities.
- data : contains the code to access databases or other microservices.
after a new article has been stored in the postgres database, a message is sent to kafka. this is triggered by the business logic, but the actual code resides in the api layer. that's why the business layer needs to send the message to the api layer first.
quarkus provides a mechanism for beans to interact via asynchronous messages by enforcing loose-coupling. check out the guide asynchronous messaging between beans . this functionality is provided via eclipse vert.x which comes with quarkus.
here is the code that sends the event in memory to the api layer:
in the api layer the event can be consumed (see code ):
eclipse microprofile supports another mechanism for memory messages. the reason why i didn't use it, in this case, was that i didn't get it to work. for me, the @outgoing annotation only worked on methods that are either triggered by an incoming event or by the platform (e.g. @postconstruct). in my case, i have to trigger this functionality from business logic. i'm not sure whether this is a missing feature in mircoprofile, a defect or user error. i'm trying to find this out.
the documentation mentions another reason when you should use the vert.x event bus: "the asynchronous message passing feature allows replying to messages which are not supported by microprofile reactive messaging. however, it is limited to single-event behavior (no stream) and local messages."
sending kafka messages via kafka api
next, the api layer of the 'web-api' service needs to send the message to kafka. to set up kafka in kubernetes, follow the instructions from my previous article accessing apache kafka from quarkus .
eclipse microprofile reactive messaging provides the same @outgoing annotation to do this, but i couldn't get it to work since i have to trigger this functionality manually. i want to find out the reason for this as well.
as a workaround, i used the kafka api instead. the usage is pretty straight forward. unfortunately, it looks like the configuration is not read from the same 'application.properties' file which microprofile uses. instead, i had to do this in the code :
next, the 'web-api' service needs to receive this message from kafka. this part can be implemented very easily with microprofile reactive messaging via the annotation @incoming. here is the code :
in the next step notifications about new articles need to be streamed to the web application. to do this, the event is forwarded to the streaming endpoint via the microprofile annotations @outgoing and @broadcast.
check out the guide using apache kafka with reactive messaging for more information about these annotations. from a developer experience, this is as easy as it can get. i like especially that the same annotations can be used for kafka channels as well as in-memory messaging.
sending events to web applications via server-sent events
the last step is to stream the events to web applications. this is done via server-sent events and with quarkus very easy to implement. the streaming endpoint receives the messages via @channel and forwards them via @produces(mediatype.server_sent_events) and @sseelementtype (see code ):
in the web application, the events can be consumed via eventsource. in my case, i only send the id of the article and refresh the list of displayed articles (see code ). alternatively, i also could send the complete article information in the event.
if you want to learn more about reactive programming and reactive messaging, try out the code yourself.
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.