コード例 #1
0
ファイル: fanout_consumer2.py プロジェクト: dxcv/pydemo

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()
コード例 #2
0
# -*- coding:utf-8 -*-
# 持续消费

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

channel.queue_declare(queue='test_fanout',durable=True) #声明一个队列,生产者和消费者都要声明一个相同的队列,用来防止万一某一方挂了,另一方能正常运行

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(10)
    channel.basic_ack(delivery_tag=method.delivery_tag, multiple=False) #手动设置 ,只有 no_ack =False 时才有用
    print("调用发送短信接口,手机号码: %s" % body)

#公平调度。在一个消费者未处理完一个消息之前不要分发新的消息给它,
# 而是将这个新消息分发给另一个不是很忙的消费者进行处理。
# 为了解决这个问题我们可以在消费者代码中使用 channel.basic.qos ( prefetch_count = 1 ),将消费者设置为公平调度。
channel.basic_qos(prefetch_count=1)

#设置no_ack = False 手动处理(channel.basic_ack(delivery_tag=method.delivery_tag, multiple=False))
channel.basic_consume(consumer_callback =consumer_callback , queue='test_fanout' ,no_ack = False )
channel.start_consuming()
コード例 #3
0
    '''
    :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(160)
    channel.basic_ack(delivery_tag=method.delivery_tag, multiple=False)
    print(body)


channel.exchange_declare(exchange='direct_log', exchange_type='direct')
#exclusive=True 跟 durable=True 不能同时设置
#exclusive=True 消费者只有线程存在的前提下收到消息,不存在就收不到,所以就不能持久化了
queue_declare = channel.queue_declare(queue="direct_queue_log1", durable=True)
# queue_name = queue_declare.method.queue #不能用这个随机的名字,会生成很多queue

severities = ['info', 'err']
for severity in severities:
    channel.queue_bind(exchange='direct_log',
                       queue='direct_queue_log1',
                       routing_key=severity)

channel.basic_consume(consumer_callback=consumer_callback,
                      queue="direct_queue_log1",
                      no_ack=False)
channel.start_consuming()