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 {
}
相关推荐
Chan163 小时前
【 SpringCloud | 微服务 MQ基础 】
java·spring·spring cloud·微服务·云原生·rabbitmq
小鸡脚来咯5 小时前
RabbitMQ入门
分布式·rabbitmq
qq_463944866 小时前
【Spark征服之路-2.2-安装部署Spark(二)】
大数据·分布式·spark
敖云岚7 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
正在努力Coding7 小时前
kafka(windows)
分布式·kafka
禺垣13 小时前
区块链技术概述
大数据·人工智能·分布式·物联网·去中心化·区块链
zhuhit16 小时前
FASTDDS的安全设计
分布式·机器人·嵌入式
暗影八度16 小时前
Spark流水线+Gravitino+Marquez数据血缘采集
大数据·分布式·spark
q5673152317 小时前
IBM官网新闻爬虫代码示例
开发语言·分布式·爬虫
不爱学英文的码字机器17 小时前
数据网格的革命:从集中式到分布式的数据管理新范式
分布式