Sobes.tech
Junior — Middle

Have you had experience setting up and using message routing in RabbitMQ?

sobes.tech AI

Answer from AI

In RabbitMQ, message routing is performed using exchanges and queues. The main types of exchanges are direct, topic, fanout, and headers, each of which routes messages differently.

For example, a direct exchange sends a message to a queue if the routing key matches the bound key of the queue. A topic exchange allows the use of wildcard patterns for more flexible routing.

Here's an example in Python using the pika library:

import pika

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

channel.exchange_declare(exchange='logs', exchange_type='topic')

result = channel.queue_declare('', exclusive=True)
queue_name = result.method.queue

binding_key = 'kern.*'
channel.queue_bind(exchange='logs', queue=queue_name, routing_key=binding_key)

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

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

print('Waiting for messages...')
channel.start_consuming()

In this example, a topic exchange is created, a queue is bound with a routing key, and the consumer receives messages matching the pattern kern.*.

Have you had experience setting up and using message… - sobes.tech