Quarkus - Using Artemis JMS extension

This guide demonstrates how your Quarkus application can use Artemis JMS messaging.

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 Artemis server, or Docker Compose to start one

  • 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 JMS queue (prices).Another component reads from the prices queue and stores the last price.The data can be fetch from a browser using a fetch button from a JAX-RS resource.

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 jms-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=jms-quickstart \
  4. -Dextensions="artemis-jms"
  5. cd jms-quickstart

This command generates a Maven project, importing the Artemis JMS extension.

Starting an Artemis server

Then, we need an Artemis server.You can follow the instructions from the Apache Artemis web site or via docker:

  1. docker run -it --rm -p 8161:8161 -p 61616:61616 -e ARTEMIS_USERNAME=quarkus -e ARTEMIS_PASSWORD=quarkus vromero/activemq-artemis:2.9.0-alpine

The price producer

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

  1. package org.acme.artemis;
  2. import java.util.Random;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.ScheduledExecutorService;
  5. import java.util.concurrent.TimeUnit;
  6. import javax.enterprise.context.ApplicationScoped;
  7. import javax.enterprise.event.Observes;
  8. import javax.inject.Inject;
  9. import javax.jms.ConnectionFactory;
  10. import javax.jms.JMSContext;
  11. import javax.jms.Session;
  12. import io.quarkus.runtime.ShutdownEvent;
  13. import io.quarkus.runtime.StartupEvent;
  14. /**
  15. * A bean producing random prices every 5 seconds and sending them to the prices JMS queue.
  16. */
  17. @ApplicationScoped
  18. public class PriceProducer implements Runnable {
  19. @Inject
  20. ConnectionFactory connectionFactory;
  21. private final Random random = new Random();
  22. private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  23. void onStart(@Observes StartupEvent ev) {
  24. scheduler.scheduleWithFixedDelay(this, 0L, 5L, TimeUnit.SECONDS);
  25. }
  26. void onStop(@Observes ShutdownEvent ev) {
  27. scheduler.shutdown();
  28. }
  29. @Override
  30. public void run() {
  31. try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
  32. context.createProducer().send(context.createQueue("prices"), Integer.toString(random.nextInt(100)));
  33. }
  34. }
  35. }

The price consumer

The price consumer reads the prices from JMS, and stores the last one.Create the src/main/java/org/acme/quarkus/sample/PriceConsumer.java file with the following content:

  1. package org.acme.artemis;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import javax.enterprise.context.ApplicationScoped;
  5. import javax.enterprise.event.Observes;
  6. import javax.inject.Inject;
  7. import javax.jms.ConnectionFactory;
  8. import javax.jms.JMSConsumer;
  9. import javax.jms.JMSContext;
  10. import javax.jms.JMSException;
  11. import javax.jms.Message;
  12. import javax.jms.Session;
  13. import io.quarkus.runtime.ShutdownEvent;
  14. import io.quarkus.runtime.StartupEvent;
  15. /**
  16. * A bean consuming prices from the JMS queue.
  17. */
  18. @ApplicationScoped
  19. public class PriceConsumer implements Runnable {
  20. @Inject
  21. ConnectionFactory connectionFactory;
  22. private final ExecutorService scheduler = Executors.newSingleThreadExecutor();
  23. private volatile String lastPrice;
  24. public String getLastPrice() {
  25. return lastPrice;
  26. }
  27. void onStart(@Observes StartupEvent ev) {
  28. scheduler.submit(this);
  29. }
  30. void onStop(@Observes ShutdownEvent ev) {
  31. scheduler.shutdown();
  32. }
  33. @Override
  34. public void run() {
  35. try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
  36. JMSConsumer consumer = context.createConsumer(context.createQueue("prices"));
  37. while (true) {
  38. Message message = consumer.receive();
  39. if (message == null) return;
  40. lastPrice = message.getBody(String.class);
  41. }
  42. } catch (JMSException e) {
  43. throw new RuntimeException(e);
  44. }
  45. }
  46. }

The price resource

Finally, let’s create a simple JAX-RS resource to show the last price.Creates the src/main/java/org/acme/quarkus/sample/PriceResource.java file with the following content:

  1. package org.acme.artemis;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. /**
  8. * A simple resource showing the last price.
  9. */
  10. @Path("/prices")
  11. public class PriceResource {
  12. @Inject
  13. PriceConsumer consumer;
  14. @GET
  15. @Path("last")
  16. @Produces(MediaType.TEXT_PLAIN)
  17. public String last() {
  18. return consumer.getLastPrice();
  19. }
  20. }

Configuring the Artemis properties

We need to configure the Artemis connection properties.This is done in the application.properties file.

  1. # Configures the Artemis properties.
  2. quarkus.artemis.url=tcp://localhost:61616
  3. quarkus.artemis.username=quarkus
  4. quarkus.artemis.password=quarkus

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"><button id="fetch">Fetch</button>The last price is <strong><span id="content">N/A</span>&nbsp;&euro;</strong>.</p>
  16. </div>
  17. </div>
  18. </body>
  19. <script>
  20. document.getElementById("fetch").addEventListener("click", function() {
  21. fetch("/prices/last").then(function (response) {
  22. response.text().then(function (text) {
  23. document.getElementById("content").textContent = text;
  24. })
  25. })
  26. })
  27. </script>
  28. </html>

Nothing spectacular here. On each fetch, it updates the page.

Get it running

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

  1. ./mvnw compile quarkus:dev

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

Running Native

You can build the native executable with:

  1. ./mvnw package -Pnative

Configuration Reference

Configuration property fixed at build time - ️ Configuration property overridable at runtime

Configuration propertyTypeDefault
quarkus.artemis.urlArtemis connection urlstringrequired
quarkus.artemis.usernameUsername for authentication, only used with JMSstring
quarkus.artemis.passwordPassword for authentication, only used with JMSstring