A kiválasztott változat és az aktuális verzió közötti különbségek a következők.
Előző változat mindkét oldalon Előző változat | |||
tanszek:oktatas:iss_t:messaging_systems [2023/04/24 19:14] knehez [RabbitMQ example] |
tanszek:oktatas:iss_t:messaging_systems [2023/05/14 15:23] (aktuális) knehez [RabbitMQ example] |
||
---|---|---|---|
Sor 243: | Sor 243: | ||
</code> | </code> | ||
- | ===== RabbitMQ example ===== | ||
- | |||
- | <code bash> | ||
- | git clone https://github.com/knehez/isi.git | ||
- | cd isis/rabbitmq-python | ||
- | docker-compose up -d rabbitmq | ||
- | </code> | ||
- | |||
- | This is a docker-compose.yml file that defines a single service called rabbitmq using the official rabbitmq Docker image with management plugin. | ||
- | |||
- | <code yml> | ||
- | version: '3.7' | ||
- | services: | ||
- | rabbitmq: | ||
- | image: 'rabbitmq:3-management' | ||
- | ports: | ||
- | - 5672:5672 | ||
- | - 15672:15672 | ||
- | </code> | ||
- | |||
- | consumer.py | ||
- | |||
- | <code python> | ||
- | import pika | ||
- | import sys | ||
- | import os | ||
- | |||
- | connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq')) | ||
- | channel = connection.channel() | ||
- | |||
- | channel.queue_declare(queue='hello') | ||
- | |||
- | |||
- | def callback(ch, method, properties, body): | ||
- | print(" [x] Received %r" % body) | ||
- | |||
- | |||
- | channel.basic_consume( | ||
- | queue='hello', on_message_callback=callback, auto_ack=True) | ||
- | |||
- | print(' [*] Waiting for messages. ') | ||
- | |||
- | while (True): | ||
- | channel.start_consuming() | ||
- | </code> | ||
- | |||
- | producer.py | ||
- | |||
- | <code python> | ||
- | import pika | ||
- | |||
- | connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq')) | ||
- | channel = connection.channel() | ||
- | |||
- | channel.queue_declare(queue='hello') | ||
- | |||
- | channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') | ||
- | print(" [x] Sent 'Hello World!'") | ||
- | connection.close() | ||
- | </code> | ||