liveBook · Manning

@JmsListener

As explained in Spring in Action, Sixth Edition

@JmsListener is a Spring annotation that marks a method for automatic invocation when JMS messages arrive at a destination.

@JmsListener marks a component method as a passive JMS message listener. Spring waits for messages on the named destination and invokes the method automatically, avoiding an explicit receive() call. section 9.1.3

Listening for Taco Cloud orders

A kitchen component can display each incoming order as soon as it arrives:\n\n@Profile("jms-listener")\n@Component\npublic class OrderListener {\n private KitchenUI ui;\n\n @Autowired\n public OrderListener(KitchenUI ui) {\n this.ui = ui;\n }\n\n @JmsListener(destination = "tacocloud.order.queue")\n public void receiveOrder(TacoOrder order) {\n ui.displayOrder(order);\n }\n}\n\nThe destination identifies the queue, while the TacoOrder parameter receives the converted message payload. 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);
  }
  
}

Browse the complete listener component, including dependency injection, the destination declaration, and automatic order handling. section 9.1.3

Unlike the pull model, which calls JmsTemplate.receive() or receiveAndConvert() and can block, a listener reacts when messages arrive. It resembles Spring MVC request mappings: both connect annotated methods to incoming events. Listeners can process multiple messages quickly, although a slower kitchen may need buffering. section 9.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains passive invocation, payload delivery, and push-versus-pull behavior without descending into listener-container internals.
Examples
Uses an OrderListener that receives TacoOrder objects from tacocloud.order.queue and forwards them to KitchenUI.
What it assumes
Introduces the distinction between pull-based receiving and push-based listening while assuming a JMS destination and message payload.
What you can do after
Apply it via Spring to implement a passive JMS consumer that receives converted payloads and invokes application logic.
How it connects
Relates message listeners to JMS messages, JmsTemplate, the pull model, and Spring MVC request mappings.

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.