Open In App

Event-Driven Communication with Messaging Queues

Event-driven communication with messaging queues is like having a virtual post office where system components can send and receive messages without waiting for each other. It's a pattern that allows different parts of a system to talk to each other asynchronously, which means they don't have to be in sync all the time. This setup is super useful for building systems that can handle lots of events happening at once, and it makes the whole microservices architecture more flexible and scalable.

Key Terminologies:

Step-by-step Implementation of Event-Driven Communication with Messaging Queues

To demonstrate event-driven communication, we'll develop a simple Spring Boot application that publishes messages to the RabbitMQ message broker, and consumers process those messages.

Step 1: Setup the RabbitMQ

We can install and run the RabbitMQ locally of the computer system.


Run the RabbitMQ:

rabbitmq-plugins.bat enable rabbitmq_management


Refer the image:

cmd Output


Once run the above command then the rabbitMQ runs on the localsystem at port number 15672. Default username and password are guest.

RabbitMQ Dashboard


Step 2: Create the Spring Boot Project

Create the new Spring Boot project using Spring initializer on creating the project including the below dependencies into the project.

Once create the project then the file structure looks like the below image.

Folder Structure


Step 3: Configure the RabbitMQ Connection

Open the application.properties file then add the RabbitMQ connection properties into the project.

spring.application.name=event-communication-rabbitMQ

# RabbitMQ connection properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

# Queue name
spring.rabbitmq.queue=myQueue


Step 4: Create the Message Producer

We will create the component class to send the message to RabbitMQ

Go to src > org.example.eventcommunicationrabbitmq > producer > MessageProducer and put the below code.

package org.example.eventcommunicationrabbitmq.producer;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private Queue queue;

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(queue.getName(), message);
        System.out.println("Message sent: " + message);
    }
}


Step 5: Create the Message Consumer

We will create the component class to consume the message to RabbitMQ

Go to src > org.example.eventcommunicationrabbitmq > consumer > MessageConsumer and put the below code.

package org.example.eventcommunicationrabbitmq.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageConsumer {
    @RabbitListener(queues = "${spring.rabbitmq.queue}")
    public void receiveMessage(String message) {
        System.out.println("Message received: " + message);
        // Process the message
    }

}


Step: 6 Configure the RabbitMQ Queue

We will define the queue in the Spring application and we can use the @Configuration class of the project.

Go to src > org.example.eventcommunicationrabbitmq > config > RabbitMQConfig and put the below code.

package org.example.eventcommunicationrabbitmq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue queue() {
        return new Queue("myQueue", false);
    }
}


Step 7: Test the Communication

Open the main class and inject the MessageProducer and send the message.

Go to src > org.example.eventcommunicationrabbitmq > EventCommunicationRabbitMqApplication and put the below code.

package org.example.eventcommunicationrabbitmq;

import org.example.eventcommunicationrabbitmq.producer.MessageProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EventCommunicationRabbitMqApplication implements CommandLineRunner {

    @Autowired
    private MessageProducer producer;

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

    @Override
    public void run(String... args) throws Exception {
        producer.sendMessage("Hello RabbitMQ!");
    }
}


Step 8: Run the Application

Once we run the application, then the project will run at port 8080. We can see the messages being sent and received in the console.

RabbitMQ Dashboard:

RabbitMQ Dashboard


Output:

Below in the console, we can see the Message is successfully sent and received.

Output Screen

This project demonstrates a basic Spring Boot application that sends messages to RabbitMQ using a producer component and consumes them using a consumer component.

Article Tags :