This module showcases asynchronous event handling in Magento 2 with RabbitMQ, providing a practical example to improve store performance and handle increased traffic.
- Producer: Sends messages to the waiting line (RabbitMQ queue)
- Queue: The waiting line itself, holding messages until they're processed.
- Topic: Topics are used to group related messages together based on a common theme or subject.
- Consumer: Listens to the queue, grabs messages, and takes action. (We have two in this example!)
- Order Processing: Don't freeze your store while handling orders. The queue takes care of it in the background.
- Product Updates: Update product info (like stock levels) without slowing down shoppers.
- Emails: Send emails (like order confirmations) in batches, improving performance.
- Indexing: Prioritize important tasks and optimize resource usage by using the queue.
- A working Magento 2 store.
- RabbitMQ server installed and configured (refer to official documentation for details)
-
Enable the module:
bin/magento module:enable Rick_RabbitMQExample
-
Upgrade Magento:
bin/magento setup:upgrade
Update the queue section with your RabbitMQ details (don't use defaults for real stores!).
'queue' => [
'consumers_wait_for_messages' => 0, // Processes messages immediately, minimizing latency.
'only_spawn_when_message_available' => 1, // Spawns consumer processes only when messages are available, optimizing resource utilization.
'amqp' => [
'host' => 'rabbitmq', // Assumes RabbitMQ server runs in a "rabbitmq" Docker container.
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'virtualhost' => '/',
'ssl' => false // Change to `true` for secure connections (recommended for production)
]
],
bin/magento queue:consumers:list
Run these commands to start listening for messages:
bin/magento queue:consumers:start test_consumer_one
bin/magento queue:consumers:start test_consumer_two
In production, you'll want to use cron to automatically start queue listeners, which runs every minute (* * * * *).
Add consumers to cron_consumers_runner in app/etc/env.php:
'cron_consumers_runner' => [
'cron_run' => true,
'max_messages' => 20000,
'consumers' => [
'test_consumer_one',
'test_consumer_two',
],
],
- Define Events: Identify the events in your module that should trigger message queue messages.
- Create Message Producer: Use the
Magento\Framework\MessageQueue\PublisherInterface
to publish messages to the RabbitMQ queue. - Implement Message Consumer: Create a queue consumer class that extends
Vendor\Module\Api\ConsumerInterface
and processes messages received from the queue. - Configure Queue and Consumer: Register your queue and consumer in the
etc
folder using XML configuration files (communication.xml
,queue_topology.xml
,queue_consumer.xml
, andqueue_publisher.xml
). These files define message routing logic and consumer association with the queue.
For more detaults on implementing custom message queues, refer to the Magento documentation: