Exemple #1
0
# -*- coding:utf-8 -*-
# 不用创建队列

from rbtmq.fanout_direct_topic.pikaUtil import pika, channel, connection

channel.exchange_declare(exchange='direct_log', exchange_type='direct')

message = '我要报告一个错误'
confirmation = channel.basic_publish(exchange='direct_log',
                                     routing_key='err',
                                     body=message,
                                     properties=None,
                                     mandatory=False,
                                     immediate=False)
if confirmation:
    print('已成功提交至队列中')
connection.close()
Exemple #2
0

def consumer_callback(channel, method, properties, body):
    '''
    :param channel: BlockingChannel
    :param method:  spec.Basic.Deliver
    :param properties: spec.BasicProperties
    :param body:  str or unicode
    :return:
    '''
    assert isinstance(channel,
                      pika.adapters.blocking_connection.BlockingChannel)
    # time.sleep(20)
    # channel.basic_ack(delivery_tag=method.delivery_tag, multiple=False) #手动设置 ,只有 no_ack =False 时才有用
    print(body)


channel.exchange_declare(exchange='important_notice1',
                         exchange_type='fanout',
                         durable=True)
#创建一个队列,exclusive=True(唯一性)表示在消费者与rabbitmq断开连接时,该队列会自动删除掉。
queue_declare = channel.queue_declare(exclusive=True)
#因为rabbitmq要求新队列名必须是与现存队列名不同,所以为保证队列的名字是唯一的,method.queue方法会随机创建一个队列名字,如:‘amq.gen-JzTY20BRgKO-HjmUJj0wLg‘。
queue_name = queue_declare.method.queue
channel.queue_bind(exchange='important_notice1', queue=queue_name)

channel.basic_consume(consumer_callback=consumer_callback,
                      queue=queue_name,
                      no_ack=True)
channel.start_consuming()
Exemple #3
0
# -*- coding:utf-8 -*-

# 数据只会放入到queue中,不会放入exchange中
#1.生产者只会往这个交换机通知;
#2.检查那些消费者,消费者绑定的queue中放入数据
#3.消费者获取queue中数据

from rbtmq.fanout_direct_topic.pikaUtil import pika, channel, connection

channel.exchange_declare(exchange='important_notice1',
                         exchange_type='fanout',
                         durable=True)  #创建一个fanout(广播)类型的交换机exchange。

message = "广播信息,请打开收音机,错过就收不到了,不提供回放哦1"
confirmation = channel.basic_publish(exchange='important_notice1',
                                     routing_key='',
                                     body=message,
                                     properties=pika.BasicProperties(
                                         delivery_mode=2, ),
                                     mandatory=False,
                                     immediate=False)
if confirmation:
    print('已成功提交至队列中')
connection.close()