TopicConfig.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package com.sf.mq.rabbitmq.topic;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.Queue;
  5. import org.springframework.amqp.core.TopicExchange;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. @Configuration
  9. public class TopicConfig {
  10. // 通过bean的声明方式 来创建交换器
  11. // 声明交换器时 只需要类型和名字 直接使用对应类型的实现类FanoutExchange
  12. // 通过构造器指定名字
  13. @Bean
  14. public TopicExchange topicExchange() {
  15. return new TopicExchange("lovecoding.topic1");
  16. }
  17. // 通过bean的声明方式 来创建队列
  18. // 通过队列的构造器指定名字
  19. // 注意引用的包来自 org.springframework.amqp.core
  20. @Bean
  21. public Queue topicQueue() {
  22. return new Queue("topic.queue1");
  23. }
  24. // 绑定交换器和队列
  25. // 当前是fanout类型 不需要routingKey
  26. // 绑定关系叫做Binding类 通过BindingBuilder来构造绑定关系
  27. // bind方法后面加队列 再使用链式编程 to方法后面加交换器
  28. @Bean
  29. public Binding bindingTopic(Queue topicQueue, TopicExchange topicExchange) {
  30. return BindingBuilder.bind(topicQueue).to(topicExchange).with("*.*.harbin");
  31. }
  32. }