← Back to the chapter · Next detail →

Technical detail · AMQP Core: Consumers and Topology Contracts

Declare the factory topology

Repeat the line-2 binding for lines 3 and 4, changing both queue name and line component.

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# Create topic exchange
channel.exchange_declare(
    exchange='factory-alerts',
    exchange_type='topic',
    durable=True, arguments={'alternate-exchange': 'unrouted-alerts'}
)

# Create bounded queues with message expiry (no dead-letter exchange yet)
queues = {
    'operations-critical': {'x-max-length': 1000, 'x-message-ttl': 3600000},
    'line1-supervisor': {'x-max-length': 500, 'x-message-ttl': 86400000},
    'line2-supervisor': {'x-max-length': 500, 'x-message-ttl': 86400000},
    'line3-supervisor': {'x-max-length': 500, 'x-message-ttl': 86400000},
    'line4-supervisor': {'x-max-length': 500, 'x-message-ttl': 86400000},
    'maintenance-log': {'x-max-length': 10000, 'x-message-ttl': 2592000000}  # 30 days
}

for queue_name, args in queues.items():
    channel.queue_declare(queue=queue_name, durable=True, arguments=args)
# Operations team: ALL critical alerts from ALL lines
channel.queue_bind(
    exchange='factory-alerts',
    queue='operations-critical',
    routing_key='alert.critical.#'
)
# Matches: alert.critical.line1.machine03
#          alert.critical.line2.machine47
#          alert.critical.line3.machine10

# Line 1 supervisor: ALL alerts from Line 1
channel.queue_bind(
    exchange='factory-alerts',
    queue='line1-supervisor',
    routing_key='alert.*.line1.#'
)
# Matches: alert.critical.line1.machine03
#          alert.warning.line1.machine15
#          alert.info.line1.machine20

# Line 2 supervisor: ALL alerts from Line 2
channel.queue_bind(
    exchange='factory-alerts',
    queue='line2-supervisor',
    routing_key='alert.*.line2.#'
)

# (Similar bindings for line 3 and 4 supervisors)

# Maintenance log: Everything for historical analysis
channel.queue_bind(
    exchange='factory-alerts',
    queue='maintenance-log',
    routing_key='alert.#'
)
# Matches: EVERYTHING

← Back to the chapter · Next detail →