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 2019/01/03 03:40:14 UTC

[GitHub] walking98 closed pull request #25: [ISSUE#21] Support both spring-boot 1.X and 2.X

walking98 closed pull request #25: [ISSUE#21] Support both spring-boot 1.X and 2.X
URL: https://github.com/apache/rocketmq-spring/pull/25
 
 
   

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-samples/pom.xml b/rocketmq-spring-boot-samples/pom.xml
index c4568fd..7214a03 100644
--- a/rocketmq-spring-boot-samples/pom.xml
+++ b/rocketmq-spring-boot-samples/pom.xml
@@ -40,6 +40,19 @@
     </properties>
 
     <dependencies>
+        <!-- comment out the following dependencies will use spring-boot 1.x
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot</artifactId>
+            <version>1.5.18.RELEASE</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+            <version>1.5.18.RELEASE</version>
+        </dependency>
+        -->
+
         <dependency>
             <groupId>org.apache.rocketmq</groupId>
             <artifactId>rocketmq-spring-boot-starter</artifactId>
@@ -47,6 +60,7 @@
         </dependency>
     </dependencies>
 
+
     <build>
         <plugins>
             <plugin>
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ListenerContainerConfiguration.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ListenerContainerConfiguration.java
index da66132..00add94 100644
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ListenerContainerConfiguration.java
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ListenerContainerConfiguration.java
@@ -29,6 +29,7 @@
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.SmartInitializingSingleton;
 import org.springframework.beans.factory.support.BeanDefinitionValidationException;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.context.ConfigurableApplicationContext;
@@ -42,6 +43,7 @@
 
 
 @Configuration
