Auto Message Retry in RabbitMQ and Spring Boot

Auto Message Retry in RabbitMQ and Spring Boot

Maheshwar Ligade's photo
Maheshwar Ligade
·Jul 9, 2022·

2 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

Table of contents

  • Introduction:
  • Tech Stack:
  • Conclusion:

In this article, you are going to learn how to auto-retry messages available on rabbitmq and Spring Boot applications. RabbitMQ is a queue messaging solution that will help you to have async communication between your services.

Introduction:

In the pub-sub implementation, a publisher should know whether the consumer consumed the message successfully or not which is the essential part. There are some patterns like a dead queue which is helping you to retry failed message.

Let us consider one scenario, you are using money transfer service but before money getting deducted from your account certain complaince and risk part needs to check that too handle in async way. Once complaince and risk factor satisfied then only send notification to deduct the money and transfer that to destination. In this scenario you need to make sure notification sent to perticular system and even in case of any failure happen the system should auto heal or auto retry.

Tech Stack:

  1. Java
  2. Spring Boot
  3. RabbitMQ
  4. Docker
  5. Gradle

RabbitMQ setup in local using Docker [%youtu.be/tmOVR2T7jkE]

Let us create spring Boot application and make sure to have below dependencies.

build.gradle:

      implementation 'org.springframework.boot:spring-boot-starter-amqp'
    implementation 'org.springframework.boot:spring-boot-starter-artemis'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.amqp:spring-rabbit-test'

application.properties:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
// enable retry
spring.rabbitmq.listener.simple.retry.enabled=true
// the first time will wait 5 seconds to try again
spring.rabbitmq.listener.simple.retry.initial-interval=5000 
//try a maximum of 10 times
spring.rabbitmq.listener.simple.retry.max-attempts=10 
spring.rabbitmq.listener.simple.retry.max-interval=300000 
spring.rabbitmq.listener.simple.retry.multiplier=3.0 // multiplies the above range by 3

queue.name=demo

Let us write down the consumer to consume the message. Consumer / Listener:

@Component
public class QueueConsumer {

    @RabbitListener(queues = {"${queue.name}"})
    public void receive(@Payload String fileBody) throws BusinessException {
        System.out.println("Message Consumed " + fileBody + LocalDateTime.now());

        if(Integer.parseInt(fileBody) == 1){
            throw new BusinessException("testing for exception");
        }

    }

}

SpringBootApplication class:

@EnableRabbit
@SpringBootApplication
public class RabbitmqretryexampleApplication{

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

}

Now coding part is done, let us do validation send some proper message and send some improper message that should raise some exception and retry happen.

Conclusion:

This article is about using property changes how you can achieve retry of messages in rabbitmq and spring boot.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this

Impressum

This is the community to share and learn together