RabbitMQ模块新增消息转换器

文章目录

1.目录结构

2.代码

1.pom.xml 排除logging
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-common</artifactId>
        <version>1.0.5</version>
    </parent>

    <version>1.0.5</version>

    <artifactId>common-rabbitmq-starter</artifactId>

    <dependencies>
        <!-- 工具包 -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-tool-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>
2.RabbitMQConfig.java
java 复制代码
package com.sunxiansheng.rabbitmq.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description: RabbitMQ配置类
 *
 * @Author sun
 * @Create 2025/1/2 14:22
 * @Version 1.0
 */
@Configuration
@AutoConfigureBefore(RabbitAutoConfiguration.class) // 在RabbitAutoConfiguration之前加载,用于覆盖默认的RabbitMQ配置
public class RabbitMQConfig {

    /**
     * 自定义消息转换器,处理JSON和文本消息
     *
     * @return
     */
    @Bean
    public MessageConverter customMessageConverter() {
        return new MessageConverter() {
            private final Jackson2JsonMessageConverter jacksonConverter = new Jackson2JsonMessageConverter();
            private final SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();

            /**
             * 发送消息的时候触发
             * @param object
             * @param messageProperties
             * @return
             * @throws RuntimeException
             */
            @Override
            public Message toMessage(Object object, MessageProperties messageProperties) throws RuntimeException {
                if (object instanceof String) {
                    // 如果是字符串,设置为文本类型发送
                    messageProperties.setContentType("text/plain");
                    return simpleMessageConverter.toMessage(object, messageProperties);
                } else {
                    // 默认当作JSON类型处理
                    messageProperties.setContentType("application/json");
                    return jacksonConverter.toMessage(object, messageProperties);
                }
            }

            /**
             * 接收消息的时候触发
             * @param message
             * @return
             * @throws RuntimeException
             */
            @Override
            public Object fromMessage(Message message) throws RuntimeException {
                String contentType = message.getMessageProperties().getContentType();
                // 如果是json类型的数据,就将其自动反序列化为Java对象
                if ("application/json".equals(contentType)) {
                    return jacksonConverter.fromMessage(message);
                    // 如果是文本类型的数据,就将其转换为字符串
                } else if ("text/plain".equals(contentType)) {
                    return simpleMessageConverter.fromMessage(message);
                } else {
                    throw new RuntimeException("自定义的消息转换器不支持该类型: " + contentType);
                }
            }
        };
    }

    /**
     * 设置RabbitTemplate的消息转换器为自定义消息转换器
     *
     * @param connectionFactory
     * @return
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        // 使用自定义消息转换器
        rabbitTemplate.setMessageConverter(customMessageConverter());
        return rabbitTemplate;
    }
}
3.RabbitMQAutoConfiguration.java
java 复制代码
package com.sunxiansheng.rabbitmq.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Description: RabbitMQ自动配置类
 *
 * @Author sun
 * @Create 2024/12/31 18:44
 * @Version 1.0
 */
@Configuration
@Import(RabbitMQConfig.class)
public class RabbitMQAutoConfiguration {
}
相关推荐
^辞安12 小时前
RocketMQ为什么自研Nameserver而不用zookeeper?
分布式·zookeeper·rocketmq
在未来等你14 小时前
Kafka面试精讲 Day 8:日志清理与数据保留策略
大数据·分布式·面试·kafka·消息队列
poemyang15 小时前
“你还活着吗?” “我没死,只是网卡了!”——来自分布式世界的“生死契约”
分布式
echoyu.15 小时前
消息队列-初识kafka
java·分布式·后端·spring cloud·中间件·架构·kafka
明达智控技术16 小时前
MR30分布式I/O在面机装备中的应用
分布式·物联网·自动化
JAVA学习通19 小时前
【RabbitMQ】---RabbitMQ 工作流程和 web 界面介绍
分布式·rabbitmq
安卓开发者20 小时前
鸿蒙NEXT应用数据持久化全面解析:从用户首选项到分布式数据库
数据库·分布式·harmonyos
kong@react1 天前
springboot项目详细配置rabbitmq及使用rabbitmq完成评论功能
spring boot·rabbitmq·java-rabbitmq
JAVA学习通1 天前
【RabbitMQ】如何在 Ubuntu 安装 RabbitMQ
分布式·rabbitmq
Lansonli1 天前
大数据Spark(六十三):RDD-Resilient Distributed Dataset
大数据·分布式·spark