Rabbitmq与spring整合之重要组件介绍——rabbitAdmin组件

  rabbitAdmin组件是一个管理组件,主要是用户通过该组件进行rabbitmq的队列交换器虚拟主机等等进行操作。这里面有些教程说不用声明可以直接绑定,但是本博主运行时,不生命情况下就会报错,可能是跟所采用的版本有关。

不通过属性文件配置

配置类

 1 package com.zxy.demo.config;
 2 
 3 import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
 4 import org.springframework.amqp.rabbit.connection.ConnectionFactory;
 5 import org.springframework.amqp.rabbit.core.RabbitAdmin;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.ComponentScan;
 8 import org.springframework.context.annotation.Configuration;
 9 
10 
11 @Configuration
12 @ComponentScan(basePackages= {"com.zxy.demo.*"})
13 public class RabbitmqCofing {
14     @Bean
15     public ConnectionFactory connectionFactory() {
16         CachingConnectionFactory connection = new CachingConnectionFactory();
17         connection.setAddresses("192.168.10.110:5672");
18         connection.setUsername("guest");
19         connection.setPassword("guest");
20         connection.setVirtualHost("/");
21         return connection;
22     }
23     @Bean
24     public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
25         RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
26         //用RabbitAdmin一定要配置这个,spring加载的是后就会加载这个类================特别重要
27         rabbitAdmin.setAutoStartup(true);
28         return rabbitAdmin;
29     }
30 }

测试类:

 1 package com.zxy.demo;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.amqp.core.Binding;
 6 import org.springframework.amqp.core.BindingBuilder;
 7 import org.springframework.amqp.core.DirectExchange;
 8 import org.springframework.amqp.core.FanoutExchange;
 9 import org.springframework.amqp.core.Queue;
10 import org.springframework.amqp.core.TopicExchange;
11 import org.springframework.amqp.rabbit.core.RabbitAdmin;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.boot.test.context.SpringBootTest;
14 import org.springframework.test.context.junit4.SpringRunner;
15 
16 @RunWith(SpringRunner.class)
17 @SpringBootTest
18 public class ForwardApplicationTests {
19 
20     @Test
21     public void contextLoads() {
22     }
23     @Autowired
24     private RabbitAdmin rabbitAdmin;
25     @Test
26     public void testAdmin() {
27 //        切记命名不能重复复
28         rabbitAdmin.declareQueue(new Queue("test.direct.queue"));
29         rabbitAdmin.declareExchange(new DirectExchange("test.direct"));
30         rabbitAdmin.declareBinding(new Binding("test.direct.queue", Binding.DestinationType.QUEUE, "test.direct", "mq.direct", null));
31 
32         rabbitAdmin.declareQueue(new Queue("test.topic.queue", true,false, false));
33         rabbitAdmin.declareExchange(new TopicExchange("test.topic", true,false));
34 //        如果注释掉上面两句实现声明,直接进行下面的绑定竟然不行,该版本amqp-client采用的是5.1.2,将上面两行代码放开,则运行成功
35         rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.topic.queue", true,false, false))
36                 .to(new TopicExchange("test.topic", true,false)).with("mq.topic"));
37 //        经过实验确实是需要先声明,才可以运行通过
38         rabbitAdmin.declareQueue(new Queue("test.fanout.queue",true,false,false,null));
39         rabbitAdmin.declareExchange(new FanoutExchange("test.fanout", true, false, null));
40         rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.fanout.queue", true, false,false))
41                 .to(new FanoutExchange("test.fanout", true, false)));
42         rabbitAdmin.purgeQueue("test.direct.queue", false);//清空队列消息
43     }
44 }

 

posted @ 2018-09-08 17:50  醉逍遥_001  阅读(623)  评论(0编辑  收藏  举报