当前位置:   article > 正文

RabbitMq创建队列绑定交换机_rabbitmq 绑定交换机

rabbitmq 绑定交换机

声明交换机

@Bean
public TopicExchange exchange() {
    return new TopicExchange(exchange);
}

声明队列

@Bean
public Queue delayedQueue(){
    return new Queue(queue);
}
@Bean
public Queue delayedQueue1(){
    return new Queue(queue1);
}

队列绑定交换机

@Bean
public Binding queueBindingExchange(@Qualifier("delayedQueue") Queue queue,
                                    @Qualifier("exchange") Exchange exchange){
    return BindingBuilder.bind(queue).to(exchange).with("#").noargs();
}

配置文件:

rabbitmq:
  exchange: 交换器名称
  host: ip地址
  port: 端口号 默认5672
  username: 用户名
  password: 密码
  virtualHost: /
  queue: 队列名称

完整代码:

  1. @Configuration
  2. public class RabbitmqConfig {
  3. @Value("${rabbitmq.exchange}")
  4. private String exchange;
  5. @Value("${rabbitmq.host}")
  6. private String host;
  7. @Value("${rabbitmq.port}")
  8. private Integer port;
  9. @Value("${rabbitmq.username}")
  10. private String username;
  11. @Value("${rabbitmq.password}")
  12. private String password;
  13. @Value("${rabbitmq.virtual-host}")
  14. private String virtualHost;
  15. @Value("${rabbitmq.queue}")
  16. private String queue;
  17. @Bean
  18. public TopicExchange exchange() {
  19. return new TopicExchange(exchange);
  20. }
  21. @Bean
  22. public ConnectionFactory connectionFactory() {
  23. CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
  24. connectionFactory.setUsername(username);
  25. connectionFactory.setPassword(password);
  26. connectionFactory.setPort(port);
  27. connectionFactory.setVirtualHost(virtualHost);
  28. return connectionFactory;
  29. }
  30. @Bean
  31. public AmqpAdmin amqpAdmin() {
  32. return new RabbitAdmin(connectionFactory());
  33. }
  34. @Bean
  35. public RabbitTemplate rabbitTemplate() {
  36. return new RabbitTemplate(connectionFactory());
  37. }
  38. @Bean
  39. public Queue delayedQueue(){
  40. return new Queue(queue);
  41. }
  42. @Bean
  43. public Binding queueBindingExchange(@Qualifier("delayedQueue") Queue queue,
  44. @Qualifier("exchange") Exchange exchange){
  45. return BindingBuilder.bind(queue).to(exchange).with("#").noargs();
  46. }
  47. }

不管是本地还是虚拟机还是服务器部署了rabbitmq之后,启动服务:

访问:http://ip地址:15672/#/queues

查看队列:

查看交换机:

 

 查看队列和交换机绑定情况:

 

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

闽ICP备14008679号