RabbitMq Queue Durability and Persistent MessageDelivery | SpringBoot

In the tutorial, JavaSampleApproach will show you how to work with RabbitMq Queue Durability and Persistent MessageDelivery.

Related posts:
RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot

I. Technologies

– Java 8
– Maven 3.6.1
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.7.RELEASE
– RabbitMq

II. RabbitMq Queue Durability and Persistent MessageDelivery

RabbitMq Queue has 2 kind of durability: {Durable, Transient}.
Durable queue is a solution for re-declared persistent messages during broker restarts if broker is taken down.

springboot rabbitmq durable queue - architecture

Scenarios with above design:
– When sending 2 messages {a persistent message, a trasient message} to exchange X, all messages will be
delivered to 2 queues {Q1, Q2}.
– Stop RabbitMq service, then re-start it -> just a persistent message will be re-declared in durable queue Q1.

With Spring framework, default message is PERSISTENT mode:

We can change the delivery mode as below:

III. Practice

In the tutorial, we create 2 SpringBoot project as below:

springboot rabbitmq exchage headers - project structures

Step to do:
– Create SpringBoot projects
– Implement RabbitMq Producer
– Implement RabbitMq Consumer
– Setup RabbitMq exchange, queues
– Run and check results

1. Create SpringBoot projects

Using SpringToolSuite, create 2 SpringBoot projects, then add need dependency spring-boot-starter-amqp:

2. Implement RabbitMq Producer

– Open application.properties file, add spring.rabbitmq.*:

– Implement Publisher:

– Implement RabbitMq Publisher client:

3. Implement RabbitMq Consumer

– Open application.properties file, add spring.rabbitmq.*:

– Implement Subcribe:

4. Setup RabbitMq exchange, queues

Start RabbitMQ service. Then,

– Add fanout exchange jsa.exchange.logs:

springboot rabbitmq durable queue - create exchange

– Add 2 queues a durable queue jsa.queue.durable.logs, and a trasient queue jsa.queue.transient.logs

springboot rabbitmq durable queue - create durable queue

springboot rabbitmq durable queue - create transient queue

– Binding the queues & exchanges:

springboot rabbitmq durable queue - binding queue with exchange

5. Run and check results

– Run SpringRabbitMqPublisher with commandline mvn spring-boot:run,

-> Console’s logs:

– Check status of Queues,

-> Both of them have 2 messages.

springboot rabbitmq durable queue - message in queues after sending

– Stop RabbitMq service. Then re-start it,

-> Now remain only one durable queue jsa.queue.durable.logs which only 1 persistent message:

springboot rabbitmq exchage headers - remain 1 queue

springboot rabbitmq exchage headers - remain 1 message

– Run SpringRabbitMqSubcriber with commandline mvn spring-boot:run,

-> Console’s logs:

IV. Sourcecode

SpringRabbitMqSubcriber
SpringRabbitMqPublisher

By JavaSampleApproach | October 16, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*