您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關RabbitMQ發送端接收端生產者消費者的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
rabbit_conn_send_producer.py
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))#rabbit默認端口5672 建立一個基本的 socket連接
channel = connection.channel()#聲明一個管道 在管道里面發消息
# 聲明queue
channel.queue_declare(queue='hello')
# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
routing_key='hello',#queue名字
body='Hello World!') #body 發送的消息
print(" [x] Sent 'Hello World!'")
connection.close()
rabbit_conn_recive_consumer.py
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost')) #rabbit默認端口5672 建立一個基本的 socket連接
channel = connection.channel()#聲明一個管道 在管道里面收消息
# You may ask why we declare the queue again ? we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
#channel.queue_declare(queue='hello')#聲明queue
def callback(ch, method, properties, body):#處理消息
print("---->",ch,method,properties)#ch 管道內存對象地址 method:發給queue的信息
print(" [x] Received %r" % body)
channel.basic_consume(#消費消息
callback,#如果收到消息,就調用CALLBACK函數來處理消息
queue='hello',#從哪個隊列里收消息
no_ack=True) #不需要確認消息是否接收
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()#啟動 開始收消息 一直收,沒有就卡主
rabbit_conn_recive_consumer_no_ack.py
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika,time
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost')) #rabbit默認端口5672 建立一個基本的 socket連接
channel = connection.channel()#聲明一個管道 在管道里面收消息
# You may ask why we declare the queue again ? we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
#channel.queue_declare(queue='hello')#聲明queue
def callback(ch, method, properties, body):#回調函數
print("---->",ch,method,properties)#ch 管道內存對象地址 method:發給queue的信息
time.sleep(5)#模擬消息處理時間
print(" [x] Received %r" % body)
channel.basic_consume(#消費消息
callback,#如果收到消息,就調用CALLBACK函數來處理消息
queue='hello',#從哪個隊列里收消息
#no_ack=True)#no acknowledgement 不確認 開啟的話表示不確認消息是否接收 接沒接收都不會給服務器端發消息 如果客戶端沒收到消息就忽略了
#關閉的話就要服務器就要確認消息是否接收,沒有確認到接收消息就會一直保留消息,會自動轉到另一個客戶端,socket一斷,rabbitMQ就將消息轉給另一個客戶端
#發送端生產者發送一條消息,被消費者接受者收到了,消費者接收端處理完之后自動給生產者發送端發一個確認,說消息處理完了,然后生產者發送端才會把消息從隊列里刪除,只要沒收到確認就不會刪除,如果生產者發送端沒收到確認,就會把消息轉給另一個消費者接收端
)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()#啟動 開始收消息 一直收,沒有就卡主
感謝各位的閱讀!關于“RabbitMQ發送端接收端生產者消費者的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。