Kombu Exchange

来源:互联网 发布:c语言数组实验报告 编辑:程序博客网 时间:2024/05/16 15:10

本文地址:http://blog.csdn.net/spch2008/article/details/11569127


示例代码:

from kombu.entity import Exchange, Queuefrom kombu.messaging import Consumerfrom kombu.connection import Connectionconnection = Connection('amqp://guest:bupt@172.16.4.1:5672//')channel = connection.channel()news_exchange = Exchange('news', type='topic')bound_exchange = news_exchange(channel)bound_exchange.declare()
第9行声明一个Exchange,并不能再上面执行任何操作,只是描述了一下这个Exchange的特性。

第10行将exchange与特定的channel进行绑定。内部其实只是复制了自身,然后将_channel赋值而已。(Exchange内部记住了channel)

def __call__(self, channel):    return self.bind(channel)def bind(self, channel):    return copy(self).maybe_bind(channel)def maybe_bind(self, channel):    if not self.is_bound and channel:        self._channel = channel        self.when_bound()        self._is_bound = True    return selfdef when_bound(self):    pass
第11行,在服务器上创建Exchange。这样,通过命令rabbitmqctl list_exchanges就可以看到创建的exchange。



参数说明:

class kombu.Exchange(name='', type='', channel=None, **kwargs)Parameters:name -  Exchange名字        type –  direct, fanout,topic,header几种类型        channel – Exchange需要绑定的channel        durable – server重启后是否恢复Exchange,默认True,即重启后Exchange仍然存在        delivery_mode – transient模式,消息只存储于内存server重启或死机,消息丢失。                persistent模式,消息存在内存与硬盘,信息不丢失,默认persistent。


函数说明:

declare(self, nowait=False)   server上创建Exchange    nowait True 不等待Server的响应信息。delete(self, if_unused=False, nowait=False)  server上删除Exchange    if_unused  True只在Exchange没有使用的时候删除



原创粉丝点击