Skip to main content

Messaging without Loss: Mastering Persistence in the Message Middleware

Messaging without Loss: Mastering the Persistence Mechanisms of Messaging Middleware

When it comes to message persistence and replay, we can implement this scenario using Spring Boot with RabbitMQ, which supports message persistence to ensure that messages are not lost during sending and receiving. At the same time, we can use the message replay mechanism to resend messages when needed.

First, we need to create a Spring Boot project and add the following dependencies to the pom.xml file:

 
<dependencies>
    <!-- Spring Boot Starter for RabbitMQ -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
</dependencies>

Next, we create a message sender and receiver. First, we define a message entity class Message:

 
public class Message implements Serializable {
    private String content;

    // Constructors, Getter and Setter omitted...
}

Then, we create a message sender MessageSender:

 
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Bean
    public DirectExchange myExchange() {
        return new DirectExchange("my-exchange", true, false); // Set switch persistence to true
    }

    public void sendMessage(Message message) {
        amqpTemplate.convertAndSend("my-exchange", "my-routing-key", message);
        System.out.println("Message sent: " + message.getContent());
    }
}


In the above code, we use AmqpTemplate to send the message and specify the switch name as my-exchange and the routing key as my-routing-key.

Next, we create a message receiver MessageReceiver:

 
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiver {
    @Bean
    public Queue myQueue() {
        return new Queue("my-queue", true); // Set queue persistence to true
    }

    @RabbitListener(queues = "my-queue")
    public void receiveMessage(Message message) {
        System.out.println("Message received: " + message.getContent());
    }
}




In the above code, we use the @RabbitListener annotation to specify the queue to listen to as my-queue and define a method receiveMessage to receive messages.

Next, we need to add the RabbitMQ configuration in the application.properties file:

 
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

In this example, we assume that RabbitMQ is running locally, on port 5672, with username and password guest.

Now, we can test it. We can call the code that sends the message in the main method:

 
@SpringBootApplication
public class MessagingApplication implements CommandLineRunner {
    @Autowired
    private MessageSender messageSender;

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

    @Override
    public void run(String... args) {
        Message message = new Message();
        message.setContent("Hello, RabbitMQ!");

        messageSender.sendMessage(message);
    }
}



In this example, we create a Message object and call messageSender.sendMessage(message) to send the message.

After running the application, you should be able to see output similar to the following on the console:

! Messaging Without Loss: Mastering the Persistence Mechanisms of the Message Middleware

This indicates that the message was successfully sent and received.

In this example, the message is persistent, which means that even if the RabbitMQ server fails during sending and receiving, the message will be saved and resent after the server recovers.

This article was published by mdnice Multiplatform