rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinrongt...@apache.org
Subject [rocketmq-spring] branch master updated: chore(autoconfigure):polish the pr #210
Date Tue, 11 Feb 2020 02:34:44 GMT
This is an automated email from the ASF dual-hosted git repository.

jinrongtong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-spring.git


The following commit(s) were added to refs/heads/master by this push:
     new 82f3e58  chore(autoconfigure):polish the pr #210
82f3e58 is described below

commit 82f3e581926412175cc8570057cf931659581baa
Author: rongtongjin <794220751@qq.com>
AuthorDate: Mon Feb 10 15:32:21 2020 +0800

    chore(autoconfigure):polish the pr #210
---
 .../ExtProducerResetConfiguration.java             | 16 +++----
 .../ListenerContainerConfiguration.java            | 26 +++++------
 .../RocketMQTransactionConfiguration.java          | 19 ++++----
 .../rocketmq/spring/support/SpringBeanUtil.java    | 52 ----------------------
 4 files changed, 27 insertions(+), 86 deletions(-)

diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ExtProducerResetConfiguration.java
b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ExtProducerResetConfiguration.java
index 9ea7699..a0cc706 100644
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ExtProducerResetConfiguration.java
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/ExtProducerResetConfiguration.java
@@ -18,17 +18,17 @@
 package org.apache.rocketmq.spring.autoconfigure;
 
 import java.util.Map;
-import java.util.Objects;
+import java.util.stream.Collectors;
 import org.apache.rocketmq.client.exception.MQClientException;
 import org.apache.rocketmq.client.producer.DefaultMQProducer;
 import org.apache.rocketmq.spring.annotation.ExtRocketMQTemplateConfiguration;
 import org.apache.rocketmq.spring.core.RocketMQTemplate;
 import org.apache.rocketmq.spring.support.RocketMQMessageConverter;
 import org.apache.rocketmq.spring.support.RocketMQUtil;
-import org.apache.rocketmq.spring.support.SpringBeanUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.aop.framework.AopProxyUtils;
+import org.springframework.aop.scope.ScopedProxyUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.SmartInitializingSingleton;
 import org.springframework.beans.factory.support.BeanDefinitionValidationException;
