Last active
July 25, 2019 22:32
-
-
Save ryanermita/82587ab703c2f0413827aa7f673a5f7a to your computer and use it in GitHub Desktop.
sample publisher using rabbitmq pika client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pika | |
import sys | |
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) | |
channel = connection.channel() | |
channel.exchange_declare(exchange='direct_exchange', exchange_type='direct') | |
channel.queue_declare(queue='direct_queue', durable=True) | |
channel.queue_bind(exchange='direct_exchange', queue="direct_queue", routing_key="direct.routing.key") | |
message = " ".join(sys.argv[1:]) or "Hello World!" | |
channel.confirm_delivery() | |
try: | |
channel.basic_publish(exchange='direct_exchange', routing_key='direct.routing.key', | |
body=message, properties=pika.BasicProperties(delivery_mode=2) | |
) | |
print("Sent %r" % message) | |
except pika.exceptions.UnroutableError: | |
print("Failed to send message %r" % message) | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment