Spring Boot整合Rabbit MQ

本文详细介绍如何在Spring Boot项目中整合RabbitMQ,包括依赖引入、单元测试配置、消息发送与接收的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先引入依赖:

[XML] 纯文本查看 复制代码
?
1
2
3
4
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>



使用的Spring Boot的版本是:2.0.4.RELEASE


由于Spring Boot跑单元测试的时候,也需要一个Spring Boot Application,因此我们手动构造一个,并且加入@EnableRabbit注解,
启动监听器。

 

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
 
/**
 * 由于是基于spring boot test组件进行单元测试,需要构建一个TestApplication上下文
 */
@SpringBootApplication
@EnableRabbit
public class TestApplication {
 
    public static void main(String[] args){
        SpringApplicationBuilder builder = new SpringApplicationBuilder();
        builder.environment(new StandardEnvironment());
        builder.sources(TestApplication.class);
        builder.main(TestApplication.class);
        builder.run(args);
    }
}



直接编写发送消息的测试类:

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.junit.Test;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class SpringBootMqSendTest{
    private final static String EXCHANGE_NAME = "hello_fanout_1";
    private final static String QUEUE_NAME = "hello_queue_1";
 
    @Autowired
    private RabbitAdmin rabbitAdmin;
 
    @Autowired
    private RabbitTemplate rabbitTemplate;
 
    @Test
    public void testSend(){
        FanoutExchange fanoutExchange = new FanoutExchange(EXCHANGE_NAME, false, false);
        Queue queue = new Queue(QUEUE_NAME, false);
        rabbitAdmin.declareExchange(fanoutExchange);
        rabbitAdmin.declareQueue(queue);
        rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(fanoutExchange));
 
        Message message = new Message("hello world.".getBytes(), new MessageProperties());
        rabbitTemplate.send(EXCHANGE_NAME, "", message);
    }
}



编写消息消费者:

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
 
@Component
public class SpringBootMsqConsumer {
    @RabbitListener(queues = "hello_queue_1")
    public void receive(Message message) {
        System.out.println("receive message:" + new String(message.getBody()));
    }
}



编写消息测试类:

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
import org.junit.Test;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class SpringBootMqConsumerTest {
    @Autowired
    private SpringBootMsqConsumer springBootMsqConsumer;
 
    @Test
    public void testConsumer(){
    }
}



执行testConsumer方法后,控制台输出:receive message:hello world.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值