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)
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

