Quarkus - Using AMQP with Reactive Messaging

This guide demonstrates how your Quarkus application can utilize MicroProfile Reactive Messaging to interact with AMQP.

Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.5.3+

  • A running AMQP 1.0 broker, or Docker Compose to start a development cluster

  • GraalVM installed if you want to run in native mode.

Architecture

In this guide, we are going to generate (random) prices in one component.These prices are written in an AMQP queue (prices).A second component reads from the prices queue and apply some magic conversion to the price.The result is sent to an in-memory stream consumed by a JAX-RS resource.The data is sent to a browser using server-sent events.

Architecture

Solution

We recommend that you follow the instructions in the next sections and create the application step by step.However, you can go right to the completed example.

Clone the Git repository: git clone https://github.com/quarkusio/quarkus-quickstarts.git, or download an archive.

The solution is located in the amqp-quickstart directory.

Creating the Maven Project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=amqp-quickstart \
  4. -Dextensions="amqp"
  5. cd amqp-quickstart

This command generates a Maven project, importing the Reactive Messaging and AMQP connector extensions.

Starting an AMQP broker

Then, we need an AMQP broker.You can follow the instructions from the Apache Artemis web site or create a docker-compose.yaml file with the following content:

  1. # A docker compose file to start an Artemis AMQP broker
  2. # more details on https://github.com/vromero/activemq-artemis-docker.
  3. version: '2'
  4. services:
  5. artemis:
  6. image: vromero/activemq-artemis:2.8.0-alpine
  7. ports:
  8. - "8161:8161"
  9. - "61616:61616"
  10. - "5672:5672"
  11. environment:
  12. ARTEMIS_USERNAME: quarkus
  13. ARTEMIS_PASSWORD: quarkus

Once created, run docker-compose up.

This is a development cluster, do not use in production.

The price generator

Create the src/main/java/org/acme/quarkus/sample/PriceGenerator.java file, with the following content:

  1. package org.acme.quarkus.sample;
  2. import io.reactivex.Flowable;
  3. import org.eclipse.microprofile.reactive.messaging.Outgoing;
  4. import javax.enterprise.context.ApplicationScoped;
  5. import java.util.Random;
  6. import java.util.concurrent.TimeUnit;
  7. /**
  8. * A bean producing random prices every 5 seconds.
  9. * The prices are written to an AMQP queue (prices). The AMQP configuration is specified in the
  10. * application configuration.
  11. */
  12. @ApplicationScoped
  13. public class PriceGenerator {
  14. private Random random = new Random();
  15. @Outgoing("generated-price") (1)
  16. public Flowable<Integer> generate() { (2)
  17. return Flowable.interval(5, TimeUnit.SECONDS)
  18. .map(tick -> random.nextInt(100));
  19. }
  20. }
1Instruct Reactive Messaging to dispatch the items from returned stream to generated-price.
2The method returns a RX Java 2 stream (Flowable) emitting a random price every 5 seconds.

The method returns a Reactive Stream. The generated items are sent to the stream named generated-price.This stream is mapped to an AMQP queue using the application.properties file that we will create soon.

The price converter

The price converter reads the prices from AMQP, and transforms them.Create the src/main/java/org/acme/quarkus/sample/PriceConverter.java file with the following content:

  1. package org.acme.quarkus.sample;
  2. import io.smallrye.reactive.messaging.annotations.Broadcast;
  3. import org.eclipse.microprofile.reactive.messaging.Incoming;
  4. import org.eclipse.microprofile.reactive.messaging.Outgoing;
  5. import javax.enterprise.context.ApplicationScoped;
  6. /**
  7. * A bean consuming data from the "prices" AMQP queue and applying some conversion.
  8. * The result is pushed to the "my-data-stream" stream which is an in-memory stream.
  9. */
  10. @ApplicationScoped
  11. public class PriceConverter {
  12. private static final double CONVERSION_RATE = 0.88;
  13. @Incoming("prices") (1)
  14. @Outgoing("my-data-stream") (2)
  15. @Broadcast (3)
  16. public double process(int priceInUsd) {
  17. return priceInUsd * CONVERSION_RATE;
  18. }
  19. }
1Indicates that the method consumes the items from the prices channel
2Indicates that the objects returned by the method are sent to the my-data-stream channel
3Indicates that the item are dispatched to all subscribers

