1、当信息在一个队列中变成死信时,可以重新发送到DLX,绑定DLX的队列称为rabbitmq。
也叫死信队列。
2、常用于自动取消订单超时、提前提醒会议预订等。
实例
importpika importjson importtime credentials=pika.PlainCredentials('admin','admin')#用户名和密码的mq #虚拟队列需要指定参数virtual_host,如果是默认的,可以不填。 connection=pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1',port=5672,credentials=credentials)) channel=connection.channel() #声明消息队列,消息将在这个队列中传递,如果不存在,则创建 queue_name="delay_queue_a" exchange='delay_exchange_a' routing_key='delay_routing_key_a' dead_letter_exchange='dead_exchange_a'#'amq.direct'#'dead_exchange_a' dead_letter_routing_key='dead_letter_routing_key_a'#'dead_queue_a'#'dead_letter_routing_key_a' arguments={ "x-message-ttl":5000, 'x-dead-letter-exchange':dead_letter_exchange, 'x-dead-letter-routing-key':dead_letter_routing_key } channel.confirm_delivery() channel.exchange_declare(exchange=exchange,durable=True,exchange_type='direct') result=channel.queue_declare(queue=queue_name,durable=False,arguments=arguments) channel.queue_bind(exchange=exchange,queue=queue_name,routing_key=routing_key) foriinrange(10): message=json.dumps({'OrderId':i}) #将数值routing_key插入到队列中 channel.basic_publish(exchange=exchange,routing_key=routing_key,body=message, properties=pika.BasicProperties(delivery_mode=2)) print(message) time.sleep(1.5) connection.close()
以上是python rabitmq的介绍,希望对大家有所帮助。更多Python学习指导:python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。