Created
July 28, 2023 20:31
-
-
Save gustavocoleta/6175c725d5179c0251c064f1dd869d10 to your computer and use it in GitHub Desktop.
Publish on RabbitMQ Queue using Python
This file contains 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
#!/usr/bin/python3 | |
import pika, os, logging, json | |
logging.basicConfig() | |
credentials = pika.PlainCredentials('user', 's3cre7') | |
parameters = pika.ConnectionParameters('localhost', | |
5672, | |
'/', | |
credentials) | |
connection = pika.BlockingConnection(parameters) # Connect to CloudAMQP | |
channel = connection.channel() # start a channel | |
channel.queue_declare(queue='queue-name', durable=True) # Declare a queue | |
for x in range(100): | |
mensagem = { | |
"pattern": "queue-name", | |
"data": { | |
"descricao": "Message: " + str(x+1) | |
} | |
} | |
channel.basic_publish(exchange='', routing_key='queue-name', body=json.dumps(mensagem)) | |
print ("[x] Message " + str(x+1) + " sent to consumer") | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment