RabbitMQ快速整合SpringBoot

  1. 導入springboot的rabbitmq依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. 配置文件
spring:
  rabbitmq:
   host: 127.0.0.1
   port: 5672
   username: guest
   password: guest
   virtualHost: /
  1. 生產者:rabbit配置類
    使用topic工作模式,注:RabbitMQ幾種工作模式介紹
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    /**
     * 隊列名
     */
    public static final String QUEUE_INFORM_A = "queue_inform_A";
    public static final String QUEUE_INFORM_B = "queue_inform_B";
    /**
     * 交換機名
     */
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    /**
     * 交換機配置
     * ExchangeBuilder提供了fanout、direct、topic、header交換機類型的配置
     *
     * @return the exchange
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM() {
        //durable(true)持久化,消息隊列重啓後交換機仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    /**
     * 聲明隊列
     *
     * @return
     */
    @Bean(QUEUE_INFORM_A)
    public Queue QUEUE_INFORM_A() {
        Queue queue = new Queue(QUEUE_INFORM_A);
        return queue;
    }

    /**
     * 聲明隊列
     *
     * @return
     */
    @Bean(QUEUE_INFORM_B)
    public Queue QUEUE_INFORM_B() {
        Queue queue = new Queue(QUEUE_INFORM_B);
        return queue;
    }

    /** channel.queueBind(INFORM_QUEUE_A,"inform_exchange_topic","inform.#.a.#");
     * 綁定隊列到交換機 .
     *
     * @param queue the queue
     * @param exchange the exchange
     * @return the binding
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_A) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.a.#").noargs();
    }

    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_B) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.b.#").noargs();
    }

}
  1. 生產者:發送消息測試
@SpringBootTest
@RunWith(SpringRunner.class)
public class RabbitMqProviderTest {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void testSendByTopics(){
        for (int i=0;i<100;i++){
            String message = "a inform to user"+i;
            rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_TOPICS_INFORM,"inform.a.b",message);
            System.out.println("Send Message is:'" + message + "'");
        }
    }

}

查看

  1. 消費者:編寫消費監聽器
@Component
public class RabbitHandler {

    /**
     * 監聽a隊列
     */
    @RabbitListener(queues = {RabbitConfig.QUEUE_INFORM_A})
    public void receiveA(String msg, Message message, Channel channel){
        System.out.println(msg);
    }

    /**
     * 監聽b隊列
     */
    @RabbitListener(queues = {RabbitConfig.QUEUE_INFORM_B})
    public void receiveB(String msg,Message message,Channel channel){
        System.out.println(msg);
    }

}

結果輸出
在這裏插入圖片描述


代碼地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章