@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
