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.
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 rabbitmqWindows
Install Erlang:
http://erlang.org/download/otp_win64_21.3.exeInstall RabbitMQ:
https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exeEnable the management plugin:
rabbitmq-plugins enable rabbitmq_managementVerify 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/profileInstall 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 startDelayed 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_exchangeJava Spring Boot Integration
Add the AMQP starter dependency:
org.springframework.boot
spring-boot-starter-amqpConfigure RabbitMQ in application.yml :
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /mall
username: im
password: xxxxxx
publisher-confirms: trueCreate a custom exchange and queue for delayed messages:
/**
* Message queue configuration
*/
@Configuration
public class RabbitMqConfig {
@Bean
CustomExchange chatPluginDirect() {
Map
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.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.