当前位置:   article > 正文

RabbitMQ - Spring配置 - topic《通配符模式》_springmvc配置rabbitmq topic主题模式通配符

springmvc配置rabbitmq topic主题模式通配符

 SpringMain_Topic.java

  1. package com.xzp.rabbitmq.spring.topic;
  2. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  3. import org.springframework.context.support.AbstractApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. /**
  6. * main程序
  7. * 交换机为- 》topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》
  8. */
  9. public class SpringMain_Topic {
  10. public static void main(final String... args) throws Exception {
  11. AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/rabbitmq-context-fanout.xml");
  12. //RabbitMQ模板
  13. //RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
  14. RabbitTemplate amqpTemplate01 = (RabbitTemplate) ctx.getBean("amqpTemplate_fanout");
  15. RabbitTemplate amqpTemplate02 = (RabbitTemplate) ctx.getBean("amqpTemplate_direct");
  16. RabbitTemplate amqpTemplate03 = (RabbitTemplate) ctx.getBean("amqpTemplate_topic");
  17. //发送消息
  18. String str01 = "Hello, world!, This exchange is fanout !!";
  19. amqpTemplate01.convertAndSend(str01);
  20. System.out.println();
  21. System.out.println("发送消息: " + str01);
  22. System.out.println();
  23. //发送消息
  24. String str02 = "Hello, world!, This exchange is direct !!";
  25. amqpTemplate02.convertAndSend(str02);
  26. System.out.println();
  27. System.out.println("发送消息: " + str02);
  28. System.out.println();
  29. //发送消息
  30. String str03 = "Hello, world!, This exchange is topic !!";
  31. amqpTemplate03.convertAndSend(str03);
  32. System.out.println();
  33. System.out.println("发送消息: " + str03);
  34. System.out.println();
  35. // 休眠1秒
  36. Thread.sleep(1000);
  37. //容器销毁
  38. ctx.destroy();
  39. }
  40. }

 Customer01.java

  1. package com.xzp.rabbitmq.spring.topic;
  2. /**
  3. * 消费者1
  4. * 交换机为- 》topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》
  5. */
  6. public class Customer01 {
  7. //具体执行业务的方法
  8. public void listen(String foo) {
  9. System.out.println();
  10. System.out.println();
  11. System.out.println("topic 消费者 Customer01: " + foo);
  12. System.out.println();
  13. System.out.println();
  14. }
  15. }

Customer02.java

  1. package com.xzp.rabbitmq.spring.topic;
  2. /**
  3. * 消费者2
  4. * 交换机为- 》topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》
  5. */
  6. public class Customer02 {
  7. //具体执行业务的方法
  8. public void listen(String foo) {
  9. System.out.println();
  10. System.out.println();
  11. System.out.println("topic 消费者 Customer02: " + foo);
  12. System.out.println();
  13. System.out.println();
  14. }
  15. }

