From 89fc1ff8c48fabad2b1f88b75d5c520902594e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Sat, 7 May 2022 21:41:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(RabbitConfig.java):=20RabbitMQ=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=B6=88=E6=81=AF=E5=BA=8F=E5=88=97=E5=8C=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/rabbitmq/config/RabbitConfig.java | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitConfig.java b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitConfig.java index 6e0d9c909..1dfd8d16f 100644 --- a/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitConfig.java +++ b/youlai-common/common-rabbitmq/src/main/java/com/youlai/common/rabbitmq/config/RabbitConfig.java @@ -4,18 +4,13 @@ import com.youlai.common.rabbitmq.dynamic.RabbitModuleInitializer; import com.youlai.common.rabbitmq.dynamic.RabbitModuleProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.AmqpAdmin; -import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer; +import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; -import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar; +import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; -import org.springframework.amqp.support.converter.MessageConverter; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.messaging.converter.MappingJackson2MessageConverter; -import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory; -import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory; /** * @author huawei @@ -24,26 +19,24 @@ import org.springframework.messaging.handler.annotation.support.MessageHandlerMe */ @Configuration @Slf4j -public class RabbitConfig implements RabbitListenerConfigurer{ +public class RabbitConfig { - @Override - public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) { - rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory()); - } - - @Bean - MessageHandlerMethodFactory messageHandlerMethodFactory(){ - DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory(); - messageHandlerMethodFactory.setMessageConverter(mappingJackson2MessageConverter()); - return messageHandlerMethodFactory; - } - - @Bean - public MappingJackson2MessageConverter mappingJackson2MessageConverter() { - return new MappingJackson2MessageConverter(); - } /** + * 消息序列化配置 + * + * @param connectionFactory + * @return + */ + @Bean + public RabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) { + SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + factory.setMessageConverter(new Jackson2JsonMessageConverter()); + return factory; + } + + /* * 动态创建队列、交换机初始化器 * * @return