SpringBoot日记——MQ消息队列整合(二)

  基于第一篇文章搭建好环境以后,我们这篇文章继续介绍如何在springboot中使用RabbitMQ。

  1)、单播:添加好pom文件和自定义配置后,来看:

复制代码
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void contextLoads() {

        // 对象被默认JAVA序列化发送,参数:Exchange,routingKey,消息
        rabbitTemplate.convertAndSend("exchange.direct", "iceoooodin.news", "瓦尔克莉");
    }
复制代码

  来看看我们发送的消息是否成功了:

  如图,成功获取了~。

  同样,使用代码来直接获取:

    @Test
    public void receive() {
        // 接受数据
        Object o = rabbitTemplate.receiveAndConvert("iceoooodin.news");
        System.out.println(o.getClass());
        System.out.println(o);
    }

  另外,我们除了str类型,还是发送map、对象等等,比如:

复制代码
    @Test
    public void contextLoads() {

        Map<String, Object> map = new HashMap<>();
        map.put("msg", "第一个数据");
        map.put("data", Arrays.asList("helloworld", 123, true));
        // 对象被默认JAVA序列化发送
        rabbitTemplate.convertAndSend("exchange.direct", "iceoooodin.news", map);
    }
复制代码
@Test
    public void contextLoads() {

        // 对象被默认JAVA序列化发送
        rabbitTemplate.convertAndSend("exchange.direct", "iceoooodin.news", new Book("金瓶M", "em.."));
    }
复制代码
public class Book {
    private String bookName;
    private String author;

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public Book(String bookName, String author) {
        this.bookName = bookName;
        this.author = author;
    }

    public Book() {
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}
Book.java
复制代码

   2)、然后我们来看广播,也就是发送一堆消息是如何处理的:

@Test
    public void sendMst() {
        rabbitTemplate.convertAndSend("exchange.fanout", "", new Book("红楼", "草 "));
    }

  如果发送的是广播,可以发现,消息会被分别发送到匹配的全部消息队列中:

  3)、我们学会发送和接收了,再看看如何创建Exchange或者Queue吧,我们创建和绑定写在了一起,根据需要自己拆:

复制代码
    @Autowired
    AmqpAdmin amqpAdmin;  //这个amqpadmin是用来管理QP的,可以创建、删除等操作;

    @Test
    public void creatExchange() {
        // 创建Exchange
        amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
        System.out.println("创建exchange完成");
        // 创建Queue
        amqpAdmin.declareQueue(new Queue("amqpadmin.queue", true));
     System.out.println("创建Queue完成");
// 绑定 amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE, "amqpadmin.exchange", "amqp.haha", null)); }
复制代码

   结果就懒得写了,大家自己试验看一下就知道了,到web管理界面看看是否有正确添加和绑定~

posted @   碎冰  阅读(1060)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2017-08-22 浅谈测试方向——转职?性能、自动化、测开?还是产品、运营……
点击右上角即可分享
微信分享提示