liveBook · Manning

@RabbitListener

As explained in Spring in Action, Sixth Edition

@RabbitListener is a Spring AMQP annotation that marks a bean method for invocation when messages arrive in a RabbitMQ queue.

@RabbitListener marks a bean method to receive pushed messages from a RabbitMQ queue. Unlike RabbitTemplate.receive(), it lets the framework invoke the method when a message arrives. section 9.2.3

Display incoming orders

A component can listen to the order queue and pass each converted TacoOrder to a user interface: @Component public class OrderListener { private KitchenUI ui; @Autowired public OrderListener(KitchenUI ui) { this.ui = ui; } @RabbitListener(queues = "tacocloud.order.queue") public void receiveOrder(TacoOrder order) { ui.displayOrder(order); } } section 9.2.3

Listing 9.7 Declaring a method as a RabbitMQ message listener
package tacos.kitchen.messaging.rabbit.listener;
 
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tacos.TacoOrder;
import tacos.kitchen.KitchenUI;
 
@Component
public class OrderListener {
  
  private KitchenUI ui;
 
  @Autowired
  public OrderListener(KitchenUI ui) {
    this.ui = ui;
  }
 
  @RabbitListener(queues = "tacocloud.order.queue")
  public void receiveOrder(TacoOrder order) {
    ui.displayOrder(order);
  }
  
}

Browse the complete listener component, including dependency injection, queue selection, and order handling. section 9.2.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows the annotation and a working listener, while deferring listener internals and configuration details.
Examples
Uses an OrderListener that consumes TacoOrder messages from tacocloud.order.queue and displays them through KitchenUI.
What you can do after
Apply via Spring AMQP by annotating a bean method with a queue name and handling incoming domain objects.
How it connects
Connects RabbitMQ queues with Spring components, TacoOrder conversion, RabbitTemplate-based receiving, and JMS-style message consumption.

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.