How to mock RabbitMQ queue binding with routing keys in Python

A mock demonstration of binding a queue to an exchange with multiple routing keys in RabbitMQ using Python and pika, without a real broker connection.

Easy Python 3.9+ Aug 9, 2026 Streaming & messaging 14 views 0 copies

Requires third-party packages — install first
pip install pika

Python code

33 lines
Python 3.9+
import pika
import sys


def bind_queue_with_routing(channel, queue_name, exchange_name, routing_keys):
    """
    Mock RabbitMQ queue binding with routing keys.
    Prints the binding configuration instead of connecting to a real broker.
    """
    for routing_key in routing_keys:
        binding = {
            "queue": queue_name,
            "exchange": exchange_name,
            "routing_key": routing_key
        }
        print(f"Binding: {binding}")
        # In a real scenario, you would call:
        # channel.queue_bind(queue=queue_name, exchange=exchange_name, routing_key=routing_key)


def main():
    queue_name = "order_events"
    exchange_name = "topic_exchange"
    routing_keys = ["order.created", "order.updated", "order.#"]

    # Mock channel (no real connection needed for demonstration)
    mock_channel = None

    bind_queue_with_routing(mock_channel, queue_name, exchange_name, routing_keys)


if __name__ == "__main__":
    main()

Output

stdout
Binding: {'queue': 'order_events', 'exchange': 'topic_exchange', 'routing_key': 'order.created'}
Binding: {'queue': 'order_events', 'exchange': 'topic_exchange', 'routing_key': 'order.updated'}
Binding: {'queue': 'order_events', 'exchange': 'topic_exchange', 'routing_key': 'order.#'}

How it works

This function simulates RabbitMQ queue binding by iterating over routing keys and printing the binding configuration. It accepts a channel parameter to mirror the real pika API, making it easy to swap in a real connection later. The mock channel is passed as None since no actual broker interaction occurs. This pattern is useful for testing and documentation without requiring a running RabbitMQ instance.

Common mistakes

  • Forgetting to pass routing keys as a list, causing iteration over a string
  • Hardcoding the exchange or queue name instead of using parameters
  • Not handling empty routing_keys list, which would produce no output

Variations

  1. Use a real pika channel with connection parameters to perform actual bindings
  2. Read routing keys from a configuration file or environment variable

Real-world use cases

  • Documenting or testing message routing configuration before deploying to production.
  • Generating a visual audit of queue bindings for microservices architecture reviews.
  • Simulating message flow in CI/CD pipelines without spinning up a RabbitMQ container.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Streaming & messaging

Related tutorials and quizzes for this topic.