zoukankan      html  css  js  c++  java
  • springboot~rabbitmq的队列初始化和绑定

    配置文件,在rabbit中自动建立exchange,queue和绑定它们的关系

    1. 代码里初始化exchange
    2. 代码里初始化queue
    3. 代码里绑定exchange,queue和routekey
    4. 配置文件,直接声明vhost

    代码里初始化exchange

       /**
       * rabbitMq里初始化exchange.
       *
       * @return
       */
      @Bean
      public TopicExchange crmExchange() {
        return new TopicExchange(EXCHANGE);
      }
    

    代码里初始化queue

      /**
       * rabbitMq里初始化队列crm.hello.
       *
       * @return
       */
      @Bean
      public Queue helloQueue() {
        return new Queue(HELLO);
      }
    

    代码里绑定exchange,queue和routekey

      /**
       * 绑定exchange & queue & routekey.
       *
       * @param queueMessage 队列
       * @param exchange     交换机
       * @param routekey     路由
       * @return
       */
      public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
        return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
      }
    

    配置文件

    spring:
        rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
        virtual-host: lind
    

    完整代码

    package com.lind.microservice.productCenter.mq;
    
    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.Queue;
    import org.springframework.amqp.core.TopicExchange;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * amqp配置.
     */
    @Configuration
    public class AmqpConfig {
    
      /**
       * 交换机.
       */
      public final static String EXCHANGE = "crm";
      /**
       * hello队列.
       */
      public final static String HELLO = "crm.hello";
      /**
       * 建立订单队列.
       */
      public final static String LIND_GENERATE_ORDER = "crm.generate.order";
    
    
      /**
       * 绑定exchange & queue & routekey.
       *
       * @param queueMessage 队列
       * @param exchange     交换机
       * @param routekey     路由
       * @return
       */
      public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
        return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
      }
    
    
      /**
       * rabbitMq里初始化exchange.
       *
       * @return
       */
      @Bean
      public TopicExchange crmExchange() {
        return new TopicExchange(EXCHANGE);
      }
    
      /**
       * rabbitMq里初始化队列crm.hello.
       *
       * @return
       */
      @Bean
      public Queue helloQueue() {
        return new Queue(HELLO);
      }
    
      /**
       * rabbitMq里初始化队列crm.generate.order.
       *
       * @return
       */
      @Bean
      public Queue orderQueue() {
        return new Queue(LIND_GENERATE_ORDER);
      }
    
    }
    
    

    队列发布者

    package com.lind.microservice.productCenter.mq;
    
    import java.util.Date;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class HelloPublisher {
      @Autowired
      AmqpTemplate rabbitTemplate;
      @Autowired
      AmqpConfig amqpConfig;
    
      public void hello() {
        String context = "hello " + new Date();
        System.out.println("HelloPublisher : " + context);
        amqpConfig.bindingExchange(
            amqpConfig.helloQueue(),
            amqpConfig.crmExchange(),
            "crm.hello.#"
        );
        this.rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.HELLO, context);
      }
    
    
    }
    
    

    队列订阅者

    package com.lind.microservice.productCenter.mq;
    
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    @Component
    @RabbitListener(queues = AmqpConfig.HELLO)
    public class HelloSubscriber {
      @RabbitHandler
      public void process(String hello) {
        System.out.println("HelloSubscriber  : " + hello);
      }
    
    }
    
    
  • 相关阅读:
    tshark的命令使用
    svn log查看自己提交的记录
    账号安全 syyh
    Trivy 容器镜像扫描工具学习 syyh
    《关键对话》脑图整理 syyh
    Grafana 任意文件读取漏洞 (CVE202143798)学习 syyh
    容器安全管理 syyh
    【Sass/SCSS】我花4小时整理了的Sass的函数
    【JavaScript使用技巧】三个截取字符串中的子串,你用的哪个
    【SVG】为了前端页面的美丽,我选择学习SVG
  • 原文地址:https://www.cnblogs.com/lori/p/9739130.html
Copyright © 2011-2022 走看看