文章目录
- 1.目录结构
- 2.代码
- 1.pom.xml 排除logging
- 2.RabbitMQConfig.java
- 3.RabbitMQAutoConfiguration.java
1.目录结构
2.代码
1.pom.xml 排除logging
<?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><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
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;
@Configuration
@AutoConfigureBefore(RabbitAutoConfiguration.class)
public class RabbitMQConfig {@Beanpublic MessageConverter customMessageConverter() {return new MessageConverter() {private final Jackson2JsonMessageConverter jacksonConverter = new Jackson2JsonMessageConverter();private final SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();@Overridepublic Message toMessage(Object object, MessageProperties messageProperties) throws RuntimeException {if (object instanceof String) {messageProperties.setContentType("text/plain");return simpleMessageConverter.toMessage(object, messageProperties);} else {messageProperties.setContentType("application/json");return jacksonConverter.toMessage(object, messageProperties);}}@Overridepublic Object fromMessage(Message message) throws RuntimeException {String contentType = message.getMessageProperties().getContentType();if ("application/json".equals(contentType)) {return jacksonConverter.fromMessage(message);} else if ("text/plain".equals(contentType)) {return simpleMessageConverter.fromMessage(message);} else {throw new RuntimeException("自定义的消息转换器不支持该类型: " + contentType);}}};}@Beanpublic RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);rabbitTemplate.setMessageConverter(customMessageConverter());return rabbitTemplate;}
}
3.RabbitMQAutoConfiguration.java
package com.sunxiansheng.rabbitmq.config;import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(RabbitMQConfig.class)
public class RabbitMQAutoConfiguration {
}