springamqp的exchange

参考博客地址: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匹配(忽略)
依赖包

<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就不演示了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值