Quick Spring AMQP Tutorial

  • Post author:
  • Post category:Java

Here’s a quick tutorial on how to get up and running with Spring AMQP 1.0.0.RELEASE. You will need to be familiar with Maven and m2e to follow the steps below.

1. Since we are going to create and run a Spring AMQP application, we need an AMQP implementation. For this tutorial, we will use RabbitMQ. Install RabbitMQ. Since RabbitMQ is written in Erlang, you will need to install Erlang as well. Run RabbitMQ and you should see the following.

Step 1, Figure 1

2. Create a new Maven in Eclipse using m2e with the maven-archetype-quickstart archetype.
Step 2, Figure 1

Step 2, Figure 2

3. Add the spring-context, spring-amqp, and spring-rabbit dependencies.

Step 3, Figure 1
Step 3, Figure 2

4. Create the Producer class.

package com.teamextension.quickamqp;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Producer {
	public static void main(String[] args) {
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
		AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
		amqpTemplate.convertAndSend("helloworld.queue", "Hello World");
	}
}

5. Create the Consumer class.

package com.teamextension.quickamqp;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Consumer {
	public static void main(String[] args) {
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
		AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
		System.out.println(amqpTemplate.receiveAndConvert("helloworld.queue"));
	}
}

6. Create the Spring applicationContext.xml.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/rabbit
		http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- A reference to the org.springframework.amqp.rabbit.connection.ConnectionFactory -->
	<rabbit:connection-factory id="connectionFactory"/>

	<!-- Creates a org.springframework.amqp.rabbit.core.RabbitTemplate for access to the broker -->
	<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

	<!-- Creates a org.springframework.amqp.rabbit.core.RabbitAdmin	to manage exchanges, queues and bindings -->
	<rabbit:admin connection-factory="connectionFactory"/>

	<!-- Creates a queue for consumers to retrieve messages -->
	<rabbit:queue name="helloworld.queue"/>
</beans>

5. Run the Producer, then the Consumer. You should see Hello World printed out

Step 5, Figure 1

For more information, visit the Spring AMQP site. Spring AMQP examples can be found at GitHub.