How do I send and receive JMS messages with Spring Boot?

When an application creates an order, it may also need to arrange shipping or send a confirmation email. A queue lets the application hand off that work. The application sends a message containing the order ID, and a consumer processes it asynchronously.

In this first article, you’ll send a message to the orders.created queue with JmsTemplate and receive it with @JmsListener. The example uses Java 25, Maven, Spring Boot 4.1.2, and Apache ActiveMQ Artemis.

What is JMS?

Java Message Service (JMS), now called Jakarta Messaging, defines a standard Java API for sending and receiving messages. Modern Spring applications use the jakarta.jms package. In this example, ActiveMQ Artemis is the message broker that manages the queue and delivers messages.

Component Role
Producer Sends a message announcing that order ORD-1001 was created.
Broker Accepts the message and delivers it to a consumer.
Queue Holds messages for delivery. If multiple consumers listen to the same queue, each delivery goes to one consumer, allowing them to share the work.
Consumer Receives the message from the queue and logs its contents.

Spring Boot configures the connection to Artemis and sets up the Spring JMS infrastructure. For this tutorial, Artemis runs as an embedded broker in the same Java process as the producer and consumer. You only need JDK 25 and Maven. Broker persistence is disabled, so any messages still in the queue are lost when the broker stops.

1. Create a Maven project

Save this configuration as pom.xml in the project directory.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.1.2</version>
        <relativePath/>
    </parent>

    <groupId>org.kodejava</groupId>
    <artifactId>spring-jms-week-01</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <java.version>25</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-artemis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>artemis-jakarta-server</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2. Configure the broker and queue

Create src/main/resources/application.properties.

spring.artemis.mode=embedded
spring.artemis.embedded.persistent=false
spring.artemis.embedded.queues=orders.created

The last property tells Spring Boot to create the orders.created queue when the embedded broker starts.

3. Send a message at startup

Create src/main/java/org/kodejava/jms/OrderMessagingApplication.java.

package org.kodejava.jms;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.core.JmsTemplate;

@SpringBootApplication
public class OrderMessagingApplication {
    static final String ORDERS_CREATED = "orders.created";

    public static void main(String[] args) {
        SpringApplication.run(OrderMessagingApplication.class, args);
    }

    @Bean
    CommandLineRunner sendOrderCreated(JmsTemplate jmsTemplate) {
        return args -> jmsTemplate.convertAndSend(ORDERS_CREATED, "ORD-1001");
    }
}

The CommandLineRunner sends one message each time the application starts. The first argument to convertAndSend() is the destination name, and the second is the payload. The default message converter turns the Java String into a JMS TextMessage.

4. Receive the message from the queue

Create src/main/java/org/kodejava/jms/OrderCreatedListener.java.

package org.kodejava.jms;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class OrderCreatedListener {
    private static final Logger log =
            LoggerFactory.getLogger(OrderCreatedListener.class);

    @JmsListener(destination = OrderMessagingApplication.ORDERS_CREATED)
    public void onOrderCreated(String orderId) {
        log.info("Order created - orderId={}", orderId);
    }
}

Spring registers the method annotated with @JmsListener with a listener container. When Artemis delivers a message from the queue, Spring extracts the text from the TextMessage and passes it to the method as the orderId argument. The listener processes the message asynchronously.

5. Run the application and check the output

Run this command from the directory containing pom.xml.

mvn spring-boot:run

Look for this message in the application log.

Order created - orderId=ORD-1001

The listener writes this log entry after receiving the message, so it confirms that the message traveled through the broker and reached the consumer. Press Ctrl+C to stop the application.

In the next article, we’ll run Artemis in Docker and connect to it over the network. We’ll reuse the producer, the consumer, and the orders.created queue name.

References

Maven Artifacts

Artifact Purpose
org.springframework.boot:spring-boot-starter-parent:4.1.2 Provides the parent POM and manages dependency versions.
org.springframework.boot:spring-boot-starter-artemis Adds Spring JMS integration and the Artemis client.
org.apache.activemq:artemis-jakarta-server Provides the embedded Artemis broker.
org.springframework.boot:spring-boot-maven-plugin Runs the application with mvn spring-boot:run.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.