This directory contains the foundational implementation of a Point-to-Point (P2P) messaging pattern. It demonstrates how a Publisher sends a batch of 10 messages to a specific queue and how a Consumer retrieves and processes them.
This example highlights the decoupling of systems: the publisher doesn't need to know if the consumer is active when the messages are sent.
- Language: Python 3.x
- Message Broker: RabbitMQ
- Library:
pika(Python RabbitMQ client)
- Queue Durability: The
ABCqueue is marked as durable to survive broker restarts. - Message Persistence: Messages are marked as persistent to prevent data loss.
- Manual Acknowledgments (ACK): The consumer explicitly tells RabbitMQ when a message is processed.
- Fair Dispatch: Uses
prefetch_count=1to ensure the consumer only receives one message at a time.
pip install pikaThe project is pre-configured with default values. You can find a template in .env.example.
- Note: If you use the automated script (
run_demo.sh), it will handle all environment variables for you automatically.
The easiest way to run the project on Linux/macOS is using the provided shell script. It handles Docker setup, environment variables, and execution in one command:
chmod +x run_demo.sh
./run_demo.shIf you prefer to run the components manually, follow these steps:
Option A: Using Docker (Recommended)
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-managementOption B: Local Installation (No Docker)
If you prefer not to use Docker, follow the official installation guides:
- Windows: Install via Chocolatey (
choco install rabbitmq) or the official installer. - macOS: Install via Homebrew (
brew install rabbitmq). - Linux (Ubuntu/Debian):
sudo apt-get install rabbitmq-server. Once installed, ensure the service is running:sudo service rabbitmq-server start.
The consumer will wait for messages to arrive in the ABC queue.
python3 consumer.pyThe publisher will send 10 messages to the queue and then exit.
python3 publisher.pyResult: You will see the consumer receive, process, and acknowledge each of the 10 messages in real-time.