+@ConditionalOnClass(name = "org.springframework.beans.factory.config.BeanDefinitionCustomizer")
 public class ListenerContainerConfiguration implements ApplicationContextAware, SmartInitializingSingleton {
     private final static Logger log = LoggerFactory.getLogger(ListenerContainerConfiguration.class);
 
@@ -91,8 +93,8 @@ private void registerContainer(String beanName, Object bean) {
             counter.incrementAndGet());
         GenericApplicationContext genericApplicationContext = (GenericApplicationContext) applicationContext;
 
-        genericApplicationContext.registerBean(containerBeanName, DefaultRocketMQListenerContainer.class,
-            () -> createRocketMQListenerContainer(bean, annotation));
+        registerRocketMQListenerContainerBean(bean, annotation, containerBeanName, genericApplicationContext,
+            environment, rocketMQProperties, objectMapper);
         DefaultRocketMQListenerContainer container = genericApplicationContext.getBean(containerBeanName,
             DefaultRocketMQListenerContainer.class);
         if (!container.isRunning()) {
@@ -107,6 +109,16 @@ private void registerContainer(String beanName, Object bean) {
         log.info("Register the listener to container, listenerBeanName:{}, containerBeanName:{}", beanName, containerBeanName);
     }
 
+    public void registerRocketMQListenerContainerBean(Object bean, RocketMQMessageListener annotation,
+                                                      String containerBeanName,
+                                                      GenericApplicationContext genericApplicationContext,
+                                                      StandardEnvironment environment,
+                                                      RocketMQProperties rocketMQProperties,
+                                                      ObjectMapper objectMapper) {
+        genericApplicationContext.registerBean(containerBeanName, DefaultRocketMQListenerContainer.class,
+            () -> createRocketMQListenerContainer(bean, annotation));
+    }
+
     private DefaultRocketMQListenerContainer createRocketMQListenerContainer(Object bean, RocketMQMessageListener annotation) {
         DefaultRocketMQListenerContainer container = new DefaultRocketMQListenerContainer();
 
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ListenerContainerConfiguration1X.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ListenerContainerConfiguration1X.java
new file mode 100644
index 0000000..a098d69
--- /dev/null
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ListenerContainerConfiguration1X.java
@@ -0,0 +1,78 @@
+/*
+ * 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.autoconfigure;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
+import org.apache.rocketmq.spring.core.RocketMQListener;
+import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.env.StandardEnvironment;
+
+import java.util.Objects;
+
+/**
+ * Add this optional configuration for Spring 4.x due to ApplicationContext.registerBean() invalid in this version.
+ */
+
+@Configuration
+@ConditionalOnMissingClass("org.springframework.beans.factory.config.BeanDefinitionCustomizer")
+public class ListenerContainerConfiguration1X extends ListenerContainerConfiguration {
+    private final static Logger log = LoggerFactory.getLogger(ListenerContainerConfiguration1X.class);
+
+    public ListenerContainerConfiguration1X(ObjectMapper rocketMQMessageObjectMapper,
+                                            StandardEnvironment environment,
+                                            RocketMQProperties rocketMQProperties) {
+        super(rocketMQMessageObjectMapper, environment, rocketMQProperties);
+    }
+
+    @Override
+    public void registerRocketMQListenerContainerBean(Object bean, RocketMQMessageListener annotation,
+                                                      String containerBeanName,
+                                                      GenericApplicationContext genericApplicationContext,
+                                                      StandardEnvironment environment,
+                                                      RocketMQProperties rocketMQProperties,
+                                                      ObjectMapper objectMapper) {
+        BeanDefinitionBuilder beanBuilder = BeanDefinitionBuilder.rootBeanDefinition(
+            DefaultRocketMQListenerContainer.class);
+        // Note: The following properties come from the fields of DefaultRocketMQListenerContainer,
+        // don't forget to adapt them if the class has any change.
+        beanBuilder.addPropertyValue("nameServer", rocketMQProperties.getNameServer());
+        beanBuilder.addPropertyValue("topic", environment.resolvePlaceholders(annotation.topic()));
+
+        beanBuilder.addPropertyValue("consumerGroup", environment.resolvePlaceholders(annotation.consumerGroup()));
+        beanBuilder.addPropertyValue("consumeMode", annotation.consumeMode());
+        beanBuilder.addPropertyValue("consumeThreadMax", annotation.consumeThreadMax());
+        beanBuilder.addPropertyValue("messageModel", annotation.messageModel());
+        beanBuilder.addPropertyValue("selectorExpression",
+            environment.resolvePlaceholders(annotation.selectorExpression()));
+        beanBuilder.addPropertyValue("selectorType", annotation.selectorType());
+        beanBuilder.addPropertyValue("rocketMQListener", (RocketMQListener)bean);
+        if (Objects.nonNull(objectMapper)) {
+            beanBuilder.addPropertyValue("objectMapper", objectMapper);
+        }
+        beanBuilder.setDestroyMethodName("destroy");
+
+        genericApplicationContext.registerBeanDefinition(containerBeanName, beanBuilder.getBeanDefinition());
+    }
+}
\ No newline at end of file
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQAutoConfiguration.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQAutoConfiguration.java
index 78a6eba..caa9159 100644
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQAutoConfiguration.java
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQAutoConfiguration.java
@@ -42,7 +42,7 @@
 @EnableConfigurationProperties(RocketMQProperties.class)
 @ConditionalOnClass({ MQAdmin.class, ObjectMapper.class })
 @ConditionalOnProperty(prefix = "rocketmq", value = "name-server")
-@Import({ JacksonFallbackConfiguration.class, ListenerContainerConfiguration.class })
+@Import({ JacksonFallbackConfiguration.class, ListenerContainerConfiguration.class, ListenerContainerConfiguration1X.class })
 @AutoConfigureAfter(JacksonAutoConfiguration.class)
 public class RocketMQAutoConfiguration {
 
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/support/DefaultRocketMQListenerContainer.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/support/DefaultRocketMQListenerContainer.java
index 7031dce..96382be 100644
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/support/DefaultRocketMQListenerContainer.java
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/support/DefaultRocketMQListenerContainer.java
@@ -170,6 +170,26 @@ public void setRocketMQMessageListener(RocketMQMessageListener anno) {
         this.selectorType = anno.selectorType();
     }
 
+    public void setConsumeThreadMax(int consumeThreadMax) {
+        this.consumeThreadMax = consumeThreadMax;
+    }
+
+    public void setConsumeMode(ConsumeMode consumeMode) {
+        this.consumeMode = consumeMode;
+    }
+
+    public void setSelectorType(SelectorType selectorType) {
+        this.selectorType = selectorType;
+    }
+
+    public void setSelectorExpression(String selectorExpression) {
+        this.selectorExpression = selectorExpression;
+    }
+
+    public void setMessageModel(MessageModel messageModel) {
+        this.messageModel = messageModel;
+    }
+
     public ConsumeMode getConsumeMode() {
         return consumeMode;
     }


 

----------------------------------------------------------------
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