RabbitMQ Overview, Installation Guide, and Delayed Message Implementation

RabbitMQ is a lightweight, open‑source AMQP broker offering reliable, flexible routing, clustering, and multi‑protocol support; it can be installed via Homebrew, Windows installers, or source on CentOS, and, using the rabbitmq_delayed_message_exchange plugin, Spring Boot applications can configure custom exchanges, queues, and send delayed messages with an x‑delay header.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
RabbitMQ Overview, Installation Guide, and Delayed Message Implementation

RabbitMQ is an open‑source implementation of the AMQP protocol, written in Erlang. It is lightweight, easy to deploy, supports multiple messaging protocols, and can be clustered for high availability and scalability.

Key features include reliability (persistence, delivery acknowledgments), flexible routing via exchanges, clustering, highly‑available queues, multi‑protocol support, many language clients, a management UI, tracing, and a plugin system.

Why use RabbitMQ?

It enables asynchronous processing, decouples producers from consumers, and helps smooth traffic spikes (throttling). An illustrative analogy uses a flower shop to show how orders can be recorded and processed later without blocking the customer.

Installation

Mac brew install rabbitmq Windows

Install Erlang: http://erlang.org/download/otp_win64_21.3.exe Install RabbitMQ:

https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe

Enable the management plugin: rabbitmq-plugins enable rabbitmq_management Verify by opening http://localhost:15672/ in a browser.

CentOS

Install Erlang (example commands):

# rabbitmq depends on Erlang – download source
cd /path/to/erlang-source && ./configure --prefix=/usr/local/erlang
make && make install

# Add Erlang to PATH
export PATH=$PATH:/usr/local/erlang/bin
source /etc/profile

Install RabbitMQ (example commands):

# Download and extract RabbitMQ
# Add RabbitMQ to PATH
export PATH=$PATH:/usr/local/rabbitmq_server-3.8.16/sbin
source /etc/profile

# Start RabbitMQ
cd /usr/local/rabbitmq_server-3.8.16/sbin
./rabbitmq-server start

Delayed Message Implementation

RabbitMQ supports delayed messages via two methods: dead‑letter queues or the rabbitmq_delayed_message_exchange plugin. The guide focuses on the plugin approach.

Install the plugin (compatible versions required):

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

Java Spring Boot Integration

Add the AMQP starter dependency:

<!-- Message queue dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Configure RabbitMQ in application.yml:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    virtual-host: /mall
    username: im
    password: xxxxxx
    publisher-confirms: true

Create a custom exchange and queue for delayed messages:

/**
 * Message queue configuration
 */
@Configuration
public class RabbitMqConfig {
    @Bean
    CustomExchange chatPluginDirect() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(QueueEnum.QUEUE_RESET_MESSAGE_CANCEL.getExchange(), "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue chatPluginQueue() {
        return new Queue(QueueEnum.QUEUE_RESET_MESSAGE_CANCEL.getName());
    }

    @Bean
    public Binding chatPluginBinding(CustomExchange chatPluginDirect, Queue chatPluginQueue) {
        return BindingBuilder.bind(chatPluginQueue).to(chatPluginDirect)
                .with(QueueEnum.QUEUE_RESET_MESSAGE_CANCEL.getRouteKey()).noargs();
    }
}

Sender that sets the x-delay header:

@Component
@Slf4j
public class ChatQueueSender {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessageToChat(Long cmid, long delayTimes) {
        amqpTemplate.convertAndSend(QueueEnum.QUEUE_RESET_MESSAGE_CANCEL.getExchange(),
                QueueEnum.QUEUE_RESET_MESSAGE_CANCEL.getRouteKey(), cmid, message -> {
            message.getMessageProperties().setHeader("x-delay", delayTimes);
            return message;
        });
    }
}

Receiver that processes delayed messages:

@Component
@Slf4j
@RabbitListener(queues = "im.chat.cancel")
public class ChatQueueReceiver {
    @Autowired
    private ChatRestartRobotService chatRestartRobotService;

    @RabbitHandler
    public void handleOnChat(Long cmid) {
        chatRestartRobotService.restartRobot(cmid);
    }
}

After configuring the plugin and deploying the above beans, delayed messages can be sent by specifying the desired delay in milliseconds.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaSpring BootMessage QueueRabbitMQDelayed MessagingInstallation
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.