The process method is called for every AMQP messages from the prices queue (configured in the application configuration).Every result is sent to the my-data-stream in-memory stream.

The price resource

Finally, let’s bind our stream to a JAX-RS resource.Creates the src/main/java/org/acme/quarkus/sample/PriceResource.java file with the following content:

  1. package org.acme.quarkus.sample;
  2. import io.smallrye.reactive.messaging.annotations.Channel;
  3. import org.reactivestreams.Publisher;
  4. import javax.inject.Inject;
  5. import javax.ws.rs.GET;
  6. import javax.ws.rs.Path;
  7. import javax.ws.rs.Produces;
  8. import javax.ws.rs.core.MediaType;
  9. /**
  10. * A simple resource retrieving the "in-memory" "my-data-stream" and sending the items as server-sent events.
  11. */
  12. @Path("/prices")
  13. public class PriceResource {
  14. @Inject
  15. @Channel("my-data-stream") Publisher<Double> prices; (1)
  16. @GET
  17. @Produces(MediaType.TEXT_PLAIN)
  18. public String hello() {
  19. return "hello";
  20. }
  21. @GET
  22. @Path("/stream")
  23. @Produces(MediaType.SERVER_SENT_EVENTS) (2)
  24. public Publisher<Double> stream() { (3)
  25. return prices;
  26. }
  27. }
1Injects the my-data-stream channel using the @Channel qualifier
2Indicates that the content is sent using Server Sent Events
3Returns the stream (Reactive Stream)

Configuring the AMQP connector

We need to configure the AMQP connector. This is done in the application.properties file.The keys are structured as follows:

mp.messaging.[outgoing|incoming].{channel-name}.property=value

The channel-name segment must match the value set in the @Incoming and @Outgoing annotation: generated-price → sink in which we write the prices prices → source in which we read the prices

  1. # Configures the AMQP broker credentials.
  2. amqp-username=quarkus
  3. amqp-password=quarkus
  4. # Configure the AMQP connector to write to the `prices` address
  5. mp.messaging.outgoing.generated-price.connector=smallrye-amqp
  6. mp.messaging.outgoing.generated-price.address=prices
  7. mp.messaging.outgoing.generated-price.durable=true
  8. # Configure the AMQP connector to read from the `prices` queue
  9. mp.messaging.incoming.prices.connector=smallrye-amqp
  10. mp.messaging.incoming.prices.durable=true

More details about this configuration is available in the SmallRye Reactive Messaging AMQP connector documentation.

What about my-data-stream? This is an in-memory stream, not connected to a message broker.

The HTML page

Final touch, the HTML page reading the converted prices using SSE.

Create the src/main/resources/META-INF/resources/prices.html file, with the following content:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Prices</title>
  6. <link rel="stylesheet" type="text/css"
  7. href="https://cdnjs.cloudflare.com/ajax/libs/patternfly/3.24.0/css/patternfly.min.css">
  8. <link rel="stylesheet" type="text/css"
  9. href="https://cdnjs.cloudflare.com/ajax/libs/patternfly/3.24.0/css/patternfly-additions.min.css">
  10. </head>
  11. <body>
  12. <div class="container">
  13. <h2>Last price</h2>
  14. <div class="row">
  15. <p class="col-md-12">The last price is <strong><span id="content">N/A</span>&nbsp;&euro;</strong>.</p>
  16. </div>
  17. </div>
  18. </body>
  19. <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  20. <script>
  21. var source = new EventSource("/prices/stream");
  22. source.onmessage = function (event) {
  23. document.getElementById("content").innerHTML = event.data;
  24. };
  25. </script>
  26. </html>

Nothing spectacular here. On each received price, it updates the page.

Get it running

If you followed the instructions, you should have the AMQP broker running.Then, you just need to run the application using:

  1. ./mvnw compile quarkus:dev

Open http://localhost:8080/prices.html in your browser.

If you started the AMQP broker with docker compose, stop it using CTRL+C followed by docker-compose down.

Running Native

You can build the native executable with:

  1. ./mvnw package -Pnative

Going further

This guide has shown how you can interact with AMQP using Quarkus.It utilizes MicroProfile Reactive Messaging to build data streaming applications.

If you did the Kafka quickstart, you have realized that it’s the same code.The only difference is the connector configuration.

If you want to go further check the documentation of SmallRye Reactive Messaging, the implementation used in Quarkus.