Presentation

RabbitMQ is an open source messaging broker. It’s often referred to as a message-oriented middleware that implements the Advanced Message Queuing Protocol (AMQP).

AMQP makes it easy to decouple applications.
RabbitMQ provides a common platform for sending and receiving messages, where it guarantee the safety of messages until they are received.
RabbitMQ guarantees data delivery, provides non-blocking operations, and sends push notifications.
RabbitMQ provides a variety of features, including the tuning of application performance, clustering, flexible routing, federation, and so on.
RabbitMQ is one of the most powerful open source message broker software that is widely used in the tech companies such as Mozilla, VMware, Google, AT&T, and so on

Message brokers

A Message Broker is an architectural pattern that can receive messages from multiple destinations, determine the correct destination, and route the message along the correct route,
Message Brokers are centralized, in the architectural sense, to control and manage all messages. Therefore, all of the incoming and outgoing messages are sent through Message Brokers, which analyze and deliver the messages to their correct destination.

Message Queues

Message Queues provide concurrent and asynchronous operations to scale applications

1
channel.queueDeclare(queueName, true, false, false, null);
  • durable: This specifies whether the queue will survive server restarts. Note that it is required for a queue to be declared as durable if you want persistent messages to survive a server restart.
  • exclusive: This specifies whether the queue is restricted to only this connection.
  • autoDelete: This specifies whether the queue will be automatically deleted by the RabbitMQ broker as soon as it is not in use.

JavaDoc

RabbitMq and EC2
we can easily install our RabbitMQ to the AWS EC2 instance and can save images of the RabbitMQ installed operating system

Virtual hosts

Virtual hosts are administrative containers; they allow to configure many logically independent brokers hosts within one single RabbitMQ instance, to let many different independent applications share the same RabbitMQ server. Each virtual host can be configured with its independent set of permissions, exchanges, and queues and will work in a logically separated environment.

Delayed Message Exchange

1 - Ajouter le plugin (attention sur docker + ubuntu mettre le fichier 777)

1
2
sudo docker cp rabbitmq_delayed_message_exchange-0.0.1.ez  88da72966ec8:/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/plugins/rabbitmq_delayed_message_exchange-0.0.1.ez
sudo docker -it 88da72966ec8 bash

2 - Declarer un Exchange avec delay

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@SpringBootApplication
@EnableRabbit
public class Application {

final static String queueName = "spring-boot2";
final static String topicName = "spring-boot-exchange2";

@Bean
Queue queue() {
return new Queue(queueName, false);
}

@Bean
TopicExchange exchange() {
TopicExchange topic = new TopicExchange(topicName);
topic.setDelayed(true);
return topic;
}

@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}

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

}

3 - Lors de la reception, renvoyer le message avec un delai

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

@Service
public class Receiver {

private final RabbitTemplate rabbitTemplate;

@Autowired
public Receiver(final RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}

@RabbitListener(queues = Application.queueName)
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");

rabbitTemplate.convertAndSend(Application.topicName, Application.queueName, message, new MessagePostProcessor() {

@Override
public Message postProcessMessage(Message message) throws AmqpException {
message.getMessageProperties().setDelay(15000);
return message;
}
});
}
}

Ref