In this post I’m going to show you how to connect to ActiveMQ Artemis using Spring JMS.
Artemis is the successor of ActiveMQ.
So if you’re starting a new project, give it a try.
Let’s take a closer look.
If you want to learn more about Spring JMS - head on over to the Spring JMS tutorials page.
1. What is Apache ActiveMQ Artemis?
Apache ActiveMQ Artemis a JMS Broker that is based on the HornetQ code base.
Artemis is a separate product to ActiveMQ. At the moment of writing the development team is working toward feature parity between ActiveMQ 5.x and Artemis.
The goal is that Artemis eventually becomes ActiveMQ 6.x.
In this guide, we will create a Hello World example that receives a greeting message from an Artemis JMS broker using Spring JMS, Spring Boot, and Maven.
2. General Project Overview
We will use the following tools/frameworks:
- Spring JMS 5.1
- Spring Boot 2.1
- Artemis 2.6
- Maven 3.6
Our project has the following directory structure:
3. Maven Setup
We build and run our example using Maven. If not already the case, download and install Apache Maven.
Let’s use Spring Initializr to generate our Maven project. Make sure to select JMS (Artemis) as a dependency.
Click Generate Project to generate and download the Spring Boot project template. At the root of the project, you’ll find a pom.xml file which is the XML representation of the Maven project.
To avoid having to manage the version compatibility of the different Spring dependencies, we will inherit the defaults from the
spring-boot-starter-parent parent POM.
You can find back the exact dependency versions in Appendix F of the reference documentation. For Spring Boot 2.1.5 the Artemis dependency is version 2.6.6.
The generated project contains Spring Boot Starters that manage the different Spring dependencies.
spring-boot-starter-artemis dependency includes the needed dependencies for using Spring JMS in combination with Artemis.
spring-boot-starter-test includes the dependencies for testing Spring Boot applications with libraries that include JUnit, Hamcrest and Mockito.
We also add a dependency on
artemis-junit. It provides tools that allow us to have access to an embedded Artemis server when running our unit test.
In the plugins section, you’ll find the Spring Boot Maven Plugin.
spring-boot-maven-plugin allows us to build a single, runnable “uber-jar”. This is a convenient way to execute and transport code.
Also, the plugin allows you to start the example via a Maven command.
The project is for a large part identical to a previous Spring JMS ActiveMQ example. As such we will only detail the changes that are needed to connect to Artemis.
4. Create a Spring JMS Message Producer
We still use an
ActiveMQConnectionFactory but this time it is part of the
We pass a brokerUrl to the constructor as shown below.
The value is specified in the application.yml properties file located under src/main/resources.
5. Create a Spring JMS Message Consumer
Similar to the
SenderConfig, we use the
ActiveMQConnectionFactory from the
And that’s it! We can now test our connection to an Artemis JMS broker.
6. Testing the JMS listener
The artemis-junit package provides some JUnit rules. These make it easy to start a server for our tests.
@Rule annotation to create an
EmbeddedJMSResource that will run an Artemis server.
Then use the
Sender to send a message.
getLatch() on the
Receiver allows us to check if the message was received.
Let’s run the unit test to check if everything is working.
Open a command prompt in the root directory and execute the following Maven command.
In the output logs, we can see that the Hello Spring JMS ActiveMQ! greeting is received.
If you would like to run the above code sample you can get the full source code here.
In this guide, we showed how to use Spring JMS to connect to ActiveMQ Artemis.
Drop a line below if this example was useful.
Or if you have a question.