liveBook · Manning

Jakarta Messaging

As explained in Spring in Action, Sixth Edition

JMS messaging is the Java standard programming model for sending and receiving messages through a JMS-compliant message broker.

Jakarta Messaging, commonly called JMS, is a Java standard API for working with message brokers. A broker relays messages between producers and consumers, allowing applications to communicate asynchronously. JMS provides a common interface across compliant brokers instead of tying messaging code to a proprietary API. section 9.1

Spring’s JmsTemplate supports sending and receiving through queues and topics. Spring also supports message-driven plain Java objects that react to arriving messages. section 9.1

For a Spring Boot application, choose a broker and add its starter dependency. For example, Taco Cloud uses Apache ActiveMQ Artemis; a running Artemis broker is also required. The broker choice has little effect on the messaging code, but it changes connection configuration. section 9.1.1

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-artemis</artifactId>
</dependency>

Add this Maven starter to use Artemis with Spring Boot. section 9.1.1

Spring assumes Artemis is on localhost port 61616 by default. For another deployment, configure the host and port; credentials are optional but recommended for production. section 9.1.1

Table 9.1 Properties for configuring the location and credentials of an Artemis broker (view table figure)

Property

Description

spring.artemis.host

The broker’s host

spring.artemis.port

The broker’s port

spring.artemis.user

The user for accessing the broker (optional)

spring.artemis.password

The password for accessing the broker (optional)

These properties configure the Artemis broker location and credentials. section 9.1.1

A consumer can pull messages with JmsTemplate or use a listener that receives them as they become available. A pull call blocks while waiting for a message, but lets the consumer request another only when ready. A listener avoids that wait in application code, although it can be overburdened if messages arrive too quickly. section 9.1.3

Listing 9.3 Receiving an already-converted TacoOrder object
package tacos.kitchen.messaging.jms;
 
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import tacos.TacoOrder;
import tacos.kitchen.OrderReceiver;
 
@Component
public class JmsOrderReceiver implements OrderReceiver {
 
  private JmsTemplate jms;
 
  public JmsOrderReceiver(JmsTemplate jms) {
    this.jms = jms;
  }
  
  @Override
  public TacoOrder receiveOrder() {
    return (TacoOrder) jms.receiveAndConvert("tacocloud.order.queue");
  }
  
}

This receiver pulls from the order queue. receiveAndConvert() uses a configured message converter to return a domain object rather than a raw message. section 9.1.3

Listing 9.4 An OrderListener component that listens for orders
package tacos.kitchen.messaging.jms.listener;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
 
import tacos.TacoOrder;
import tacos.kitchen.KitchenUI;
 
@Profile("jms-listener")
@Component
public class OrderListener {
  
  private KitchenUI ui;
 
  @Autowired
  public OrderListener(KitchenUI ui) {
    this.ui = ui;
  }
 
  @JmsListener(destination = "tacocloud.order.queue")
  public void receiveOrder(TacoOrder order) {
    ui.displayOrder(order);
  }
  
}

This alternative uses @JmsListener to handle orders as they arrive and passes each order to the kitchen UI. section 9.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains broker setup and two receiving models with Spring examples, while deferring broader JMS detail.
Examples
Uses Taco Cloud orders, an Artemis broker, a queue receiver, and a kitchen listener.
What you can do after
Apply it via Spring Boot and JmsTemplate to configure a broker connection and receive messages by pulling or listening.
How it connects
Connects message brokers, queues, topics, message conversion, and message listeners.

Also explained in

Compare how 2 books explain Jakarta Messaging →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.