You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2018/12/19 07:22:58 UTC

[GitHub] vongosling closed pull request #6: Rationalise use of ObjectMapper

vongosling closed pull request #6: Rationalise use of ObjectMapper
URL: https://github.com/apache/rocketmq-spring/pull/6
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/JacksonFallbackConfiguration.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/JacksonFallbackConfiguration.java
new file mode 100644
index 0000000..a66887a
--- /dev/null
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/JacksonFallbackConfiguration.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.spring.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@ConditionalOnMissingBean(ObjectMapper.class)
+class JacksonFallbackConfiguration {
+
+    @Bean
+    public ObjectMapper rocketMQMessageObjectMapper() {
+        return new ObjectMapper();
+    }
+
+}
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/ListenerContainerConfiguration.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/ListenerContainerConfiguration.java
index 9f78a19..625bae6 100644
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/ListenerContainerConfiguration.java
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/ListenerContainerConfiguration.java
@@ -28,7 +28,6 @@
 import org.springframework.aop.support.AopUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.SmartInitializingSingleton;
-import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.support.BeanDefinitionValidationException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
@@ -56,11 +55,10 @@
 
     private ObjectMapper objectMapper;
 
-    public ListenerContainerConfiguration(
-        @Qualifier("rocketMQMessageObjectMapper") ObjectMapper objectMapper,
+    public ListenerContainerConfiguration(ObjectMapper rocketMQMessageObjectMapper,
         StandardEnvironment environment,
         RocketMQProperties rocketMQProperties) {
-        this.objectMapper = objectMapper;
+        this.objectMapper = rocketMQMessageObjectMapper;
         this.environment = environment;
         this.rocketMQProperties = rocketMQProperties;
     }
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/RocketMQAutoConfiguration.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/RocketMQAutoConfiguration.java
index b65dee1..a2efeb6 100644
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/RocketMQAutoConfiguration.java
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/config/RocketMQAutoConfiguration.java
@@ -18,15 +18,16 @@
 package org.apache.rocketmq.spring.config;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.rocketmq.client.MQAdmin;
 import org.apache.rocketmq.client.producer.DefaultMQProducer;
 import org.apache.rocketmq.spring.core.RocketMQTemplate;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -34,12 +35,12 @@
 import org.springframework.context.annotation.Role;
 import org.springframework.util.Assert;
 
-import java.util.Objects;
-
 @Configuration
 @EnableConfigurationProperties(RocketMQProperties.class)
+@ConditionalOnClass({ MQAdmin.class, ObjectMapper.class })
 @ConditionalOnProperty(prefix = "spring.rocketmq", value = "name-server")
-@Import(ListenerContainerConfiguration.class)
+@Import({ JacksonFallbackConfiguration.class, ListenerContainerConfiguration.class })
+@AutoConfigureAfter(JacksonAutoConfiguration.class)
 public class RocketMQAutoConfiguration {
 
     @Bean
@@ -67,17 +68,10 @@ public DefaultMQProducer defaultMQProducer(RocketMQProperties rocketMQProperties
     @Bean(destroyMethod = "destroy")
     @ConditionalOnBean(DefaultMQProducer.class)
     @ConditionalOnMissingBean(RocketMQTemplate.class)
-    public RocketMQTemplate rocketMQTemplate(DefaultMQProducer mqProducer,
-        @Autowired(required = false)
-        @Qualifier("rocketMQMessageObjectMapper")
-            ObjectMapper objectMapper) {
+    public RocketMQTemplate rocketMQTemplate(DefaultMQProducer mqProducer, ObjectMapper rocketMQMessageObjectMapper) {
         RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
         rocketMQTemplate.setProducer(mqProducer);
-        if (Objects.nonNull(objectMapper)) {
-            rocketMQTemplate.setObjectMapper(objectMapper);
-        } else {
-            throw new IllegalStateException("Can not inject null objectMapper into RocketMQTemplate!");
-        }
+        rocketMQTemplate.setObjectMapper(rocketMQMessageObjectMapper);
         return rocketMQTemplate;
     }
 
@@ -96,16 +90,4 @@ public static RocketMQTransactionAnnotationProcessor transactionAnnotationProces
         return new RocketMQTransactionAnnotationProcessor(transactionHandlerRegistry);
     }
 
-    @Configuration
-    @ConditionalOnClass(ObjectMapper.class)
-    static class JacksonConfiguration {
-
-        @Bean
-        @ConditionalOnMissingBean(ObjectMapper.class)
-        public ObjectMapper rocketMQMessageObjectMapper() {
-            return new ObjectMapper();
-        }
-
-    }
-
 }
diff --git a/rocketmq-spring-boot/src/test/java/org/apache/rocketmq/spring/config/RocketMQAutoConfigurationTest.java b/rocketmq-spring-boot/src/test/java/org/apache/rocketmq/spring/config/RocketMQAutoConfigurationTest.java
index c0fa370..b819e3f 100644
--- a/rocketmq-spring-boot/src/test/java/org/apache/rocketmq/spring/config/RocketMQAutoConfigurationTest.java
+++ b/rocketmq-spring-boot/src/test/java/org/apache/rocketmq/spring/config/RocketMQAutoConfigurationTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.rocketmq.spring.config;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.rocketmq.client.producer.DefaultMQProducer;
 import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
 import org.apache.rocketmq.spring.core.RocketMQListener;
@@ -81,6 +82,28 @@ public void testRocketMQListenerContainer() {
 
     }
 
+    @Test
+    public void testRocketMQListenerWithCustomObjectMapper() {
+        runner.withPropertyValues("spring.rocketmq.name-server=127.0.0.1:9876").
+                withUserConfiguration(TestConfig.class, CustomObjectMapperConfig.class).
+                run((context) -> {
+                    assertThat(context).hasSingleBean(DefaultRocketMQListenerContainer.class);
+                    assertThat(context.getBean(DefaultRocketMQListenerContainer.class).getObjectMapper())
+                            .isSameAs(context.getBean(CustomObjectMapperConfig.class).testObjectMapper());
+                });
+    }
+
+    @Test
+    public void testRocketMQListenerWithSeveralObjectMappers() {
+        runner.withPropertyValues("spring.rocketmq.name-server=127.0.0.1:9876").
+                withUserConfiguration(TestConfig.class, CustomObjectMappersConfig.class).
+                run((context) -> {
+                    assertThat(context).hasSingleBean(DefaultRocketMQListenerContainer.class);
+                    assertThat(context.getBean(DefaultRocketMQListenerContainer.class).getObjectMapper())
+                            .isSameAs(context.getBean(CustomObjectMappersConfig.class).rocketMQMessageObjectMapper());
+                });
+    }
+
     @Configuration
     static class TestConfig {
 
@@ -90,6 +113,31 @@ public Object consumeListener() {
         }
     }
 
+    @Configuration
+    static class CustomObjectMapperConfig {
+
+        @Bean
+        public ObjectMapper testObjectMapper() {
+            return new ObjectMapper();
+        }
+
+    }
+
+    @Configuration
+    static class CustomObjectMappersConfig {
+
+        @Bean
+        public ObjectMapper testObjectMapper() {
+            return new ObjectMapper();
+        }
+
+        @Bean
+        public ObjectMapper rocketMQMessageObjectMapper() {
+            return new ObjectMapper();
+        }
+
+    }
+
     @RocketMQMessageListener(consumerGroup = "abc", topic = "test")
     static class MyMessageListener implements RocketMQListener {
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services