RabbitMq中的exchange是什么
2015-10-25 00:00
148 查看
摘要: RabbitMq中的exchange的作用
在下图中的x便是exchange
P是生产者,红色为queue
X可以将P的task进行过滤,从而决定将task做如何处理:例如:
(1),舍弃任务
(2),将任务发送到某个task
(3),将任务发送到所有task
exchange有4中类型:direct, topic, headers and fanout
这次主要使用fanout
emit_log.py:task将被发送到exchange
receive_log.py
在下图中的x便是exchange
P是生产者,红色为queue
X可以将P的task进行过滤,从而决定将task做如何处理:例如:
(1),舍弃任务
(2),将任务发送到某个task
(3),将任务发送到所有task
exchange有4中类型:direct, topic, headers and fanout
这次主要使用fanout
emit_log.py:task将被发送到exchange
# -*- coding: UTF-8 -*- import pika if __name__ == '__main__': connection = pika.BlockingConnection(pika.ConnectionParameters("localhost")) channel = connection.channel() channel.exchange_declare(exchange="logs",type="fanout") message = "You are awsome!" for i in range(0, 100): # 循环100次发送消息 channel.basic_publish(exchange="logs", routing_key='', body=message + " " + str(i),) print "sending ", message
receive_log.py
# -*- coding: UTF-8 -*- import pika __author__ = 'Yue' def callback(ch, method, properties, body): print body if __name__ == '__main__': connection=pika.BlockingConnection(pika.ConnectionParameters("localhost")) channel=connection.channel() channel.exchange_declare(exchange="logs",type="fanout") #随机生成Queue result=channel.queue_declare(exclusive=True) #获取queue的name queue_name=result.method.queue print "queue_name",queue_name channel.queue_bind(exchange="logs",queue=queue_name) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
相关文章推荐
- 编写不受魔术引号影响的php应用
- 面向对象类-类
- 正则表达式
- Mint17/Ubuntu14.04 samba文件服务器搭建
- 解决PhpStorm不能自动提示父类的方法的问题
- HTTP的请求类型GET,PUT,POST
- MyBatis整合Spring的实现(2)
- MyBatis整合Spring的实现(3)
- MyBatis整合Spring的实现(4)
- MyBatis整合Spring的实现(5)
- MyBatis整合Spring的实现(6)
- Swift引入Header.h文件
- OC17内存管理和自动引用计数
- JAVA动态代理的理解
- 创业的第五十七天
- JavaScript相等操作符
- JavaScript 控制结构
- Ember.js 入门指南——自定义序列化器
- CoreData 持久化数据存储的注意点
- 【Monkey Run】Excel编程 VBA