@@ -53,7 +53,7 @@ public class ExtProducerResetConfiguration implements ApplicationContextAware,
S
     private RocketMQMessageConverter rocketMQMessageConverter;
 
     public ExtProducerResetConfiguration(RocketMQMessageConverter rocketMQMessageConverter,
-                                         StandardEnvironment environment, RocketMQProperties
rocketMQProperties) {
+        StandardEnvironment environment, RocketMQProperties rocketMQProperties) {
         this.rocketMQMessageConverter = rocketMQMessageConverter;
         this.environment = environment;
         this.rocketMQProperties = rocketMQProperties;
@@ -66,11 +66,11 @@ public class ExtProducerResetConfiguration implements ApplicationContextAware,
S
 
     @Override
     public void afterSingletonsInstantiated() {
-        Map<String, Object> beans = SpringBeanUtil.getBeansWithAnnotation(this.applicationContext,
ExtRocketMQTemplateConfiguration.class);
+        Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(ExtRocketMQTemplateConfiguration.class)
+            .entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
+            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
 
-        if (Objects.nonNull(beans)) {
-            beans.forEach(this::registerTemplate);
-        }
+        beans.forEach(this::registerTemplate);
     }
 
     private void registerTemplate(String beanName, Object bean) {
@@ -131,7 +131,7 @@ public class ExtProducerResetConfiguration implements ApplicationContextAware,
S
     }
 
     private void validate(ExtRocketMQTemplateConfiguration annotation,
-                          GenericApplicationContext genericApplicationContext) {
+        GenericApplicationContext genericApplicationContext) {
         if (genericApplicationContext.isBeanNameInUse(annotation.value())) {
             throw new BeanDefinitionValidationException(String.format("Bean {} has been used
in Spring Application Context, " +
                     "please check the @ExtRocketMQTemplateConfiguration",
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 008a4db..1e3f1ed 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
@@ -17,6 +17,10 @@
 
 package org.apache.rocketmq.spring.autoconfigure;
 
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
 import org.apache.rocketmq.client.AccessChannel;
 import org.apache.rocketmq.spring.annotation.ConsumeMode;
 import org.apache.rocketmq.spring.annotation.MessageModel;
@@ -25,10 +29,10 @@ import org.apache.rocketmq.spring.core.RocketMQListener;
 import org.apache.rocketmq.spring.core.RocketMQReplyListener;
 import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
 import org.apache.rocketmq.spring.support.RocketMQMessageConverter;
-import org.apache.rocketmq.spring.support.SpringBeanUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.aop.framework.AopProxyUtils;
+import org.springframework.aop.scope.ScopedProxyUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.SmartInitializingSingleton;
 import org.springframework.beans.factory.support.BeanDefinitionValidationException;
@@ -40,11 +44,6 @@ import org.springframework.context.support.GenericApplicationContext;
 import org.springframework.core.env.StandardEnvironment;
 import org.springframework.util.StringUtils;
 
-import java.util.Collections;
-import java.util.Map;
-import java.util.Objects;
-import java.util.concurrent.atomic.AtomicLong;
-
 @Configuration
 public class ListenerContainerConfiguration implements ApplicationContextAware, SmartInitializingSingleton
{
     private final static Logger log = LoggerFactory.getLogger(ListenerContainerConfiguration.class);
@@ -60,7 +59,7 @@ public class ListenerContainerConfiguration implements ApplicationContextAware,
     private RocketMQMessageConverter rocketMQMessageConverter;
 
     public ListenerContainerConfiguration(RocketMQMessageConverter rocketMQMessageConverter,
-                                          StandardEnvironment environment, RocketMQProperties
rocketMQProperties) {
+        StandardEnvironment environment, RocketMQProperties rocketMQProperties) {
         this.rocketMQMessageConverter = rocketMQMessageConverter;
         this.environment = environment;
         this.rocketMQProperties = rocketMQProperties;
@@ -73,12 +72,11 @@ public class ListenerContainerConfiguration implements ApplicationContextAware,
 
     @Override
     public void afterSingletonsInstantiated() {
-        Map<String, Object> beans = SpringBeanUtil.getBeansWithAnnotation(this.applicationContext,
RocketMQMessageListener.
-            class);
+        Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class)
+            .entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
+            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
 
-        if (Objects.nonNull(beans)) {
-            beans.forEach(this::registerContainer);
-        }
+        beans.forEach(this::registerContainer);
     }
 
     private void registerContainer(String beanName, Object bean) {
@@ -130,7 +128,7 @@ public class ListenerContainerConfiguration implements ApplicationContextAware,
     }
 
     private DefaultRocketMQListenerContainer createRocketMQListenerContainer(String name,
Object bean,
-                                                                             RocketMQMessageListener
annotation) {
+        RocketMQMessageListener annotation) {
         DefaultRocketMQListenerContainer container = new DefaultRocketMQListenerContainer();
 
         container.setRocketMQMessageListener(annotation);
@@ -148,13 +146,11 @@ public class ListenerContainerConfiguration implements ApplicationContextAware,
             container.setSelectorExpression(tags);
         }
         container.setConsumerGroup(environment.resolvePlaceholders(annotation.consumerGroup()));
-
         if (RocketMQListener.class.isAssignableFrom(bean.getClass())) {
             container.setRocketMQListener((RocketMQListener) bean);
         } else if (RocketMQReplyListener.class.isAssignableFrom(bean.getClass())) {
             container.setRocketMQReplyListener((RocketMQReplyListener) bean);
         }
-      
         container.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
         container.setName(name);
 
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQTransactionConfiguration.java
b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQTransactionConfiguration.java
index 2daefcf..74badbb 100644
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQTransactionConfiguration.java
+++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/autoconfigure/RocketMQTransactionConfiguration.java
@@ -18,20 +18,19 @@
 package org.apache.rocketmq.spring.autoconfigure;
 
 import java.util.Map;
-import java.util.Objects;
 import java.util.concurrent.LinkedBlockingDeque;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
-
+import java.util.stream.Collectors;
 import org.apache.rocketmq.client.producer.TransactionMQProducer;
 import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
 import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
 import org.apache.rocketmq.spring.core.RocketMQTemplate;
 import org.apache.rocketmq.spring.support.RocketMQUtil;
-import org.apache.rocketmq.spring.support.SpringBeanUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.aop.framework.AopProxyUtils;
+import org.springframework.aop.scope.ScopedProxyUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.SmartInitializingSingleton;
 import org.springframework.context.ApplicationContext;
@@ -46,18 +45,16 @@ public class RocketMQTransactionConfiguration implements ApplicationContextAware
 
     private ConfigurableApplicationContext applicationContext;
 
-    @Override
-    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
+    @Override public void setApplicationContext(ApplicationContext applicationContext) throws
BeansException {
         this.applicationContext = (ConfigurableApplicationContext) applicationContext;
     }
 
-    @Override
-    public void afterSingletonsInstantiated() {
-        Map<String, Object> beans = SpringBeanUtil.getBeansWithAnnotation(this.applicationContext,
RocketMQTransactionListener.class);
+    @Override public void afterSingletonsInstantiated() {
+        Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQTransactionListener.class)
+            .entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
+            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
 
-        if (Objects.nonNull(beans)) {
-            beans.forEach(this::registerTransactionListener);
-        }
+        beans.forEach(this::registerTransactionListener);
     }
 
     private void registerTransactionListener(String beanName, Object bean) {
diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/support/SpringBeanUtil.java
b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/support/SpringBeanUtil.java
deleted file mode 100644
index b5d1161..0000000
--- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/support/SpringBeanUtil.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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.support;
-
-import org.springframework.aop.scope.ScopedProxyUtils;
-import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.lang.NonNull;
-
-import java.lang.annotation.Annotation;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-public class SpringBeanUtil {
-
-    /**
-     * Override applicationContext.getBeansWithAnnotation method to make sure without same
ProxyTarget beans
-     *
-     * @param applicationContext spring Application Context
-     * @param clazz              annotation class
-     * @return beans map without proxyTarget bean
-     */
-    public static Map<String, Object> getBeansWithAnnotation(@NonNull ConfigurableApplicationContext
applicationContext, Class<? extends Annotation> clazz) {
-        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(clazz);
-        Map<String, Object> filterBeans = new HashMap<>(beans.size());
-        // remove proxy target
-        Set<Map.Entry<String, Object>> entrySet = beans.entrySet();
-        entrySet.forEach((entry) -> {
-            final String beanName = entry.getKey();
-            if (!ScopedProxyUtils.isScopedTarget(beanName)) {
-                filterBeans.put(beanName, entry.getValue());
-            }
-        });
-        return filterBeans;
-    }
-
-}


Mime
View raw message