首页 > 其他分享 >springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制

时间:2022-11-30 19:05:53浏览次数:40  
标签:RabbitTemplate springboot author bookName rabbitmq org import 序列化 public

1-引入spring-boot-starter-amqp

2-application.yml配置

3-测试RabbitMQ

    1--AmqpAdmin:管理组件

    2--RabbitTemplate:消息发送处理组件

==========================================================================================

1-新建工程

选中spring for rabbitmq和web模块springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_测试方法

2-RabbitMQ自动配置

  • RabbitAutoConfiguration
  • 自动配置了连接工厂ConnectionFactory
  • RabbitProperties封装了RabbitMQ的配置
  • RabbitTemplate :给RabbitMQ发送和接收消息
  • AmqpAdmin : RabbitMQ系统管理功能组件,创建和删除exchange,queue,binding

3-application.properties中

spring.rabbitmq.host=192.168.3.18
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#spring.rabbitmq.port=
#spring.rabbitmq.virtual-host=

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_其他_02

4-测试类中写入

package com.example.springbootamqp;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class SpringbootRabbitmqAmqpApplicationTests {
    
    @Autowired
    RabbitTemplate rabbitTemplate;

    /*
    1`单播(点对点)
     */
    @Test
    void contextLoads() {
        //Message需要自己构造一个;定义消息体内容和消息头
        //rabbitTemplate.send(exchange,routeKey,message);
        
        //object默认当成消息体,只需要传入要发送的对象,自动序列化发送给rabbitmq
        //rabbitTemplate.convertAndSend(exchange,routeKey,object);

        Map<String,Object> map = new HashMap<>();
        map.put("msg","这是第一个消息");
        map.put("data", Arrays.asList("helloword",123,true));
        rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
    }

}

5-运行测试类

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_spring_03

6-添加接收消息测试方法

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_json_04

    @Test
    public void receive(){
        Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
        System.out.println(o.getClass()); //查看对象类型
        System.out.println(o);//输出对象
    }

 

运行此测试类方法

 springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_json_05

收到之后队列里就没有这个数据了

7-如何将数据自动的转为json发送出去?

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_json_06

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_json_07

新建config包MyAMQPConfig配置类

package com.example.springbootamqp.config;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyAMQPConfig {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

运行发送消息测试方法

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_json_08

8-发送和接收自定义数据类型消息

新建bean包Book类

package com.example.springbootamqp.bean;

public class Book {
    private String bookName;
    private String author;

    public Book() {
    }

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

    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;
    }
}

修改测试方法

发送消息

    @Test
    void contextLoads() {
        rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",new Book("西游记","吴承恩"));
    }

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_json_09

反序列化接收消息

springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制_json_10

9-测试广播

添加测试方法

     /**
     * 广播
     */
    @Test
    public void sendMsg(){
        rabbitTemplate.convertAndSend("exchange.fanout","",new Book("三国演义","罗贯中"));
    }

 

标签:RabbitTemplate,springboot,author,bookName,rabbitmq,org,import,序列化,public
From: https://blog.51cto.com/u_12528551/5900182

相关文章

  • springboot消息之@RabbitListener&@EnableRabbit监听消息队列的内容
    1-新建service包BookService类packagecom.example.springbootamqp.service;importcom.example.springbootamqp.bean.Book;importorg.springframework.amqp.rabbit......
  • springboot消息之JMS&AMQP简介
    大多应用中,可通过消息服务中间件来提升系统异步通信`扩展解耦能力.    异步处理  应用解耦  流量削峰消息服务中有两个重要概念:消息代理和目的地(m......
  • springboot消息之RabbitMQ简介
    RabbitMQ是一个有erlang开发的AMQP(AavancedMessageQueueProtocol)的开源实现核心概念:Publisher :消息的生产者,也是一个向交换器发布消息的客户端应用程序Mess......
  • springboot消息之AmqpAdmin管理组件的使用
    如果在使用@RabbitListener`@EnableRabbit注解之前没有在rabbitmq管理界面上创建exchange和queue,绑定规则,那么就可以在程序中使用AmqpAdmin管理组件测试类中添加,......
  • springboot任务之定时任务
    1-service包下新建ScheduleService类packagecom.example.springboottask.service;importorg.springframework.scheduling.annotation.Scheduled;importorg.springf......
  • springboot检索之springboot整合springdata elasticsearch
    1-修改pom.xml释放出注释<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elastic......
  • springboot之整合druid&配置数据源监控
    1-mavenrepository中搜索druid,引入druid数据源<!--https://mvnrepository.com/artifact/com.alibaba/druid--><dependency><groupId>com.alibaba</groupId>......
  • 使用向导spring initializer快速创建springboot应用
    1、 选择项目包含的依赖场景,向导会联网创建springboot项目将需要的东西删掉打开pom.xml后可以看到文件内容已自动写入将java目录变为sourcesroot,resources变......
  • springboot配置之外部配置加载顺序
    SpringBoot也可以从以下位置加载配置:1、命令行配置,多个配置用空格分开,--配置项=值java-jarspringboot-02-config-0.0.1-SNAPSHOT.jar--server.port=80872、来自java:comp......
  • springboot配置之配置文件加载位置
    1、springboot启动会扫描以下位置的application.properties或者application.yml文件作为springboot的配置文件,按照优先级从高到低的顺序。所有位置的文件都会被加载,高优先级......