1. 程式人生 > >springboot~rabbitmq的佇列初始化和繫結

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);
  }

}