赞
踩
SpringMain_Topic.java
package com.xzp.rabbitmq.spring.topic; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * main程序 * 交换机为- 》topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》 */ public class SpringMain_Topic { public static void main(final String... args) throws Exception { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/rabbitmq-context-fanout.xml"); //RabbitMQ模板 //RabbitTemplate template = ctx.getBean(RabbitTemplate.class); RabbitTemplate amqpTemplate01 = (RabbitTemplate) ctx.getBean("amqpTemplate_fanout"); RabbitTemplate amqpTemplate02 = (RabbitTemplate) ctx.getBean("amqpTemplate_direct"); RabbitTemplate amqpTemplate03 = (RabbitTemplate) ctx.getBean("amqpTemplate_topic"); //发送消息 String str01 = "Hello, world!, This exchange is fanout !!"; amqpTemplate01.convertAndSend(str01); System.out.println(); System.out.println("发送消息: " + str01); System.out.println(); //发送消息 String str02 = "Hello, world!, This exchange is direct !!"; amqpTemplate02.convertAndSend(str02); System.out.println(); System.out.println("发送消息: " + str02); System.out.println(); //发送消息 String str03 = "Hello, world!, This exchange is topic !!"; amqpTemplate03.convertAndSend(str03); System.out.println(); System.out.println("发送消息: " + str03); System.out.println(); // 休眠1秒 Thread.sleep(1000); //容器销毁 ctx.destroy(); } }
Customer01.java
package com.xzp.rabbitmq.spring.topic; /** * 消费者1 * 交换机为- 》topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》 */ public class Customer01 { //具体执行业务的方法 public void listen(String foo) { System.out.println(); System.out.println(); System.out.println("topic 消费者 Customer01: " + foo); System.out.println(); System.out.println(); } }
Customer02.java
package com.xzp.rabbitmq.spring.topic; /** * 消费者2 * 交换机为- 》topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》 */ public class Customer02 { //具体执行业务的方法 public void listen(String foo) { System.out.println(); System.out.println(); System.out.println("topic 消费者 Customer02: " + foo); System.out.println(); System.out.println(); } }
rabbitmq-context-fanout.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
- xsi:schemaLocation="http://www.springframework.org/schema/rabbit
- http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
-
- <!--定义连接工厂-->
- <rabbit:connection-factory id="connectionFactory" host="127.0.0.1" port="5672" username="baseup" password="wukong@123" virtual-host="/baseup" />
-
- <!-- MQ的管理,包括队列、交换器等 -->
- <rabbit:admin id="rabbitAdmin_fanout" connection-factory="connectionFactory" />
- <rabbit:admin id="rabbitAdmin_direct" connection-factory="connectionFactory" />
- <rabbit:admin id="rabbitAdmin_topic" connection-factory="connectionFactory" />
-
- <!-- 定义Rabbit模板,指定连接工厂以及定义exchange -->
- <rabbit:template id="amqpTemplate_fanout" connection-factory="connectionFactory" exchange="exchange_setting_fanout" />
- <rabbit:template id="amqpTemplate_direct" connection-factory="connectionFactory" exchange="exchange_setting_direct" routing-key="wh.public" />
- <rabbit:template id="amqpTemplate_topic" connection-factory="connectionFactory" exchange="exchange_setting_topic" routing-key="wh.public.news" />
-
-
- <!-- 定义队列,自动声明 -->
- <rabbit:queue name="exchange_setting_fanout_queue01" auto-declare="true" declared-by="rabbitAdmin_fanout" />
- <rabbit:queue name="exchange_setting_fanout_queue02" auto-declare="true" declared-by="rabbitAdmin_fanout" />
-
- <rabbit:queue name="exchange_setting_direct_queue01" auto-declare="true" declared-by="rabbitAdmin_direct" />
- <rabbit:queue name="exchange_setting_direct_queue02" auto-declare="true" declared-by="rabbitAdmin_direct" />
-
- <rabbit:queue name="exchange_setting_topic_queue01" auto-declare="true" declared-by="rabbitAdmin_topic" />
- <rabbit:queue name="exchange_setting_topic_queue02" auto-declare="true" declared-by="rabbitAdmin_topic" />
-
- <!-- 定义交换器,自动声明 -->
- <rabbit:fanout-exchange name="exchange_setting_fanout" auto-declare="true" declared-by="rabbitAdmin_fanout">
- <rabbit:bindings>
- <rabbit:binding queue="exchange_setting_fanout_queue01" />
- <rabbit:binding queue="exchange_setting_fanout_queue02" />
- </rabbit:bindings>
- </rabbit:fanout-exchange>
-
- <rabbit:direct-exchange name="exchange_setting_direct" auto-declare="true" declared-by="rabbitAdmin_direct">
- <rabbit:bindings>
- <rabbit:binding queue="exchange_setting_direct_queue01" key="wh.public" />
- <rabbit:binding queue="exchange_setting_direct_queue02" key="wh.public" />
- </rabbit:bindings>
- </rabbit:direct-exchange>
-
- <rabbit:topic-exchange name="exchange_setting_topic" auto-declare="true" declared-by="rabbitAdmin_topic">
- <rabbit:bindings>
- <rabbit:binding queue="exchange_setting_topic_queue01" pattern="wh.*.news" />
- <rabbit:binding queue="exchange_setting_topic_queue02" pattern="wh.#" />
- </rabbit:bindings>
- </rabbit:topic-exchange>
-
- <!-- 队列监听 -->
- <rabbit:listener-container connection-factory="connectionFactory">
- <rabbit:listener ref="consumer01" method="listen" queue-names="exchange_setting_fanout_queue01" />
- <rabbit:listener ref="consumer02" method="listen" queue-names="exchange_setting_fanout_queue02" />
- </rabbit:listener-container>
-
-
- <rabbit:listener-container connection-factory="connectionFactory">
- <rabbit:listener ref="user01" method="listen" queue-names="exchange_setting_direct_queue01" />
- <rabbit:listener ref="user02" method="listen" queue-names="exchange_setting_direct_queue02" />
- </rabbit:listener-container>
-
- <rabbit:listener-container connection-factory="connectionFactory">
- <rabbit:listener ref="customer01" method="listen" queue-names="exchange_setting_topic_queue01" />
- <rabbit:listener ref="customer02" method="listen" queue-names="exchange_setting_topic_queue02" />
- </rabbit:listener-container>
-
- <!--定义消费Bean-->
- <bean id="consumer01" class="com.xzp.rabbitmq.spring.fanout.Consumer01" />
- <bean id="consumer02" class="com.xzp.rabbitmq.spring.fanout.Consumer02" />
-
- <bean id="user01" class="com.xzp.rabbitmq.spring.direct.User01" />
- <bean id="user02" class="com.xzp.rabbitmq.spring.direct.User02" />
-
- <bean id="customer01" class="com.xzp.rabbitmq.spring.topic.Customer01" />
- <bean id="customer02" class="com.xzp.rabbitmq.spring.topic.Customer02" />
-
- </beans>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。