Springboot集成RabbitMQ一个完整案例

springboot 集成 RabbitMQ 非常简单,如果只是简单的使用配置非常少,springboot 提供了 spring-boot-starter-amqp 对消息各种支持。
1、配置pom文件,主要是添加spring-boot-starter-amqp的支持

org.springframework.boot spring-boot-starter-amqp

2、配置application.properties文件

配置rabbitmq的安装地址、端口以及账户信息

spring.application.name=spirng-boot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

3、配置队列

package com.zpc.rabbitmq;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
@Bean
public Queue queue() {
return new Queue(“q_hello”);
}
}

4、发送者

package com.zpc.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class HelloSender {
@Autowired
private AmqpTemplate rabbitTemplate;

public void send() {
    String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//24小时制
    String context = "hello " + date;
    System.out.println("Sender : " + context);
    //简单对列的情况下routingKey即为Q名
    this.rabbitTemplate.convertAndSend("q_hello", context);
}

}

5、接收者

package com.zpc.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = “q_hello”)
public class HelloReceiver {

@RabbitHandler
public void process(String hello) {
    System.out.println("Receiver  : " + hello);
}

}

6、测试

package com.zpc.rabbitmq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMqHelloTest {

@Autowired
private HelloSender helloSender;

@Test
public void hello() throws Exception {
    helloSender.send();
}

}

输出结果:
在这里插入图片描述
发送者已经发送消息,并且接收者已经成功获取到消息!
证明发送和接收操作是异步的

消息队列实现了消息发送方和接收方对消息的异步处理
为了更加真实地还原发送和接收这两个异步操作,我们先注释掉接收者:

//@Component
//@RabbitListener(queues = “q_hello”)
//public class HelloReceiver {
//
// @RabbitHandler
// public void process(String hello) {
// System.out.println("Receiver : " + hello);
// }
//}

这时,发送者发送的消息,会存储到名字为q_hello的队列里面,等待接收者来提取
再次运行测试方法
控制台输出:
在这里插入图片描述
这里只显示了发送方发送了消息,但是还没有接收方接收消息
打开RabbitMQ在线管理工具(如果还没下载可以根据教程下载使用:搭建RabbitMQ环境)
我们可以看到q_hello队列中有一条代取的消息,内容正是上面发送方发送的消息!
在这里插入图片描述
因为还没有接收方,所以消息一直存留在q_hello消息队列中,现在我们来把它取出来
这次,我们取消接收方的注释:

@Component
@RabbitListener(queues = “q_hello”)
public class HelloReceiver {

@RabbitHandler
public void process(String hello) {
    System.out.println("Receiver  : " + hello);
}

}

修改测试方法:
让发送方不再发送数据

@Test
public void hello() throws Exception {

// helloSender.send();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值