rabbitmq 消息安全接收与消息持久化

可插拔式:一个插件,安装和写在不影响主程序运行

durable=True  持久,持续地 | 队列持久化

delivery_mode=2  消息持久化

import pika
import time

credentials = pika.PlainCredentials('alex', 'alex123')
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '192.168.14.52',credentials=credentials))
channel = connection.channel()

# 声明queue
channel.queue_declare(queue='task_queue',durable=True)

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
import sys

message = ' '.join(sys.argv[1:]) or "Hello World! %s" % time.time()

channel.basic_publish(exchange='',
                      routing_key='task_queue',
                      body=message,
                      properties=pika.BasicProperties(
                          delivery_mode=2,  # make message persistent
                      )

                      )
print(" [x] Sent %r" % message)
connection.close()
send_msg_safe
import pika, time

credentials = pika.PlainCredentials('alex', 'alex123')
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '192.168.14.52',credentials=credentials))
channel = connection.channel()


def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    time.sleep(20)
    print(" [x] Done")
    print("method.delivery_tag", method.delivery_tag)
    ch.basic_ack(delivery_tag=method.delivery_tag)
    #ackownledgement


channel.basic_consume(callback,
                      queue='task_queue',
                      #no_ack=True
                      )

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
recv_msg_safe.py

猜你喜欢

转载自www.cnblogs.com/jintian/p/11370901.html