Programmation Concurrente

RabbitMQ Exam Cheat Sheet

Programmation Concurrente 2 - RabbitMQ Exam Cheat Sheet

Exam Context: Consumer/Producer RabbitMQ implementation in D20.22 on Mac minis Access: Internet available, AI blocked Format: Repo clone in PyCharm, partial code to complete

🔗 Liens Utiles


Navigation Rapide

📚 Patterns Essentiels

⚠️ Problèmes Courants & Solutions

🔧 Code Snippets Pratiques

🎯 Préparation Examen


Table of Contents

  1. RabbitMQ Core Concepts
  2. Python Pika Library
  3. Producer/Consumer Patterns
  4. Exchange Types
  5. Quick Reference

RabbitMQ Core Concepts

Message Broker Architecture

Message Broker = Service that validates, transforms, and routes messages in a distributed system

Key Components:

  • Producer - Sends messages to exchanges
  • Exchange - Routes messages to queues (like a post office)
  • Queue - Stores messages until consumed (like a mailbox)
  • Consumer - Receives and processes messages
  • Binding - Rule connecting exchange to queue

AMQP Protocol:

  • Messages published to exchanges
  • Exchanges distribute to queues via bindings
  • Broker delivers or consumers fetch messages

Python Pika Library

Installation

pip install pika
poetry add pika

Basic Connection

import pika

conn_param = pika.ConnectionParameters(
    host='concurp1.isc.heia-fr.ch',
    port=5072,
    credentials=pika.PlainCredentials('guest', 'guest')
)

connection = pika.BlockingConnection(conn_param)
channel = connection.channel()
channel.queue_declare(queue='my_queue')

# Always close
connection.close()

Producer/Consumer Patterns

Simple Producer

channel.basic_publish(
    exchange='',
    routing_key='task_queue',
    body=message.encode(),
    properties=pika.BasicProperties(delivery_mode=2)
)

Simple Consumer

def callback(ch, method, properties, body):
    print(f"Received: {body.decode()}")
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(
    queue='task_queue',
    on_message_callback=callback,
    auto_ack=False
)

channel.start_consuming()

Exchange Types

Direct - Exact key match → Unicast (point-to-point)

Fanout - Broadcast (ignore key) → Pub/Sub (broadcast)

Topic - Pattern match (*, #) → Filtered pub/sub

Headers - Header attributes → Complex routing rules


Quick Reference

Connection

pika.ConnectionParameters(host, port, credentials)
pika.BlockingConnection(params)
connection.channel()

Queue Operations

channel.queue_declare(queue, durable=True)
channel.queue_bind(exchange, queue, routing_key)
channel.queue_delete(queue)

Publish

channel.basic_publish(exchange, routing_key, body, properties)

Consume

channel.basic_consume(queue, on_message_callback, auto_ack)
channel.start_consuming()

Acknowledgment

ch.basic_ack(delivery_tag)      # Success
ch.basic_nack(delivery_tag)      # Failure, requeue
ch.basic_reject(delivery_tag)    # Reject single message

Exam Strategy

Pre-Exam

  • [ ] Arrive 10 min early
  • [ ] Configure git credentials
  • [ ] Clone repo, open PyCharm
  • [ ] Test RabbitMQ connection

During Exam

  1. Read scenario (5 pages)
  2. Identify pattern (Producer/Consumer, RPC, Work Queue)
  3. Check provided code
  4. Implement missing parts
  5. Test incrementally

School Server Config

HOST = 'concurp1.isc.heia-fr.ch'
PORT = 5072
USER = 'guest'
PASSWORD = 'guest'

Explore sub-pages above for deep-dive examples and problem solutions!