rabbitmq RabbitAdmin

<dependency>
   <groupId>com.rabbitmq</groupId>
   <artifactId>amqp-client</artifactId>
   <version>3.6.5</version>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
@Configuration
@ComponentScan({"com.bfxy.spring.*"})
public class RabbitMQConfig {

   @Bean
   public ConnectionFactory connectionFactory(){
      CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
      connectionFactory.setAddresses("192.168.6.1:5672");
      connectionFactory.setUsername("longlong");
      connectionFactory.setPassword("");
      connectionFactory.setVirtualHost("/");
      return connectionFactory;
   }
   
   @Bean
   public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
      RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
      rabbitAdmin.setAutoStartup(true);//autoStartup必须设置true,否则Spring容器不会加载RabbitAdmin类
      return rabbitAdmin;
   }
}
@Test
public void testAdmin() throws Exception {
   rabbitAdmin.declareExchange(new DirectExchange("test.direct", false, false));
   
   rabbitAdmin.declareExchange(new TopicExchange("test.topic", false, false));
   
   rabbitAdmin.declareExchange(new FanoutExchange("test.fanout", false, false));
   
   rabbitAdmin.declareQueue(new Queue("test.direct.queue", false));
   
   rabbitAdmin.declareQueue(new Queue("test.topic.queue", false));
   
   rabbitAdmin.declareQueue(new Queue("test.fanout.queue", false));
   
   rabbitAdmin.declareBinding(new Binding("test.direct.queue",
         Binding.DestinationType.QUEUE,
         "test.direct", "direct", new HashMap<>()));
   
   rabbitAdmin.declareBinding(
         BindingBuilder
         .bind(new Queue("test.topic.queue", false))       //直接创建队列
         .to(new TopicExchange("test.topic", false, false)) //直接创建交换机 建立关联关系
         .with("user.#"));  //指定路由Key
   
   
   rabbitAdmin.declareBinding(
         BindingBuilder
         .bind(new Queue("test.fanout.queue", false))      
         .to(new FanoutExchange("test.fanout", false, false)));
   
   //清空队列数据
   rabbitAdmin.purgeQueue("dlx.queue", false);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值