What is RabbitMQ ?
RabbitMQ is an open source message broker that implements AMQP(Advance Message Queuing Protocol). Some of the main features of RabbitMQ are Security, Asynchronous Messaging, etc.
To perform message queuing we need three components:
- Producers
- Broker
- Consumers
Producers: It publishes the message to the queue based on queue name.
Broker: The message broker acts as a container to hold the message that is further used by another application.
Consumer: It consumes the message from the broker and use that message to perform further operations.
Architecture Diagram of RabbitMQ
Producer will publish the message to exchange and exchange will redirect the message to the corresponding queue and then the consumers attach to the queue can consume the message. And there is some binding between exchange and queue i.e. routing key. So through routing key exchange and queue bind to each other.
Let’s start the implementation :
First we will create two springboot projects one for producer and another for consumer. Also rabbitmq server should be there in our system (if not please install) and it should be running.
Folder Structure of rabbitmq-producer:
Folder Structure of rabbitmq-consumer:
Dependency for rabbitmq:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Dependencies of producer and consumer project
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
rabbitmq-producer
RabbitMQCondig.class
package com.gangforcode.rabbitmqproducer.config;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String QUEUE = "RabbitMQ queue";
public static final String EXCHANGE = "RabbitMQ Topic Exchange";
public static final String ROUTING_KEY = "RabbitMQ";
@Bean
public Queue queue(){
return new Queue(QUEUE);
}
@Bean
public TopicExchange exchange(){
return new TopicExchange(EXCHANGE);
}
@Bean
public Binding binding(Queue queue , TopicExchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
}
@Bean
public MessageConverter converter(){
return new Jackson2JsonMessageConverter();
}
@Bean
public AmqpTemplate template(ConnectionFactory connectionFactory){
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(
converter());
return rabbitTemplate;
}
}
UserController.class
package com.gangforcode.rabbitmqproducer.controller;
import com.gangforcode.rabbitmqproducer.config.RabbitMQConfig;
import com.gangforcode.rabbitmqproducer.model.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private RabbitTemplate template;
@PostMapping("/publish")
public String publisher(@RequestBody User user){
template.convertAndSend(RabbitMQConfig.EXCHANGE, RabbitMQConfig.ROUTING_KEY, user);
return "Data Published";
}
}
User.class
package com.gangforcode.rabbitmqproducer.model;
import lombok.Data;
@Data
public class User {
private String username;
private String password;
}
RabbitmqProducerApplication.class
package com.gangforcode.rabbitmqproducer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitmqProducerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqProducerApplication.class, args);
}
}
Console Output
rabbitmq-consumer project
RabbitMQConfig.class
package com.gangforcode.rabbitmqconsumer.config;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String QUEUE = "RabbitMQ queue";
public static final String EXCHANGE = "RabbitMQ Topic Exchange";
public static final String ROUTING_KEY = "RabbitMQ";
@Bean
public Queue queue(){
return new Queue(QUEUE);
}
@Bean
public TopicExchange exchange(){
return new TopicExchange(EXCHANGE);
}
@Bean
public Binding binding(Queue queue , TopicExchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
}
@Bean
public MessageConverter converter(){
return new Jackson2JsonMessageConverter();
}
@Bean
public AmqpTemplate template(ConnectionFactory connectionFactory){
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(
converter());
return rabbitTemplate;
}
}
Customer.class
package com.gangforcode.rabbitmqconsumer.model;
import lombok.Data;
@Data
public class Customer {
private String username;
private String password;
private String location;
}
RabbitMQListner.class
package com.gangforcode.rabbitmqconsumer;
import com.gangforcode.rabbitmqconsumer.config.RabbitMQConfig;
import com.gangforcode.rabbitmqconsumer.model.Customer;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQListner {
@RabbitListener(queues = RabbitMQConfig.QUEUE)
public void listner(Customer customer){
System.out.println(customer);
}
}
RabbitmqConsumerApplication.class
package com.gangforcode.rabbitmqconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitmqConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqConsumerApplication.class, args);
}
}
Console Output
Postman Client