springamqp的exchange
2016-05-20 10:27
381 查看
参考博客地址:http://www.infoq.com/cn/articles/AMQP-RabbitMQ/、http://www.open-open.com/lib/view/open1453350095355.html
rabbitmq安装地址:http://blog.csdn.net/xia744510124/article/details/51226310
exchange:是一个交换器,负责将生产者生产的消息,选择性交给与它绑定哪个queue,然后通过queue由消费者消费queue中的消息。
exchange的类型有四种:direct、fanout、topic、header
direct:直接交换器,工作方式类似于单播,Exchange会将消息发送完全匹配ROUTING_KEY的Queue
fanout:广播是式交换器,不管消息的ROUTING_KEY设置为什么,Exchange都会将消息转发给所有绑定的queue
topic:交换器用过模式匹配分析消息的routing-key属性。它将routing-key和binding-key的字符串切分成单词。这些单词之间用点隔开。它同样也会识别两个通配符:#匹配0个或者多个单词,*匹配一个单词(记住单词和点分割符)
headers:消息体的header匹配(忽略)
依赖包
direct交换方式配置文件
在applicaionContext_1.xml中
测试代码
代码没执行前Rabbitmq:
可以看到没有任何queue
只存在默认的exchange,没有我们定义名为remoting.exchange的exchange
代码执行后的Rabbitmq:
生成了我们定义的hello、world queue,并队列里面有了一条消息?生产者发布消息的时候会带一个routing-key到指定的exchange,让后exchange根据binding-key和routing-key匹配关系,将消息发到匹配的queue中去
生成了direct.exchange
topic交换方式配置文件
测试代码:
接着direct运行结果再次运行程序
两个队列又多了一条消息?hello的binding-key为he.*,world的binding-key为he.#,发布消息传的routing-key为he.he,显然hello,world队列都能匹配上,所以exchange会把消息都传给它们
生成了topic.exchange并且type为topic
fanout与header就不演示了!
rabbitmq安装地址:http://blog.csdn.net/xia744510124/article/details/51226310
exchange:是一个交换器,负责将生产者生产的消息,选择性交给与它绑定哪个queue,然后通过queue由消费者消费queue中的消息。
exchange的类型有四种:direct、fanout、topic、header
direct:直接交换器,工作方式类似于单播,Exchange会将消息发送完全匹配ROUTING_KEY的Queue
fanout:广播是式交换器,不管消息的ROUTING_KEY设置为什么,Exchange都会将消息转发给所有绑定的queue
topic:交换器用过模式匹配分析消息的routing-key属性。它将routing-key和binding-key的字符串切分成单词。这些单词之间用点隔开。它同样也会识别两个通配符:#匹配0个或者多个单词,*匹配一个单词(记住单词和点分割符)
headers:消息体的header匹配(忽略)
依赖包
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.5.6.RELEASE</version> </dependency>
direct交换方式配置文件
在applicaionContext_1.xml中
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd"> <rabbit:connection-factory id="connectionFactory" host="xxxxxx" port="5672" username="xxxxxx" password="xxxxxx" /> <!-- <property name="channelCacheSize" value="25"/> --> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" reply-timeout="2000" /> <rabbit:admin connection-factory="connectionFactory" /> <rabbit:queue name="hello" /> <rabbit:queue name="world" /> <rabbit:direct-exchange name="direct.exchange"> <rabbit:bindings> <!--将hello与exchange绑定,并指定一个binding-key--> <rabbit:binding queue="hello" key="binding" /> <!--将world与exchange绑定,并指定一个binding-key--> <rabbit:binding queue="world" key="binding" /> </rabbit:bindings> </rabbit:direct-exchange> </beans>
测试代码
@Test public void testWithNameAndPassword() { ApplicationContext acc = new ClassPathXmlApplicationContext("applicationContext_1.xml"); AmqpTemplate template = acc.getBean(AmqpTemplate.class); //发送消息并指定exchange和routing-key template.send("direct.exchange", "binding", new Message("nihao".getBytes(), new MessageProperties())); ((AbstractApplicationContext) acc).destroy(); }
代码没执行前Rabbitmq:
可以看到没有任何queue
只存在默认的exchange,没有我们定义名为remoting.exchange的exchange
代码执行后的Rabbitmq:
生成了我们定义的hello、world queue,并队列里面有了一条消息?生产者发布消息的时候会带一个routing-key到指定的exchange,让后exchange根据binding-key和routing-key匹配关系,将消息发到匹配的queue中去
生成了direct.exchange
topic交换方式配置文件
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd"> <rabbit:connection-factory id="connectionFactory" host="xxxxxx" port="5672" username="xxxxxx" password="xxxxxx" /> <!-- <property name="channelCacheSize" value="25"/> --> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" reply-timeout="2000" /> <rabbit:admin connection-factory="connectionFactory" /> <rabbit:queue name="hello" /> <rabbit:queue name="world" /> <rabbit:topic-exchange name="topic.exchange"> <rabbit:bindings> <!--绑定queue hello和匹配模式 *匹配一个单词--> <rabbit:binding queue="hello" pattern="he.*" /> <!--绑定queue world和匹配模式 #匹配多个单词--> <rabbit:binding queue="world" pattern="he.#" /> </rabbit:bindings> </rabbit:topic-exchange> </beans>
测试代码:
@Test public void testWithNameAndPassword() { @SuppressWarnings("resource") ApplicationContext acc = new ClassPathXmlApplicationContext("applicationContext_1.xml"); AmqpTemplate template = acc.getBean(AmqpTemplate.class); template.send("topic.exchange", "he.he", new Message("nihao".getBytes(), new MessageProperties())); ((AbstractApplicationContext) acc).destroy(); }
接着direct运行结果再次运行程序
两个队列又多了一条消息?hello的binding-key为he.*,world的binding-key为he.#,发布消息传的routing-key为he.he,显然hello,world队列都能匹配上,所以exchange会把消息都传给它们
生成了topic.exchange并且type为topic
fanout与header就不演示了!
相关文章推荐
- Rabbitmq集群搭建笔记
- Windows Server 2008 R2上部署Exchange Server 2010图文教程
- 利用Python学习RabbitMQ消息队列
- python使用rabbitmq实现网络爬虫示例
- Linux下PHP扩展amqp安装
- EJB3.0 JBoss的JMS实例
- rabbitmq学习
- SMTPClient连接EXCHANGE2007发送邮件
- 在 Exchange 资源林拓扑中部署 Exchange 2010
- Exchange邮件服务器中的部署更改
- Exchange公用文件夹恢复及常见问题
- Exchange 分析器工具介绍及案例分析
- Exchange动态同步中的INTERNET_29错误代码
- Exchange Server 2003的虚拟服务器中使用证书
- Exchange 2003 使用队列查看器的秘籍
- Exchange Server退信(NDR)及解决方案总汇
- Exchange中处理地址冲突的邮件
- Exchange Server灾难恢复的黄金三招
- 学习Exchange管理最佳实践