Spring 4 + RabbitMQ Integration Annotation Example

By Arvind Rai, December 20, 2014
On this page we will integrate Spring 4 and RabbitMQ using annotation. We need to install RabbitMQ AMQP server for the integration. RabbitMQ is an open source messaging service. Spring has implemented RabbitTemplate, Queue, TopicExchange and Binding classes to support RabbitMQ messaging. Find the example step by step.

Software Required to Run Example

To run the example we need the following software.
1. JDK 6
2. Gradle
3. Eclipse
4. RabbitMQ
5. Erlang

Start with Erlang and RabbitMQ Installation

Before integration of spring and RabbitMQ, go to RabbitMQ installation site installation site . To install on windows, follow the link . To run RabbitMQ server, we need Erlang. So install Erlang first then RabbitMQ. To start the server , go to \rabbitmq_server-3.4.2\sbin using command prompt and run rabbitmq-server file.

Project Structure in Eclipse

To start with RabbitMQ, it will be helpful to first check project structure in eclipse.
Spring 4 + RabbitMQ Integration Annotation  Example

Gradle build file to Resolve JAR Dependency

Find the gradle to resolve the Spring and RabbitMQ JAR dependency.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'Concretepage'
version = '1.0-SNAPSHOT' 
repositories {
    maven { url "https://repo.spring.io/libs-release" }
    mavenLocal()
    mavenCentral()
}
dependencies {
    compile  'org.springframework.boot:spring-boot-starter-amqp:1.2.0.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-security:1.2.0.RELEASE'
    compile 'org.springframework.data:spring-data-commons:1.9.1.RELEASE'
} 

Create a Message Receiver Class

Find the receiver class that will receive the message. The method implemented to receive message should be acknowledged to MessageListenerAdapter in configuration class.
MessageReceiver.java
package com.concretepage.rabbitmq;
import java.util.concurrent.CountDownLatch;
public class MessageReceiver {
	private CountDownLatch countDownLatch = new CountDownLatch(1);
	public void receiveMsg(String message) {
		System.out.println("Message Received: " + message);
		countDownLatch.countDown();
	}
	public CountDownLatch getCountDownLatch() {
		return countDownLatch;
	}
} 

Configuration file for RabbitMQ


RabbitTemplate: This is helper class to send and receive messages.
Queue: This is container for the messages.
TopicExchange: Routs the message to a queue.
Binding: Binds the queue and topic exchange for a given queue name.
Find the configuration class.
RabbitMQConfig.java
package com.concretepage.rabbitmq;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
public class RabbitMQConfig {
	final static String queueName = "concretepage";
	@Autowired
	RabbitTemplate rabbitTemplate;
	@Bean
	Queue queue() {
		return new Queue(queueName, false);
	}
	@Bean
	TopicExchange exchange() {
		return new TopicExchange("concretepage-exchange");
	}
	@Bean
	Binding binding(Queue queue, TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(queueName);
	}
	@Bean
	SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
		SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.setQueueNames(queueName);
		container.setMessageListener(listenerAdapter);
		return container;
	}
        @Bean
        MessageReceiver receiver() {
                return new MessageReceiver();
        }
	@Bean
	MessageListenerAdapter listenerAdapter(MessageReceiver receiver) {
		return new MessageListenerAdapter(receiver, "receiveMsg");
	}
} 

Run the Example using RabbitTemplate.convertAndSend method

To run the example, we will use RabbitTemplate.convertAndSend method to send the message. This method converts java object to Amqp message and is sent to default topic exchange.
Main.java
package com.concretepage.rabbitmq;
import java.util.concurrent.TimeUnit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
	final static String queueName = "concretepage";
	public static void main(String[] args) throws InterruptedException {
		   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	           ctx.register(RabbitMQConfig.class);
	           ctx.refresh();
		   System.out.println("---Message is being sent---");
		   RabbitTemplate rabbitTemplate = (RabbitTemplate)ctx.getBean("rabbitTemplate");
		   MessageReceiver receiver = (MessageReceiver)ctx.getBean("receiver");
		   rabbitTemplate.convertAndSend(queueName, "Hello World!");
		   receiver.getCountDownLatch().await(1, TimeUnit.SECONDS);
		   ctx.close();
	}
} 
Find the output.
---Message is being sent--- 
Message Received: Hello World! 
Now we are done. Enjoy learning.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us