[ 
https://issues.apache.org/jira/browse/ROCKETMQ-120?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16226377#comment-16226377
 ] 

ASF GitHub Bot commented on ROCKETMQ-120:
-----------------------------------------

zhouxinyu commented on a change in pull request #32: [ROCKETMQ-120] Provider a 
adapter for spring and spring-boot
URL: https://github.com/apache/rocketmq-externals/pull/32#discussion_r147909531
 
 

 ##########
 File path: 
spring-boot-starter-rocketmq/src/main/java/org/apache/rocketmq/spring/starter/RocketMQAutoConfiguration.java
 ##########
 @@ -0,0 +1,190 @@
+/*
+ * 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.starter;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.rocketmq.spring.starter.annotation.RocketMQMessageListener;
+import 
org.apache.rocketmq.spring.starter.core.DefaultRocketMQListenerContainer;
+import org.apache.rocketmq.spring.starter.core.RocketMQListener;
+import org.apache.rocketmq.spring.starter.core.RocketMQTemplate;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.annotation.Resource;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
+import org.apache.rocketmq.client.impl.MQClientAPIImpl;
+import org.apache.rocketmq.client.producer.DefaultMQProducer;
+import org.springframework.aop.support.AopUtils;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+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.context.properties.EnableConfigurationProperties;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.annotation.Order;
+import org.springframework.core.env.StandardEnvironment;
+import org.springframework.util.Assert;
+
+@Configuration
+@EnableConfigurationProperties(RocketMQProperties.class)
+@ConditionalOnClass(MQClientAPIImpl.class)
+@Order
+@Slf4j
+public class RocketMQAutoConfiguration {
+
+    @Bean
+    @ConditionalOnClass(DefaultMQProducer.class)
+    @ConditionalOnMissingBean(DefaultMQProducer.class)
+    @ConditionalOnProperty(prefix = "spring.rocketmq", value = {"nameServer", 
"producer.group"})
+    public DefaultMQProducer mqProducer(RocketMQProperties rocketMQProperties) 
{
+
+        RocketMQProperties.Producer producerConfig = 
rocketMQProperties.getProducer();
+        String groupName = producerConfig.getGroup();
+        Assert.hasText(groupName, "[spring.rocketmq.producer.group] must not 
be null");
+
+        DefaultMQProducer producer = new 
DefaultMQProducer(producerConfig.getGroup());
+        producer.setNamesrvAddr(rocketMQProperties.getNameServer());
+        producer.setSendMsgTimeout(producerConfig.getSendMsgTimeout());
+        
producer.setRetryTimesWhenSendFailed(producerConfig.getRetryTimesWhenSendFailed());
+        
producer.setRetryTimesWhenSendAsyncFailed(producerConfig.getRetryTimesWhenSendAsyncFailed());
+        producer.setMaxMessageSize(producerConfig.getMaxMessageSize());
+        
producer.setCompressMsgBodyOverHowmuch(producerConfig.getCompressMsgBodyOverHowmuch());
+        
producer.setRetryAnotherBrokerWhenNotStoreOK(producerConfig.isRetryAnotherBrokerWhenNotStoreOk());
+
+        return producer;
+    }
+
+    @Bean
+    @ConditionalOnClass(ObjectMapper.class)
+    @ConditionalOnMissingBean(name = "rocketMQMessageObjectMapper")
+    public ObjectMapper rocketMQMessageObjectMapper() {
+        return new ObjectMapper();
+    }
+
+    @Bean(destroyMethod = "destroy")
+    @ConditionalOnBean(DefaultMQProducer.class)
+    @ConditionalOnMissingBean(name = "rocketMQTemplate")
+    public RocketMQTemplate rocketMQTemplate(DefaultMQProducer mqProducer,
+        @Autowired(required = false)
+        @Qualifier("rocketMQMessageObjectMapper")
+            ObjectMapper objectMapper) {
+        RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
+        rocketMQTemplate.setProducer(mqProducer);
+        if (Objects.nonNull(objectMapper)) {
+            rocketMQTemplate.setObjectMapper(objectMapper);
+        }
+
+        return rocketMQTemplate;
+    }
+
+    @Configuration
+    @ConditionalOnClass(DefaultMQPushConsumer.class)
+    @EnableConfigurationProperties(RocketMQProperties.class)
+    @ConditionalOnProperty(prefix = "spring.rocketmq", value = "nameServer")
+    @Order
+    public static class ListenerContainerConfiguration implements 
ApplicationContextAware, InitializingBean {
+        private ConfigurableApplicationContext applicationContext;
+
+        private AtomicLong counter = new AtomicLong(0);
+
+        @Resource
+        private StandardEnvironment environment;
+
+        @Resource
+        private RocketMQProperties rocketMQProperties;
+
+        private ObjectMapper objectMapper;
+
+        public ListenerContainerConfiguration() {
+        }
+
+        @Autowired(required = false)
+        public ListenerContainerConfiguration(
+            @Qualifier("rocketMQMessageObjectMapper") ObjectMapper 
objectMapper) {
+            this.objectMapper = objectMapper;
+        }
+
+        @Override
+        public void setApplicationContext(ApplicationContext 
applicationContext) throws BeansException {
+            this.applicationContext = (ConfigurableApplicationContext) 
applicationContext;
+        }
+
+        @Override
+        public void afterPropertiesSet() throws Exception {
+            Map<String, Object> beans = 
this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class);
+
+            if (Objects.nonNull(beans)) {
+                beans.forEach(this::registerContainer);
+            }
+        }
+
+        private void registerContainer(String beanName, Object bean) {
+            Class<?> clazz = AopUtils.getTargetClass(bean);
+
+            if (!RocketMQListener.class.isAssignableFrom(bean.getClass())) {
+                throw new IllegalStateException(clazz + " is not instance of " 
+ RocketMQListener.class.getName());
+            }
+
+            RocketMQListener rocketMQListener = (RocketMQListener) bean;
+            RocketMQMessageListener annotation = 
clazz.getAnnotation(RocketMQMessageListener.class);
+            BeanDefinitionBuilder beanBuilder = 
BeanDefinitionBuilder.rootBeanDefinition(DefaultRocketMQListenerContainer.class);
+            beanBuilder.addPropertyValue("nameServer", 
rocketMQProperties.getNameServer());
+            beanBuilder.addPropertyValue("topic", 
environment.resolvePlaceholders(annotation.topic()));
+
+            beanBuilder.addPropertyValue("consumerGroup", 
environment.resolvePlaceholders(annotation.consumerGroup()));
 
 Review comment:
   So many hardcode strings, I suggest use a class to centralize these string 
constants.

----------------------------------------------------------------
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:
[email protected]


> Provide a adapter for spring and spring-boot
> --------------------------------------------
>
>                 Key: ROCKETMQ-120
>                 URL: https://issues.apache.org/jira/browse/ROCKETMQ-120
>             Project: Apache RocketMQ
>          Issue Type: Wish
>            Reporter: yukon
>            Assignee: dongeforever
>            Priority: Minor
>             Fix For: 4.2.0
>
>
> Provider a rocketmq-spring adapter for the convenience of the spring user.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to