rabbitmq-context-fanout.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  3. xsi:schemaLocation="http://www.springframework.org/schema/rabbit
  4. http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
  7. <!--定义连接工厂-->
  8. <rabbit:connection-factory id="connectionFactory" host="127.0.0.1" port="5672" username="baseup" password="wukong@123" virtual-host="/baseup" />
  9. <!-- MQ的管理,包括队列、交换器等 -->
  10. <rabbit:admin id="rabbitAdmin_fanout" connection-factory="connectionFactory" />
  11. <rabbit:admin id="rabbitAdmin_direct" connection-factory="connectionFactory" />
  12. <rabbit:admin id="rabbitAdmin_topic" connection-factory="connectionFactory" />
  13. <!-- 定义Rabbit模板,指定连接工厂以及定义exchange -->
  14. <rabbit:template id="amqpTemplate_fanout" connection-factory="connectionFactory" exchange="exchange_setting_fanout" />
  15. <rabbit:template id="amqpTemplate_direct" connection-factory="connectionFactory" exchange="exchange_setting_direct" routing-key="wh.public" />
  16. <rabbit:template id="amqpTemplate_topic" connection-factory="connectionFactory" exchange="exchange_setting_topic" routing-key="wh.public.news" />
  17. <!-- 定义队列,自动声明 -->
  18. <rabbit:queue name="exchange_setting_fanout_queue01" auto-declare="true" declared-by="rabbitAdmin_fanout" />
  19. <rabbit:queue name="exchange_setting_fanout_queue02" auto-declare="true" declared-by="rabbitAdmin_fanout" />
  20. <rabbit:queue name="exchange_setting_direct_queue01" auto-declare="true" declared-by="rabbitAdmin_direct" />
  21. <rabbit:queue name="exchange_setting_direct_queue02" auto-declare="true" declared-by="rabbitAdmin_direct" />
  22. <rabbit:queue name="exchange_setting_topic_queue01" auto-declare="true" declared-by="rabbitAdmin_topic" />
  23. <rabbit:queue name="exchange_setting_topic_queue02" auto-declare="true" declared-by="rabbitAdmin_topic" />
  24. <!-- 定义交换器,自动声明 -->
  25. <rabbit:fanout-exchange name="exchange_setting_fanout" auto-declare="true" declared-by="rabbitAdmin_fanout">
  26. <rabbit:bindings>
  27. <rabbit:binding queue="exchange_setting_fanout_queue01" />
  28. <rabbit:binding queue="exchange_setting_fanout_queue02" />
  29. </rabbit:bindings>
  30. </rabbit:fanout-exchange>
  31. <rabbit:direct-exchange name="exchange_setting_direct" auto-declare="true" declared-by="rabbitAdmin_direct">
  32. <rabbit:bindings>
  33. <rabbit:binding queue="exchange_setting_direct_queue01" key="wh.public" />
  34. <rabbit:binding queue="exchange_setting_direct_queue02" key="wh.public" />
  35. </rabbit:bindings>
  36. </rabbit:direct-exchange>
  37. <rabbit:topic-exchange name="exchange_setting_topic" auto-declare="true" declared-by="rabbitAdmin_topic">
  38. <rabbit:bindings>
  39. <rabbit:binding queue="exchange_setting_topic_queue01" pattern="wh.*.news" />
  40. <rabbit:binding queue="exchange_setting_topic_queue02" pattern="wh.#" />
  41. </rabbit:bindings>
  42. </rabbit:topic-exchange>
  43. <!-- 队列监听 -->
  44. <rabbit:listener-container connection-factory="connectionFactory">
  45. <rabbit:listener ref="consumer01" method="listen" queue-names="exchange_setting_fanout_queue01" />
  46. <rabbit:listener ref="consumer02" method="listen" queue-names="exchange_setting_fanout_queue02" />
  47. </rabbit:listener-container>
  48. <rabbit:listener-container connection-factory="connectionFactory">
  49. <rabbit:listener ref="user01" method="listen" queue-names="exchange_setting_direct_queue01" />
  50. <rabbit:listener ref="user02" method="listen" queue-names="exchange_setting_direct_queue02" />
  51. </rabbit:listener-container>
  52. <rabbit:listener-container connection-factory="connectionFactory">
  53. <rabbit:listener ref="customer01" method="listen" queue-names="exchange_setting_topic_queue01" />
  54. <rabbit:listener ref="customer02" method="listen" queue-names="exchange_setting_topic_queue02" />
  55. </rabbit:listener-container>
  56. <!--定义消费Bean-->
  57. <bean id="consumer01" class="com.xzp.rabbitmq.spring.fanout.Consumer01" />
  58. <bean id="consumer02" class="com.xzp.rabbitmq.spring.fanout.Consumer02" />
  59. <bean id="user01" class="com.xzp.rabbitmq.spring.direct.User01" />
  60. <bean id="user02" class="com.xzp.rabbitmq.spring.direct.User02" />
  61. <bean id="customer01" class="com.xzp.rabbitmq.spring.topic.Customer01" />
  62. <bean id="customer02" class="com.xzp.rabbitmq.spring.topic.Customer02" />
  63. </beans>

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/139101
推荐阅读
相关标签
  

闽ICP备14008679号