U1.21 Ubuntu Quick Start (QS): RabbitMq direct and topic exchange type. - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki

Preliminary steps

Read the articles

Install Python Pika

  • run the command
sudo apt-get install python3-pip
  • run the command
sudo python3 -m pip install pika --upgrade

Topics

Under Documents folder create two files

  • create two files
Click to show the picture

picture

  • emit_log_topic.py
#!/usr/bin/env python
import pika
import sys

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

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

routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(
    exchange='topic_logs', routing_key=routing_key, body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()
  • emit_log_topic.py
#!/usr/bin/env python
import pika, sys, os

def main():
    connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()

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

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

    binding_keys = sys.argv[1:]
    if not binding_keys:
        sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
        sys.exit(1)

    for binding_key in binding_keys:
        channel.queue_bind(
            exchange='topic_logs', queue=queue_name, routing_key=binding_key)

    print(' [*] Waiting for logs. To exit press CTRL+C')


    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body.decode()))


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

    channel.start_consuming()


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)

Run four terminals

  • in the first terminal run the commands
cd Documents
sudo python receive_logs_topic.py "kern.*"
  • in the second terminal run the commands
cd Documents
sudo python receive_logs_topic.py "*.critical"
  • in the third terminal run the commands
cd Documents
sudo python receive_logs_topic.py "*.critical"
  • in the fourth terminal run the commands
cd Documents
sudo python emit_log_topic.py "kern.critical" "A critical kernel error 1"
sudo python emit_log_topic.py "kern.critical" "A critical kernel error 2"
Click to show the picture

picture

Routing

  • We get the same functions as above (as for Topics)
    • but instead of a dot-separated list of words (like "stock.usd.nyse" or "*.xxx" or "#.yyy")
      • we define the filter as a list of routing keys, combined by the operator "OR":
         foreach(var severity in args)
            {
                channel.QueueBind(queue: queueName,
                                  exchange: "direct_logs",
                                  routingKey: severity);
            }
⚠️ **GitHub.com Fallback** ⚠️