Quick Tour for the impatient


This is the five-minute tour to get started with Spring AMQP.

Prerequisites: Install and run the RabbitMQ broker (https://www.rabbitmq.com/download.html). Then grab the spring-rabbit JAR and all its dependencies - the easiest way to do so is to declare a dependency in your build tool. For example, for Maven, you can do something resembling the following:


For Gradle, you can do something resembling the following:

compile 'org.springframework.amqp:spring-rabbit:3.0.1'

The minimum Spring Framework version dependency is 5.2.0.

The minimum amqp-client Java client library version is 5.7.0.

The minimum stream-client Java client library for stream queues is 0.7.0.

Very, Very Quick

This section offers the fastest introduction.

First, add the following import statements to make the examples later in this section work:

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

The following example uses plain, imperative Java to send and receive a message:

ConnectionFactory connectionFactory = new CachingConnectionFactory();
AmqpAdmin admin = new RabbitAdmin(connectionFactory);
admin.declareQueue(new Queue("myqueue"));
AmqpTemplate template = new RabbitTemplate(connectionFactory);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");

Note that there is also a ConnectionFactory in the native Java Rabbit client. We use the Spring abstraction in the preceding code. It caches channels (and optionally connections) for reuse. We rely on the default exchange in the broker (since none is specified in the send), and the default binding of all queues to the default exchange by their name (thus, we can use the queue name as a routing key in the send). Those behaviors are defined in the AMQP specification.

With XML Configuration

The following example is the same as the preceding example but externalizes the resource configuration to XML:

ApplicationContext context =
    new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
<beans xmlns="http://www.springframework.org/schema/beans"

    <rabbit:connection-factory id="connectionFactory"/>

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

    <rabbit:admin connection-factory="connectionFactory"/>

    <rabbit:queue name="myqueue"/>


By default, the <rabbit:admin/> declaration automatically looks for beans of type Queue, Exchange, and Binding and declares them to the broker on behalf of the user. As a result, you need not use that bean explicitly in the simple Java driver. There are plenty of options to configure the properties of the components in the XML schema. You can use auto-complete features of your XML editor to explore them and look at their documentation.

With Java Configuration

The following example repeats the same example as the preceding example but with the external configuration defined in Java:

ApplicationContext context =
    new AnnotationConfigApplicationContext(RabbitConfiguration.class);
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");


public class RabbitConfiguration {

    public CachingConnectionFactory connectionFactory() {
        return new CachingConnectionFactory("localhost");

    public RabbitAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());

    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());

    public Queue myQueue() {
       return new Queue("myqueue");
With Spring Boot Auto Configuration and an Async POJO Listener

Spring Boot automatically configures the infrastructure beans, as the following example shows:

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    public ApplicationRunner runner(AmqpTemplate template) {
        return args -> template.convertAndSend("myqueue", "foo");

    public Queue myQueue() {
        return new Queue("myqueue");

    @RabbitListener(queues = "myqueue")
    public void listen(String in) {