SpringBoot + RabbitMQ 简单实现创建队列的三种方式

2018/11 20 15:11

1、前提:安装:RabbitMQ 通过Docker方式快速安装 

2、pox.xml 引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-cloud-starter-amqp</artifactId>
</dependency>

3、application.yml 配置

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

4、三种方式代码

package com.macs.order.message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 接收mq消息
 * @author antma
 */
@Slf4j
@Component
public class MqReceiver {

    //1. 手动创建,需在RabbitMQ中手动创建myQueue1 队列,否则报错
    @RabbitListener(queues = "myQueue1")
    public void process1(String message){
        log.info("MqReceiver1: {}", message);
    }

    //2. 自动创建队列
    @RabbitListener(queuesToDeclare = @Queue("myQueue2"))
    public void process2(String message){
        log.info("MqReceiver2: {}", message);

    }

    //3. 自动创建队列,Exchange 与 Queue绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue3"),
            exchange = @Exchange("testExChange")
    ))
    public void process3(String message){
        log.info("MqReceiver3: {}", message);
    }


//    @RabbitListener(bindings = @QueueBinding(
//            exchange = @Exchange("myOrder"),
//            key = "computer",
//            value = @Queue("computerOrder")
//    ))
//    public void processComputer(String message){
//        log.info("computer MqReceiver: {}", message);
//
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            exchange = @Exchange("myOrder"),
//            key = "fruit",
//            value = @Queue("fruitOrder")
//    ))
//    public void processFruit(String message){
//        log.info("fruit MqReceiver: {}", message);
//
//    }

}

5、测试类

package com.macs.order;

import org.junit.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class MqSenderTest extends OrderApplicationTests {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Test
    public void send1(){
        amqpTemplate.convertAndSend("myQueue1", "1 now " + new Date());
    }

    @Test
    public void send2(){
        amqpTemplate.convertAndSend("myQueue2", "2 now " + new Date());
    }

    @Test
    public void send3(){
        amqpTemplate.convertAndSend("myQueue3", "3 now " + new Date());
    }

//    @Test
//    public void sendOrder(){
//        amqpTemplate.convertAndSend("myOrder","computer", "now " + new Date());
//    }
//
//
//    @Test
//    public void sendFruitOrder(){
//        amqpTemplate.convertAndSend("myOrder","fruit", "now " + new Date());
//    }
}

6、启动spring boot 项目,在RabbitMQ队列(Queues)中即可看到队列出现:

注意:第一种方式为手动创建队列,否则启动会报错:

org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: 
Failed to declare queue(s):[myQueue1]

可以看到 myQueue3 绑定了 testExChange ,如下图:

7、执行测试类的三个测试方法:

可以看到idea控制台输出可以分别接收到RabbitMQ消息。

在RabbitMQ控制台上看到有相应的队列并被消费:

--转载请注明: https://www.macs.vip/archives/51